From d373c09cfbd4eec467fdd05faebd30df2e410fed Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Sun, 6 Jul 2014 05:32:22 +0400 Subject: [PATCH] Generate REPL interpreter tests --- .../jet/repl/AbstractReplInterpreterTest.java | 68 ++++++++ .../jet/repl/ReplInterpreterTest.java | 155 ------------------ .../repl/ReplInterpreterTestGenerated.java | 109 ++++++++++++ .../jet/generators/tests/GenerateTests.kt | 5 + 4 files changed, 182 insertions(+), 155 deletions(-) create mode 100644 compiler/tests/org/jetbrains/jet/repl/AbstractReplInterpreterTest.java delete mode 100644 compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTest.java create mode 100644 compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTestGenerated.java diff --git a/compiler/tests/org/jetbrains/jet/repl/AbstractReplInterpreterTest.java b/compiler/tests/org/jetbrains/jet/repl/AbstractReplInterpreterTest.java new file mode 100644 index 00000000000..abcbd1ea4b9 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/repl/AbstractReplInterpreterTest.java @@ -0,0 +1,68 @@ +/* + * 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.repl; + +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.testFramework.UsefulTestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.ConfigurationKind; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.TestJdkKind; +import org.jetbrains.jet.cli.jvm.repl.ReplInterpreter; +import org.jetbrains.jet.config.CompilerConfiguration; +import org.junit.Assert; + +import java.io.File; + +public abstract class AbstractReplInterpreterTest extends UsefulTestCase { + static { + System.setProperty("java.awt.headless", "true"); + } + + protected void doTest(@NotNull String path) { + CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.FULL_JDK); + ReplInterpreter repl = new ReplInterpreter(getTestRootDisposable(), configuration); + + ReplSessionTestFile file = ReplSessionTestFile.load(new File(path)); + for (ReplSessionTestFile.OneLine line : file.getLines()) { + String code = line.getCode(); + + String expected = StringUtil.convertLineSeparators(line.getExpected()).replaceFirst("\n$", ""); + ReplSessionTestFile.MatchType matchType = line.getMatchType(); + + ReplInterpreter.LineResult lineResult = repl.eval(code); + Object actual; + if (lineResult.getType() == ReplInterpreter.LineResultType.SUCCESS) { + actual = lineResult.getValue(); + } + else if (lineResult.getType() == ReplInterpreter.LineResultType.INCOMPLETE) { + actual = "incomplete"; + } + else { + actual = lineResult.getErrorText(); + } + String actualString = StringUtil.convertLineSeparators(actual != null ? actual.toString() : "null").replaceFirst("\n$", ""); + + if (matchType == ReplSessionTestFile.MatchType.EQUALS) { + Assert.assertEquals("after evaluation of: " + code, expected, actualString); + } + else if (matchType == ReplSessionTestFile.MatchType.SUBSTRING) { + Assert.assertTrue("must contain substring: " + expected + ", actual: " + actualString, actualString.contains(expected)); + } + } + } +} diff --git a/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTest.java b/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTest.java deleted file mode 100644 index 1b181943b32..00000000000 --- a/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * 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.repl; - -import com.intellij.openapi.Disposable; -import com.intellij.openapi.util.Disposer; -import com.intellij.openapi.util.text.StringUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.ConfigurationKind; -import org.jetbrains.jet.JetTestUtils; -import org.jetbrains.jet.TestJdkKind; -import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys; -import org.jetbrains.jet.cli.jvm.repl.ReplInterpreter; -import org.jetbrains.jet.codegen.forTestCompile.ForTestCompileRuntime; -import org.jetbrains.jet.config.CompilerConfiguration; -import org.junit.After; -import org.junit.Assert; -import org.junit.Test; - -import java.io.File; - -public class ReplInterpreterTest { - - static { - System.setProperty("java.awt.headless", "true"); - } - - private final Disposable disposable = Disposer.newDisposable(); - - @After - public void tearDown() { - Disposer.dispose(disposable); - } - - private void testFile(@NotNull String relativePath) { - CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(ConfigurationKind.JDK_ONLY, TestJdkKind.FULL_JDK); - configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, ForTestCompileRuntime.runtimeJarForTests()); - ReplInterpreter repl = new ReplInterpreter(disposable, configuration); - - ReplSessionTestFile file = ReplSessionTestFile.load(new File("compiler/testData/repl/" + relativePath)); - for (ReplSessionTestFile.OneLine t : file.getLines()) { - String code = t.getCode(); - - String expected = StringUtil.convertLineSeparators(t.getExpected()).replaceFirst("\n$", ""); - ReplSessionTestFile.MatchType matchType = t.getMatchType(); - - ReplInterpreter.LineResult lineResult = repl.eval(code); - Object actual; - if (lineResult.getType() == ReplInterpreter.LineResultType.SUCCESS) { - actual = lineResult.getValue(); - } - else if (lineResult.getType() == ReplInterpreter.LineResultType.INCOMPLETE) { - actual = "incomplete"; - } - else { - actual = lineResult.getErrorText(); - } - String actualString = StringUtil.convertLineSeparators(actual != null ? actual.toString() : "null").replaceFirst("\n$", ""); - - if (matchType == ReplSessionTestFile.MatchType.EQUALS) { - Assert.assertEquals("after evaluation of: " + code, expected, actualString); - } - else if (matchType == ReplSessionTestFile.MatchType.SUBSTRING) { - Assert.assertTrue("must contain substring: " + expected + ", actual: " + actualString, actualString.contains(expected)); - } - - } - } - - @Test - public void constants() { - testFile("constants.repl"); - } - - @Test - public void simple() { - testFile("simple.repl"); - } - - @Test - public void function() { - testFile("function.repl"); - } - - @Test - public void functionReferencesPrev() { - testFile("functionReferencesPrev.repl"); - } - - @Test - public void twoClosures() { - testFile("twoClosures.repl"); - } - - @Test - public void functionOverloadResolutionAnyBeatsString() { - testFile("functionOverloadResolutionAnyBeatsString.repl"); - } - - @Test - public void functionOverloadResolution() { - testFile("functionOverloadResolution.repl"); - } - - @Test - public void empty() { - testFile("empty.repl"); - } - - @Test - public void imports() { - testFile("imports.repl"); - } - - - @Test - public void syntaxErrors() { - testFile("syntaxErrors.repl"); - } - - @Test - public void analyzeErrors() { - testFile("analyzeErrors.repl"); - } - - @Test - public void evaluationErrors() { - testFile("evaluationErrors.repl"); - } - - @Test - public void multiline() { - testFile("multiline.repl"); - } - - @Test - public void multiline3() { - testFile("multiline3.repl"); - } - -} diff --git a/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTestGenerated.java b/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTestGenerated.java new file mode 100644 index 00000000000..9f8105278ab --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTestGenerated.java @@ -0,0 +1,109 @@ +/* + * Copyright 2010-2014 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.repl; + +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.repl.AbstractReplInterpreterTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("compiler/testData/repl") +public class ReplInterpreterTestGenerated extends AbstractReplInterpreterTest { + public void testAllFilesPresentInRepl() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/repl"), Pattern.compile("^(.+)\\.repl$"), true); + } + + @TestMetadata("analyzeErrors.repl") + public void testAnalyzeErrors() throws Exception { + doTest("compiler/testData/repl/analyzeErrors.repl"); + } + + @TestMetadata("constants.repl") + public void testConstants() throws Exception { + doTest("compiler/testData/repl/constants.repl"); + } + + @TestMetadata("empty.repl") + public void testEmpty() throws Exception { + doTest("compiler/testData/repl/empty.repl"); + } + + @TestMetadata("evaluationErrors.repl") + public void testEvaluationErrors() throws Exception { + doTest("compiler/testData/repl/evaluationErrors.repl"); + } + + @TestMetadata("function.repl") + public void testFunction() throws Exception { + doTest("compiler/testData/repl/function.repl"); + } + + @TestMetadata("functionOverloadResolution.repl") + public void testFunctionOverloadResolution() throws Exception { + doTest("compiler/testData/repl/functionOverloadResolution.repl"); + } + + @TestMetadata("functionOverloadResolutionAnyBeatsString.repl") + public void testFunctionOverloadResolutionAnyBeatsString() throws Exception { + doTest("compiler/testData/repl/functionOverloadResolutionAnyBeatsString.repl"); + } + + @TestMetadata("functionReferencesPrev.repl") + public void testFunctionReferencesPrev() throws Exception { + doTest("compiler/testData/repl/functionReferencesPrev.repl"); + } + + @TestMetadata("imports.repl") + public void testImports() throws Exception { + doTest("compiler/testData/repl/imports.repl"); + } + + @TestMetadata("multiline.repl") + public void testMultiline() throws Exception { + doTest("compiler/testData/repl/multiline.repl"); + } + + @TestMetadata("multiline3.repl") + public void testMultiline3() throws Exception { + doTest("compiler/testData/repl/multiline3.repl"); + } + + @TestMetadata("simple.repl") + public void testSimple() throws Exception { + doTest("compiler/testData/repl/simple.repl"); + } + + @TestMetadata("syntaxErrors.repl") + public void testSyntaxErrors() throws Exception { + doTest("compiler/testData/repl/syntaxErrors.repl"); + } + + @TestMetadata("twoClosures.repl") + public void testTwoClosures() throws Exception { + doTest("compiler/testData/repl/twoClosures.repl"); + } + +} diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index a568427dd92..c9abf3c3c66 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -40,6 +40,7 @@ import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveRecursiveComparing import org.jetbrains.jet.modules.xml.AbstractModuleXmlParserTest import org.jetbrains.jet.jvm.compiler.AbstractWriteSignatureTest import org.jetbrains.jet.cli.AbstractKotlincExecutableTest +import org.jetbrains.jet.repl.AbstractReplInterpreterTest import org.jetbrains.jet.cfg.AbstractControlFlowTest import org.jetbrains.jet.psi.AbstractJetPsiMatcherTest import org.jetbrains.jet.checkers.AbstractJetPsiCheckerTest @@ -252,6 +253,10 @@ fun main(args: Array) { model("cli/js", extension = "args", testMethod = "doJsTest") } + testClass(javaClass()) { + model("repl", extension = "repl") + } + testClass(javaClass()) { model("cfg") }