diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/CodegenUtil.java b/compiler/backend/src/org/jetbrains/jet/codegen/CodegenUtil.java index 1b1bb5db25c..2a2fcf34ea4 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/CodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/CodegenUtil.java @@ -118,7 +118,7 @@ public class CodegenUtil { return false; } - private static void addSuperTypes(JetType type, Set set) { + public static void addSuperTypes(JetType type, Set set) { set.add(type); for (JetType jetType : type.getConstructor().getSupertypes()) { diff --git a/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java b/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java index 282fe666fa7..724cac07e35 100644 --- a/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java +++ b/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java @@ -30,6 +30,11 @@ public class CompileSession { private final List mySourceFiles = new ArrayList(); private final List myLibrarySourceFiles = new ArrayList(); private List myErrors = new ArrayList(); + + public BindingContext getMyBindingContext() { + return myBindingContext; + } + private BindingContext myBindingContext; public CompileSession(JetCoreEnvironment environment) { diff --git a/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java b/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java index 2fca4ed2987..44244460da5 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java @@ -1,9 +1,36 @@ package org.jetbrains.jet.codegen; +import com.intellij.openapi.util.ClassLoaderUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.local.CoreLocalFileSystem; +import com.intellij.testFramework.TestRunnerUtil; +import gnu.trove.THashSet; +import junit.framework.TestCase; +import junit.framework.TestResult; +import junit.framework.TestSuite; +import junit.textui.TestRunner; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.compiler.CompileEnvironment; import org.jetbrains.jet.compiler.CompileSession; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.psi.JetClass; +import org.jetbrains.jet.lang.psi.JetDeclaration; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lang.psi.JetPsiUtil; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.parsing.JetParsingTest; + +import java.beans.beancontext.BeanContext; +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; +import java.net.URL; +import java.net.URLClassLoader; +import java.net.URLDecoder; +import java.util.Set; /** * @author alex.tkachman @@ -84,4 +111,82 @@ public class StdlibTest extends CodegenTestCase { public void testKt274 () { // blackBoxFile("regressions/kt274.kt"); } + + public void testTestlib () { + try { + CompileSession session = new CompileSession(myEnvironment); + CompileEnvironment.initializeKotlinRuntime(myEnvironment); + URLClassLoader classLoader = (URLClassLoader) TestCase.class.getClassLoader(); + CoreLocalFileSystem localFileSystem = myEnvironment.getLocalFileSystem(); + for(URL url: classLoader.getURLs()) { + if(url.getProtocol().equals("file") && url.getPath().contains("junit")) { + File file = new File(URLDecoder.decode(url.getPath())); + if(file.exists()) { + myEnvironment.addToClasspath(file); + } + } + } + VirtualFile path = localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../testlib/test"); + session.addSources(path); + session.addStdLibSources(true); + + if (!session.analyze(System.out)) { + throw new RuntimeException(); + } + + ClassFileFactory classFileFactory = session.generate(); + GeneratedClassLoader loader = new GeneratedClassLoader(classFileFactory); + + JetTypeMapper typeMapper = new JetTypeMapper(classFileFactory.state.getStandardLibrary(), session.getMyBindingContext()); + TestSuite suite = new TestSuite("StandardLibrary"); + try { + for(JetFile jetFile : session.getSourceFileNamespaces()) { + for(JetDeclaration decl : jetFile.getDeclarations()) { + if(decl instanceof JetClass) { + JetClass jetClass = (JetClass) decl; + + ClassDescriptor descriptor = (ClassDescriptor) session.getMyBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, jetClass); + Set allSuperTypes = new THashSet(); + CodegenUtil.addSuperTypes(descriptor.getDefaultType(), allSuperTypes); + + for(JetType type : allSuperTypes) { + String internalName = typeMapper.mapType(type).getInternalName(); + if(internalName.equals("junit/framework/Test")) { + String name = typeMapper.mapType(descriptor.getDefaultType()).getInternalName(); + System.out.println(name); + Class aClass = (Class) loader.loadClass(name.replace('/', '.')); + if((aClass.getModifiers() & Modifier.ABSTRACT) == 0 + && (aClass.getModifiers() & Modifier.PUBLIC) != 0) { + try { + Constructor constructor = aClass.getConstructor(); + if(constructor != null && (constructor.getModifiers() & Modifier.PUBLIC) != 0) { + suite.addTestSuite(aClass); + } + } + catch (NoSuchMethodException e) { + } + } + break; + } + } + } + } + } + } + finally { + typeMapper = null; + } + + TestResult testResult = TestRunner.run(suite); + assertEquals(0, testResult.errorCount()); + assertEquals(0, testResult.failureCount()); + + + } catch (RuntimeException e) { + System.out.println(generateToText()); + throw e; + } catch (Throwable e) { + throw new RuntimeException(e); + } + } } diff --git a/testlib/test/Test.kt b/testlib/test/Test.kt index ecac22b3faa..7c433ccae60 100644 --- a/testlib/test/Test.kt +++ b/testlib/test/Test.kt @@ -9,7 +9,7 @@ import org.junit.runner.* import org.junit.runner.notification.* import junit.framework.* -class BuiltTest(name: String, val builder: TestBuilder, val test: BuiltTest.() -> Unit) : TestCase(name) { +protected class BuiltTest(name: String, val builder: TestBuilder, val test: BuiltTest.() -> Unit) : TestCase(name) { private var myState: T? = null var state : T