From cb37f7b56f9679279cc9f83864eb7067abadb0d9 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 23 Nov 2011 09:56:20 +0200 Subject: [PATCH] ability to test stdlib --- .../jet/codegen/GenerationState.java | 14 ++++ .../jet/compiler/CompileSession.java | 5 ++ .../testData/codegen/inputStreamIterator.jet | 13 +--- .../org/jetbrains/jet/JetLiteFixture.java | 2 +- .../jet/codegen/CodegenTestCase.java | 17 ++--- .../org/jetbrains/jet/codegen/StdlibTest.java | 67 ++++++++++++++++--- stdlib/ktSrc/StandardLibrary.kt | 17 ++--- 7 files changed, 94 insertions(+), 41 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java b/compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java index 52e37842318..56b432cf2c9 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java @@ -157,4 +157,18 @@ public class GenerationState { }); } + public String createText() { + StringBuilder answer = new StringBuilder(); + + final ClassFileFactory factory = getFactory(); + List files = factory.files(); + for (String file : files) { + if (!file.startsWith("std/")) { + answer.append("@").append(file).append('\n'); + answer.append(factory.asText(file)); + } + } + + return answer.toString(); + } } diff --git a/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java b/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java index 7f643b90d14..e90fda9eb8a 100644 --- a/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java +++ b/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java @@ -99,4 +99,9 @@ public class CompileSession { return generationState.getFactory(); } + public String generateText() { + GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.TEXT); + generationState.compileCorrectNamespaces(myBindingContext, mySourceFileNamespaces); + return generationState.createText(); + } } diff --git a/compiler/testData/codegen/inputStreamIterator.jet b/compiler/testData/codegen/inputStreamIterator.jet index 2c1d4e89857..7be02b077f2 100644 --- a/compiler/testData/codegen/inputStreamIterator.jet +++ b/compiler/testData/codegen/inputStreamIterator.jet @@ -1,16 +1,7 @@ +import std.io.* + import java.io.* -val ByteArray.inputStream : ByteArrayInputStream - get() = ByteArrayInputStream(this) - -fun InputStream.iterator() : ByteIterator = - object: ByteIterator() { - override val hasNext : Boolean - get() = available() > 0 - - override fun nextByte() = read().byt - } - fun box() : String { val x = ByteArray (10) diff --git a/compiler/tests/org/jetbrains/jet/JetLiteFixture.java b/compiler/tests/org/jetbrains/jet/JetLiteFixture.java index f975ba79778..3ed62e38ddb 100644 --- a/compiler/tests/org/jetbrains/jet/JetLiteFixture.java +++ b/compiler/tests/org/jetbrains/jet/JetLiteFixture.java @@ -28,7 +28,7 @@ public abstract class JetLiteFixture extends UsefulTestCase { @NonNls protected final String myFullDataPath; protected JetFile myFile; - private JetCoreEnvironment myEnvironment; + protected JetCoreEnvironment myEnvironment; public JetLiteFixture(@NonNls String dataPath) { myFullDataPath = getTestDataPath() + "/" + dataPath; diff --git a/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java b/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java index 45723092fcd..8c584e9cc26 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java +++ b/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java @@ -93,25 +93,16 @@ public abstract class CodegenTestCase extends JetLiteFixture { final JetNamespace namespace = myFile.getRootNamespace(); String fqName = NamespaceCodegen.getJVMClassName(CodegenUtil.getFQName(namespace)).replace("/", "."); Class namespaceClass = loader.loadClass(fqName); - Method method = namespaceClass.getMethod("box"); - return (String) method.invoke(null); - } + Method method = namespaceClass.getMethod("box"); + return (String) method.invoke(null); + } protected String generateToText() { GenerationState state = new GenerationState(getProject(), ClassBuilderFactory.TEXT); AnalyzingUtils.checkForSyntacticErrors(myFile); state.compile(myFile); - StringBuilder answer = new StringBuilder(); - - final ClassFileFactory factory = state.getFactory(); - List files = factory.files(); - for (String file : files) { - answer.append("@").append(file).append('\n'); - answer.append(factory.asText(file)); - } - - return answer.toString(); + return state.createText(); } protected Class generateNamespaceClass() { diff --git a/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java b/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java index 1e3f514a26a..14ee5b98b5b 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java @@ -1,25 +1,76 @@ package org.jetbrains.jet.codegen; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.CharsetToolkit; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; +import org.jetbrains.jet.compiler.CompileSession; import org.jetbrains.jet.lang.psi.JetNamespace; +import org.jetbrains.jet.lang.resolve.AnalyzingUtils; +import org.jetbrains.jet.parsing.JetParsingTest; -import java.lang.reflect.Method; +import java.io.File; +import java.io.IOException; +import java.util.List; /** * @author alex.tkachman */ public class StdlibTest extends CodegenTestCase { - public void testLib () throws ClassNotFoundException { + @Override + protected void setUp() throws Exception { + super.setUp(); createEnvironmentWithFullJdk(); - loadFile("../../../stdlib/ktSrc/StandardLibrary.kt"); - ClassFileFactory codegens = generateClassesInFile(); - GeneratedClassLoader loader = new GeneratedClassLoader(codegens); + } + + protected String generateToText() { + CompileSession session = new CompileSession(myEnvironment); - final JetNamespace namespace = myFile.getRootNamespace(); - String fqName = NamespaceCodegen.getJVMClassName(CodegenUtil.getFQName(namespace)).replace("/", "."); - Class namespaceClass = loader.loadClass(fqName); + session.addSources(myFile.getVirtualFile()); + try { + session.addSources(addStdLib()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + if (!session.analyze(System.out)) { + return null; + } + + return session.generateText(); + } + + protected ClassFileFactory generateClassesInFile() { + try { + CompileSession session = new CompileSession(myEnvironment); + + session.addSources(myFile.getVirtualFile()); + session.addSources(addStdLib()); + + if (!session.analyze(System.out)) { + return null; + } + + return session.generate(); + } catch (RuntimeException e) { + System.out.println(generateToText()); + throw e; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private VirtualFile addStdLib() throws IOException { + String text = FileUtil.loadFile(new File(JetParsingTest.getTestDataDir() + "/../../stdlib/ktSrc/StandardLibrary.kt"), CharsetToolkit.UTF8).trim(); + text = StringUtil.convertLineSeparators(text); + PsiFile stdLibFile = createFile("StandardLibrary.kt", text); + return stdLibFile.getVirtualFile(); } public void testInputStreamIterator () { blackBoxFile("inputStreamIterator.jet"); + System.out.println(generateToText()); } } diff --git a/stdlib/ktSrc/StandardLibrary.kt b/stdlib/ktSrc/StandardLibrary.kt index 3fc3bffbcf0..86d4b033c29 100644 --- a/stdlib/ktSrc/StandardLibrary.kt +++ b/stdlib/ktSrc/StandardLibrary.kt @@ -30,7 +30,15 @@ namespace io { val ByteArray.inputStream : ByteArrayInputStream get() = ByteArrayInputStream(this) - inline fun ByteArray.inputStream(offset: Int, length: Int) : ByteArrayInputStream = ByteArrayInputStream(this, offset, length) +// inline fun ByteArray.inputStream(offset: Int, length: Int) = ByteArrayInputStream(this, offset, length) + + fun InputStream.iterator() : ByteIterator = + object: ByteIterator() { + override val hasNext : Boolean + get() = available() > 0 + + override fun nextByte() = read().byt + } val InputStream.buffered : BufferedInputStream get() = if(this is BufferedInputStream) this else BufferedInputStream(this) @@ -53,13 +61,6 @@ namespace io { inline fun InputStream.reader(charsetDecoder: CharsetDecoder) = InputStreamReader(this, charsetDecoder) */ - fun InputStream.iterator() : ByteIterator = object: ByteIterator() { - override val hasNext : Boolean - get() = available() > 0 - - override fun nextByte() = read().byt - } - // val Reader.buffered : BufferedReader // get() = if(this instanceof BufferedReader) this else BufferedReader(this)