codegen tests use JetLiteFixture

This commit is contained in:
Dmitry Jemerov
2011-10-27 17:46:10 +02:00
parent 1f7a784732
commit 22afa7dcf4
2 changed files with 22 additions and 28 deletions
@@ -1,17 +1,14 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetLightProjectDescriptor; import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespace; import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils; import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.parsing.JetParsingTest; import org.jetbrains.jet.parsing.JetParsingTest;
import org.jetbrains.jet.plugin.JetFileType;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
@@ -21,8 +18,9 @@ import java.util.Map;
/** /**
* @author yole * @author yole
*/ */
public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase { public abstract class CodegenTestCase extends JetLiteFixture {
private MyClassLoader myClassLoader; private MyClassLoader myClassLoader;
private JetFile myFile;
protected static void assertThrows(Method foo, Class<? extends Throwable> exceptionClass, Object instance, Object... args) throws IllegalAccessException { protected static void assertThrows(Method foo, Class<? extends Throwable> exceptionClass, Object instance, Object... args) throws IllegalAccessException {
boolean caught = false; boolean caught = false;
@@ -43,16 +41,24 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
myFile = null;
myClassLoader = null; myClassLoader = null;
super.tearDown(); super.tearDown();
} }
protected void loadText(final String text) { protected void loadText(final String text) {
myFixture.configureByText(JetFileType.INSTANCE, text); myFile = (JetFile) createFile("a.jet", text);
} }
protected void loadFile(final String name) { @Override
myFixture.configureByFile(JetParsingTest.getTestDataDir() + "/codegen/" + name); protected String loadFile(final String name) {
try {
final String content = doLoadFile(JetParsingTest.getTestDataDir() + "/codegen/", name);
myFile = (JetFile) createFile(name, content);
return content;
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
protected void loadFile() { protected void loadFile() {
@@ -85,8 +91,7 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
ClassFileFactory codegens = generateClassesInFile(); ClassFileFactory codegens = generateClassesInFile();
CodegensClassLoader loader = new CodegensClassLoader(codegens); CodegensClassLoader loader = new CodegensClassLoader(codegens);
JetFile jetFile = (JetFile) myFixture.getFile(); final JetNamespace namespace = myFile.getRootNamespace();
final JetNamespace namespace = jetFile.getRootNamespace();
String fqName = NamespaceCodegen.getJVMClassName(namespace.getFQName()).replace("/", "."); String fqName = NamespaceCodegen.getJVMClassName(namespace.getFQName()).replace("/", ".");
Class<?> namespaceClass = loader.loadClass(fqName); Class<?> namespaceClass = loader.loadClass(fqName);
Method method = namespaceClass.getMethod("box"); Method method = namespaceClass.getMethod("box");
@@ -95,9 +100,8 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
protected String generateToText() { protected String generateToText() {
GenerationState state = new GenerationState(getProject(), true); GenerationState state = new GenerationState(getProject(), true);
JetFile jetFile = (JetFile) myFixture.getFile(); AnalyzingUtils.checkForSyntacticErrors(myFile);
AnalyzingUtils.checkForSyntacticErrors(jetFile); state.compile(myFile);
state.compile(jetFile);
StringBuilder answer = new StringBuilder(); StringBuilder answer = new StringBuilder();
@@ -117,8 +121,7 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
} }
protected Class loadRootNamespaceClass(ClassFileFactory state) { protected Class loadRootNamespaceClass(ClassFileFactory state) {
JetFile jetFile = (JetFile) myFixture.getFile(); final JetNamespace namespace = myFile.getRootNamespace();
final JetNamespace namespace = jetFile.getRootNamespace();
String fqName = NamespaceCodegen.getJVMClassName(namespace.getFQName()).replace("/", "."); String fqName = NamespaceCodegen.getJVMClassName(namespace.getFQName()).replace("/", ".");
Map<String, Class> classMap = loadAllClasses(state); Map<String, Class> classMap = loadAllClasses(state);
return classMap.get(fqName); return classMap.get(fqName);
@@ -151,9 +154,8 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
protected ClassFileFactory generateClassesInFile() { protected ClassFileFactory generateClassesInFile() {
try { try {
GenerationState state = new GenerationState(getProject(), false); GenerationState state = new GenerationState(getProject(), false);
JetFile jetFile = (JetFile) myFixture.getFile(); AnalyzingUtils.checkForSyntacticErrors(myFile);
AnalyzingUtils.checkForSyntacticErrors(jetFile); state.compile(myFile);
state.compile(jetFile);
return state.getFactory(); return state.getFactory();
} catch (RuntimeException e) { } catch (RuntimeException e) {
@@ -233,10 +235,4 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase {
return super.findClass(name); return super.findClass(name);
} }
} }
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JetLightProjectDescriptor.INSTANCE;
}
} }
@@ -2,10 +2,8 @@ package org.jetbrains.jet.codegen;
import jet.IntRange; import jet.IntRange;
import jet.Tuple2; import jet.Tuple2;
import jet.Tuple3;
import jet.Tuple4; import jet.Tuple4;
import jet.typeinfo.TypeInfo; import jet.typeinfo.TypeInfo;
import org.jetbrains.jet.parsing.JetParsingTest;
import java.awt.*; import java.awt.*;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@@ -48,7 +46,7 @@ public class NamespaceGenTest extends CodegenTestCase {
} }
public void testLocalProperty() throws Exception { public void testLocalProperty() throws Exception {
myFixture.configureByFile(JetParsingTest.getTestDataDir() + "/codegen/localProperty.jet"); loadFile("localProperty.jet");
final String text = generateToText(); final String text = generateToText();
System.out.println(text); System.out.println(text);