From 8f45f73340736518fb437a02ee72250250ee95ca Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Fri, 29 Nov 2013 14:43:54 +0400 Subject: [PATCH] JetControlFlowTest rewritten to generator --- .../tests/org/jetbrains/jet/JetTestUtils.java | 6 + ...Test.java => AbstractControlFlowTest.java} | 65 ++------- .../jet/cfg/ControlFlowTestGenerated.java | 134 ++++++++++++++++++ .../jet/generators/tests/GenerateTests.java | 8 ++ 4 files changed, 163 insertions(+), 50 deletions(-) rename compiler/tests/org/jetbrains/jet/cfg/{JetControlFlowTest.java => AbstractControlFlowTest.java} (89%) create mode 100644 compiler/tests/org/jetbrains/jet/cfg/ControlFlowTestGenerated.java diff --git a/compiler/tests/org/jetbrains/jet/JetTestUtils.java b/compiler/tests/org/jetbrains/jet/JetTestUtils.java index 0e597d6bfb8..fbbeab8ed02 100644 --- a/compiler/tests/org/jetbrains/jet/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/jet/JetTestUtils.java @@ -38,6 +38,7 @@ import com.intellij.util.containers.ContainerUtil; import junit.framework.TestCase; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; import org.jetbrains.jet.analyzer.AnalyzeExhaust; 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")); return test; } + + @NotNull + public static File replaceExtension(@NotNull File file, @Nullable String newExtension) { + return new File(file.getParentFile(), FileUtil.getNameWithoutExtension(file) + (newExtension == null ? "" : "." + newExtension)); + } } diff --git a/compiler/tests/org/jetbrains/jet/cfg/JetControlFlowTest.java b/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java similarity index 89% rename from compiler/tests/org/jetbrains/jet/cfg/JetControlFlowTest.java rename to compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java index dddd33d727c..b1698b855d4 100644 --- a/compiler/tests/org/jetbrains/jet/cfg/JetControlFlowTest.java +++ b/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java @@ -18,18 +18,15 @@ package org.jetbrains.jet.cfg; import com.google.common.collect.Sets; import com.intellij.openapi.util.io.FileUtil; -import junit.framework.Test; -import junit.framework.TestSuite; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.ConfigurationKind; -import org.jetbrains.jet.JetLiteFixture; -import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.analyzer.AnalyzeExhaust; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.lang.cfg.pseudocode.*; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.lazy.KotlinTestWithEnvironment; import java.io.File; import java.io.FileNotFoundException; @@ -37,40 +34,23 @@ import java.io.IOException; import java.io.PrintStream; import java.util.*; -public class JetControlFlowTest extends JetLiteFixture { +public abstract class AbstractControlFlowTest extends KotlinTestWithEnvironment { static { System.setProperty("idea.platform.prefix", "Idea"); } - private String myName; - - public JetControlFlowTest(String dataPath, String name) { - super(dataPath); - myName = name; - } - - @Override protected JetCoreEnvironment createEnvironment() { return createEnvironmentWithMockJdk(ConfigurationKind.JDK_ONLY); } - @Override - public String getName() { - return "test" + myName; - } - - protected String getTestFilePath() { - return myFullDataPath + "/" + myName; - } - - @Override - protected void runTest() throws Throwable { - JetFile file = loadPsiFile(myName + ".kt"); + protected void doTest(String fileName) throws Exception { + File file = new File(fileName); + JetFile jetFile = JetTestUtils.loadJetFile(getProject(), file); Map data = new LinkedHashMap(); - AnalyzeExhaust analyzeExhaust = JetTestUtils.analyzeFile(file); - List declarations = file.getDeclarations(); + AnalyzeExhaust analyzeExhaust = JetTestUtils.analyzeFile(jetFile); + List declarations = jetFile.getDeclarations(); BindingContext bindingContext = analyzeExhaust.getBindingContext(); for (JetDeclaration declaration : declarations) { Pseudocode pseudocode = PseudocodeUtil.generatePseudocode(declaration, bindingContext); @@ -82,19 +62,19 @@ public class JetControlFlowTest extends JetLiteFixture { } try { - processCFData(myName, data); + processCFData(file, data); } catch (IOException e) { throw new RuntimeException(e); } finally { 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 data) throws IOException { + private void processCFData(File file, Map data) throws IOException { Collection pseudocodes = data.values(); StringBuilder instructionDump = new StringBuilder(); @@ -140,11 +120,10 @@ public class JetControlFlowTest extends JetLiteFixture { } } - String expectedInstructionsFileName = getTestFilePath() + ".instructions"; - File expectedInstructionsFile = new File(expectedInstructionsFileName); + File expectedInstructionsFile = JetTestUtils.replaceExtension(file, "instructions"); if (!expectedInstructionsFile.exists()) { 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()); @@ -380,13 +359,12 @@ public class JetControlFlowTest extends JetLiteFixture { out.println(from + " -> " + to + label + ";"); } - private void dumpDot(String name, Collection pseudocodes) throws FileNotFoundException { - String graphFileName = getTestFilePath() + ".dot"; - File target = new File(graphFileName); + private void dumpDot(File file, Collection pseudocodes) throws FileNotFoundException { + File target = JetTestUtils.replaceExtension(file, "dot"); PrintStream out = new PrintStream(target); - out.println("digraph " + name + " {"); + out.println("digraph " + FileUtil.getNameWithoutExtension(file) + " {"); int[] count = new int[1]; Map nodeToName = new HashMap(); for (Pseudocode pseudocode : pseudocodes) { @@ -413,17 +391,4 @@ public class JetControlFlowTest extends JetLiteFixture { out.println("}"); 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; - } - } diff --git a/compiler/tests/org/jetbrains/jet/cfg/ControlFlowTestGenerated.java b/compiler/tests/org/jetbrains/jet/cfg/ControlFlowTestGenerated.java new file mode 100644 index 00000000000..75a463b29a4 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/cfg/ControlFlowTestGenerated.java @@ -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"); + } + +} diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java index a19a92f03ab..c4db518cc0b 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -18,6 +18,7 @@ package org.jetbrains.jet.generators.tests; import junit.framework.TestCase; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.cfg.AbstractControlFlowTest; import org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve; import org.jetbrains.jet.checkers.AbstractJetJsCheckerTest; import org.jetbrains.jet.checkers.AbstractJetPsiCheckerTest; @@ -267,6 +268,13 @@ public class GenerateTests { testModel("compiler/testData/cli/js", true, "args", "doJsTest") ); + generateTest( + "compiler/tests/", + "ControlFlowTestGenerated", + AbstractControlFlowTest.class, + testModel("compiler/testData/cfg") + ); + generateTest( "idea/tests/", "JetPsiMatcherTest",