JetControlFlowTest rewritten to generator

This commit is contained in:
Andrey Breslav
2013-11-29 14:43:54 +04:00
parent 93160431f9
commit 8f45f73340
4 changed files with 163 additions and 50 deletions
@@ -38,6 +38,7 @@ import com.intellij.util.containers.ContainerUtil;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly; import org.jetbrains.annotations.TestOnly;
import org.jetbrains.jet.analyzer.AnalyzeExhaust; import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.cli.jvm.compiler.CliLightClassGenerationSupport; import org.jetbrains.jet.cli.jvm.compiler.CliLightClassGenerationSupport;
@@ -713,4 +714,9 @@ public class JetTestUtils {
test.initialize(new WritableScopeImpl(JetScope.EMPTY, test, RedeclarationHandler.DO_NOTHING, "members of test namespace")); test.initialize(new WritableScopeImpl(JetScope.EMPTY, test, RedeclarationHandler.DO_NOTHING, "members of test namespace"));
return test; return test;
} }
@NotNull
public static File replaceExtension(@NotNull File file, @Nullable String newExtension) {
return new File(file.getParentFile(), FileUtil.getNameWithoutExtension(file) + (newExtension == null ? "" : "." + newExtension));
}
} }
@@ -18,18 +18,15 @@ package org.jetbrains.jet.cfg;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtil;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.analyzer.AnalyzeExhaust; import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.lang.cfg.pseudocode.*; import org.jetbrains.jet.lang.cfg.pseudocode.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.lazy.KotlinTestWithEnvironment;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@@ -37,40 +34,23 @@ import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.*; import java.util.*;
public class JetControlFlowTest extends JetLiteFixture { public abstract class AbstractControlFlowTest extends KotlinTestWithEnvironment {
static { static {
System.setProperty("idea.platform.prefix", "Idea"); System.setProperty("idea.platform.prefix", "Idea");
} }
private String myName;
public JetControlFlowTest(String dataPath, String name) {
super(dataPath);
myName = name;
}
@Override @Override
protected JetCoreEnvironment createEnvironment() { protected JetCoreEnvironment createEnvironment() {
return createEnvironmentWithMockJdk(ConfigurationKind.JDK_ONLY); return createEnvironmentWithMockJdk(ConfigurationKind.JDK_ONLY);
} }
@Override protected void doTest(String fileName) throws Exception {
public String getName() { File file = new File(fileName);
return "test" + myName; JetFile jetFile = JetTestUtils.loadJetFile(getProject(), file);
}
protected String getTestFilePath() {
return myFullDataPath + "/" + myName;
}
@Override
protected void runTest() throws Throwable {
JetFile file = loadPsiFile(myName + ".kt");
Map<JetElement, Pseudocode> data = new LinkedHashMap<JetElement, Pseudocode>(); Map<JetElement, Pseudocode> data = new LinkedHashMap<JetElement, Pseudocode>();
AnalyzeExhaust analyzeExhaust = JetTestUtils.analyzeFile(file); AnalyzeExhaust analyzeExhaust = JetTestUtils.analyzeFile(jetFile);
List<JetDeclaration> declarations = file.getDeclarations(); List<JetDeclaration> declarations = jetFile.getDeclarations();
BindingContext bindingContext = analyzeExhaust.getBindingContext(); BindingContext bindingContext = analyzeExhaust.getBindingContext();
for (JetDeclaration declaration : declarations) { for (JetDeclaration declaration : declarations) {
Pseudocode pseudocode = PseudocodeUtil.generatePseudocode(declaration, bindingContext); Pseudocode pseudocode = PseudocodeUtil.generatePseudocode(declaration, bindingContext);
@@ -82,19 +62,19 @@ public class JetControlFlowTest extends JetLiteFixture {
} }
try { try {
processCFData(myName, data); processCFData(file, data);
} }
catch (IOException e) { catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
finally { finally {
if ("true".equals(System.getProperty("jet.control.flow.test.dump.graphs"))) { if ("true".equals(System.getProperty("jet.control.flow.test.dump.graphs"))) {
dumpDot(myName, data.values()); dumpDot(file, data.values());
} }
} }
} }
private void processCFData(String name, Map<JetElement, Pseudocode> data) throws IOException { private void processCFData(File file, Map<JetElement, Pseudocode> data) throws IOException {
Collection<Pseudocode> pseudocodes = data.values(); Collection<Pseudocode> pseudocodes = data.values();
StringBuilder instructionDump = new StringBuilder(); StringBuilder instructionDump = new StringBuilder();
@@ -140,11 +120,10 @@ public class JetControlFlowTest extends JetLiteFixture {
} }
} }
String expectedInstructionsFileName = getTestFilePath() + ".instructions"; File expectedInstructionsFile = JetTestUtils.replaceExtension(file, "instructions");
File expectedInstructionsFile = new File(expectedInstructionsFileName);
if (!expectedInstructionsFile.exists()) { if (!expectedInstructionsFile.exists()) {
FileUtil.writeToFile(expectedInstructionsFile, instructionDump.toString()); FileUtil.writeToFile(expectedInstructionsFile, instructionDump.toString());
fail("No expected instructions for " + name + " generated result is written into " + expectedInstructionsFileName); fail("No expected instructions for " + FileUtil.getNameWithoutExtension(file) + " generated result is written into " + expectedInstructionsFile);
} }
JetTestUtils.assertEqualsToFile(expectedInstructionsFile, instructionDump.toString()); JetTestUtils.assertEqualsToFile(expectedInstructionsFile, instructionDump.toString());
@@ -380,13 +359,12 @@ public class JetControlFlowTest extends JetLiteFixture {
out.println(from + " -> " + to + label + ";"); out.println(from + " -> " + to + label + ";");
} }
private void dumpDot(String name, Collection<Pseudocode> pseudocodes) throws FileNotFoundException { private void dumpDot(File file, Collection<Pseudocode> pseudocodes) throws FileNotFoundException {
String graphFileName = getTestFilePath() + ".dot"; File target = JetTestUtils.replaceExtension(file, "dot");
File target = new File(graphFileName);
PrintStream out = new PrintStream(target); PrintStream out = new PrintStream(target);
out.println("digraph " + name + " {"); out.println("digraph " + FileUtil.getNameWithoutExtension(file) + " {");
int[] count = new int[1]; int[] count = new int[1];
Map<Instruction, String> nodeToName = new HashMap<Instruction, String>(); Map<Instruction, String> nodeToName = new HashMap<Instruction, String>();
for (Pseudocode pseudocode : pseudocodes) { for (Pseudocode pseudocode : pseudocodes) {
@@ -413,17 +391,4 @@ public class JetControlFlowTest extends JetLiteFixture {
out.println("}"); out.println("}");
out.close(); out.close();
} }
public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTest(JetTestCaseBuilder.suiteForDirectory(JetTestCaseBuilder.getTestDataPathBase(), "/cfg/", true, new JetTestCaseBuilder.NamedTestFactory() {
@NotNull
@Override
public Test createTest(@NotNull String dataPath, @NotNull String name, @NotNull File file) {
return new JetControlFlowTest(dataPath, name);
}
}));
return suite;
}
} }
@@ -0,0 +1,134 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cfg;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.cfg.AbstractControlFlowTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/cfg")
public class ControlFlowTestGenerated extends AbstractControlFlowTest {
public void testAllFilesPresentInCfg() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/cfg"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("AnonymousInitializers.kt")
public void testAnonymousInitializers() throws Exception {
doTest("compiler/testData/cfg/AnonymousInitializers.kt");
}
@TestMetadata("ArrayAccess.kt")
public void testArrayAccess() throws Exception {
doTest("compiler/testData/cfg/ArrayAccess.kt");
}
@TestMetadata("Assignments.kt")
public void testAssignments() throws Exception {
doTest("compiler/testData/cfg/Assignments.kt");
}
@TestMetadata("Basic.kt")
public void testBasic() throws Exception {
doTest("compiler/testData/cfg/Basic.kt");
}
@TestMetadata("DeadCode.kt")
public void testDeadCode() throws Exception {
doTest("compiler/testData/cfg/DeadCode.kt");
}
@TestMetadata("DelegatedProperty.kt")
public void testDelegatedProperty() throws Exception {
doTest("compiler/testData/cfg/DelegatedProperty.kt");
}
@TestMetadata("EmptyFunction.kt")
public void testEmptyFunction() throws Exception {
doTest("compiler/testData/cfg/EmptyFunction.kt");
}
@TestMetadata("FailFunction.kt")
public void testFailFunction() throws Exception {
doTest("compiler/testData/cfg/FailFunction.kt");
}
@TestMetadata("Finally.kt")
public void testFinally() throws Exception {
doTest("compiler/testData/cfg/Finally.kt");
}
@TestMetadata("FinallyTestCopy.kt")
public void testFinallyTestCopy() throws Exception {
doTest("compiler/testData/cfg/FinallyTestCopy.kt");
}
@TestMetadata("For.kt")
public void testFor() throws Exception {
doTest("compiler/testData/cfg/For.kt");
}
@TestMetadata("If.kt")
public void testIf() throws Exception {
doTest("compiler/testData/cfg/If.kt");
}
@TestMetadata("LazyBooleans.kt")
public void testLazyBooleans() throws Exception {
doTest("compiler/testData/cfg/LazyBooleans.kt");
}
@TestMetadata("LocalDeclarations.kt")
public void testLocalDeclarations() throws Exception {
doTest("compiler/testData/cfg/LocalDeclarations.kt");
}
@TestMetadata("MultiDecl.kt")
public void testMultiDecl() throws Exception {
doTest("compiler/testData/cfg/MultiDecl.kt");
}
@TestMetadata("ObjectExpression.kt")
public void testObjectExpression() throws Exception {
doTest("compiler/testData/cfg/ObjectExpression.kt");
}
@TestMetadata("OnlyWhileInFunctionBody.kt")
public void testOnlyWhileInFunctionBody() throws Exception {
doTest("compiler/testData/cfg/OnlyWhileInFunctionBody.kt");
}
@TestMetadata("ReturnFromExpression.kt")
public void testReturnFromExpression() throws Exception {
doTest("compiler/testData/cfg/ReturnFromExpression.kt");
}
@TestMetadata("ShortFunction.kt")
public void testShortFunction() throws Exception {
doTest("compiler/testData/cfg/ShortFunction.kt");
}
}
@@ -18,6 +18,7 @@ package org.jetbrains.jet.generators.tests;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cfg.AbstractControlFlowTest;
import org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve; import org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve;
import org.jetbrains.jet.checkers.AbstractJetJsCheckerTest; import org.jetbrains.jet.checkers.AbstractJetJsCheckerTest;
import org.jetbrains.jet.checkers.AbstractJetPsiCheckerTest; import org.jetbrains.jet.checkers.AbstractJetPsiCheckerTest;
@@ -267,6 +268,13 @@ public class GenerateTests {
testModel("compiler/testData/cli/js", true, "args", "doJsTest") testModel("compiler/testData/cli/js", true, "args", "doJsTest")
); );
generateTest(
"compiler/tests/",
"ControlFlowTestGenerated",
AbstractControlFlowTest.class,
testModel("compiler/testData/cfg")
);
generateTest( generateTest(
"idea/tests/", "idea/tests/",
"JetPsiMatcherTest", "JetPsiMatcherTest",