ability to test stdlib

This commit is contained in:
Alex Tkachman
2011-11-23 09:56:20 +02:00
parent 4bf3a61621
commit cb37f7b56f
7 changed files with 94 additions and 41 deletions
@@ -157,4 +157,18 @@ public class GenerationState {
});
}
public String createText() {
StringBuilder answer = new StringBuilder();
final ClassFileFactory factory = getFactory();
List<String> 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();
}
}
@@ -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();
}
}
@@ -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)
@@ -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;
@@ -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<String> 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() {
@@ -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());
}
}
+9 -8
View File
@@ -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)