diff --git a/compiler/testData/codegen/script/withdefimports.def.kt b/compiler/testData/codegen/script/withdefimports.def.kt deleted file mode 100644 index 9f2cac3c405..00000000000 --- a/compiler/testData/codegen/script/withdefimports.def.kt +++ /dev/null @@ -1 +0,0 @@ -Collections.emptyList() \ No newline at end of file diff --git a/compiler/testData/codegen/script/fib.lang.kt b/compiler/testData/codegen/scriptCustom/fib.lang.kt similarity index 100% rename from compiler/testData/codegen/script/fib.lang.kt rename to compiler/testData/codegen/scriptCustom/fib.lang.kt diff --git a/compiler/testData/codegen/script/fibwp.lang.kt b/compiler/testData/codegen/scriptCustom/fibwp.lang.kt similarity index 100% rename from compiler/testData/codegen/script/fibwp.lang.kt rename to compiler/testData/codegen/scriptCustom/fibwp.lang.kt diff --git a/compiler/testData/codegen/script/fibwprunner.kts b/compiler/testData/codegen/scriptCustom/fibwprunner.kts similarity index 100% rename from compiler/testData/codegen/script/fibwprunner.kts rename to compiler/testData/codegen/scriptCustom/fibwprunner.kts diff --git a/compiler/testData/codegen/script/methodWithClosure.lang.kt b/compiler/testData/codegen/scriptCustom/methodWithClosure.lang.kt similarity index 100% rename from compiler/testData/codegen/script/methodWithClosure.lang.kt rename to compiler/testData/codegen/scriptCustom/methodWithClosure.lang.kt diff --git a/compiler/tests/org/jetbrains/jet/codegen/AbstractScriptCodegenTest.java b/compiler/tests/org/jetbrains/jet/codegen/AbstractScriptCodegenTest.java new file mode 100644 index 00000000000..ef4a995af4b --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/codegen/AbstractScriptCodegenTest.java @@ -0,0 +1,83 @@ +/* + * 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.codegen; + +import com.intellij.openapi.util.Pair; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.ConfigurationKind; +import org.jetbrains.jet.lang.resolve.ScriptNameUtil; +import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.utils.UtilsPackage; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; + +public abstract class AbstractScriptCodegenTest extends CodegenTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); + } + + protected void doTest(@NotNull String filename) { + loadFileByFullPath(filename); + + try { + FqName fqName = ScriptNameUtil.classNameForScript(myFiles.getPsiFile().getScript()); + Class scriptClass = generateClass(fqName.asString()); + + Constructor constructor = getTheOnlyConstructor(scriptClass); + Object scriptInstance = constructor.newInstance(myFiles.getScriptParameterValues().toArray()); + + assertFalse("expecting at least one expectation", myFiles.getExpectedValues().isEmpty()); + + for (Pair nameValue : myFiles.getExpectedValues()) { + String fieldName = nameValue.first; + String expectedValue = nameValue.second; + + if (expectedValue.equals("")) { + try { + scriptClass.getDeclaredField(fieldName); + fail("must have no field " + fieldName); + } + catch (NoSuchFieldException e) { + continue; + } + } + + Field field = scriptClass.getDeclaredField(fieldName); + field.setAccessible(true); + Object result = field.get(scriptInstance); + String resultString = result != null ? result.toString() : "null"; + assertEquals("comparing field " + fieldName, expectedValue, resultString); + } + } + catch (Throwable e) { + System.out.println(generateToText()); + throw UtilsPackage.rethrow(e); + } + } + + @NotNull + private static Constructor getTheOnlyConstructor(@NotNull Class clazz) { + Constructor[] constructors = clazz.getConstructors(); + if (constructors.length != 1) { + throw new IllegalArgumentException("Script class should have one constructor: " + clazz); + } + return constructors[0]; + } +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/ScriptCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/ScriptCodegenTestGenerated.java new file mode 100644 index 00000000000..ffe0c7d1806 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/codegen/ScriptCodegenTestGenerated.java @@ -0,0 +1,104 @@ +/* + * 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.codegen; + +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.codegen.AbstractScriptCodegenTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("compiler/testData/codegen/script") +public class ScriptCodegenTestGenerated extends AbstractScriptCodegenTest { + public void testAllFilesPresentInScript() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/script"), Pattern.compile("^(.+)\\.kts$"), true); + } + + @TestMetadata("empty.kts") + public void testEmpty() throws Exception { + doTest("compiler/testData/codegen/script/empty.kts"); + } + + @TestMetadata("helloWorld.kts") + public void testHelloWorld() throws Exception { + doTest("compiler/testData/codegen/script/helloWorld.kts"); + } + + @TestMetadata("parameter.kts") + public void testParameter() throws Exception { + doTest("compiler/testData/codegen/script/parameter.kts"); + } + + @TestMetadata("parameterArray.kts") + public void testParameterArray() throws Exception { + doTest("compiler/testData/codegen/script/parameterArray.kts"); + } + + @TestMetadata("parameterClosure.kts") + public void testParameterClosure() throws Exception { + doTest("compiler/testData/codegen/script/parameterClosure.kts"); + } + + @TestMetadata("parameterLong.kts") + public void testParameterLong() throws Exception { + doTest("compiler/testData/codegen/script/parameterLong.kts"); + } + + @TestMetadata("secondLevelFunction.kts") + public void testSecondLevelFunction() throws Exception { + doTest("compiler/testData/codegen/script/secondLevelFunction.kts"); + } + + @TestMetadata("secondLevelFunctionClosure.kts") + public void testSecondLevelFunctionClosure() throws Exception { + doTest("compiler/testData/codegen/script/secondLevelFunctionClosure.kts"); + } + + @TestMetadata("secondLevelVal.kts") + public void testSecondLevelVal() throws Exception { + doTest("compiler/testData/codegen/script/secondLevelVal.kts"); + } + + @TestMetadata("string.kts") + public void testString() throws Exception { + doTest("compiler/testData/codegen/script/string.kts"); + } + + @TestMetadata("topLevelFunction.kts") + public void testTopLevelFunction() throws Exception { + doTest("compiler/testData/codegen/script/topLevelFunction.kts"); + } + + @TestMetadata("topLevelFunctionClosure.kts") + public void testTopLevelFunctionClosure() throws Exception { + doTest("compiler/testData/codegen/script/topLevelFunctionClosure.kts"); + } + + @TestMetadata("topLevelProperty.kts") + public void testTopLevelProperty() throws Exception { + doTest("compiler/testData/codegen/script/topLevelProperty.kts"); + } + +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/ScriptGenTest.java b/compiler/tests/org/jetbrains/jet/codegen/ScriptGenTest.java index a5c383a941a..e1701f01d20 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ScriptGenTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ScriptGenTest.java @@ -16,17 +16,12 @@ package org.jetbrains.jet.codegen; -import com.intellij.openapi.util.Pair; -import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.lang.parsing.JetScriptDefinition; import org.jetbrains.jet.lang.parsing.JetScriptDefinitionProvider; import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter; -import org.jetbrains.jet.lang.resolve.ScriptNameUtil; -import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; -import org.jetbrains.jet.utils.UtilsPackage; import org.jetbrains.org.objectweb.asm.Opcodes; import java.lang.reflect.Constructor; @@ -34,9 +29,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; public class ScriptGenTest extends CodegenTestCase { - protected Object scriptInstance; - - public static final JetScriptDefinition FIB_SCRIPT_DEFINITION = + private static final JetScriptDefinition FIB_SCRIPT_DEFINITION = new JetScriptDefinition(".lang.kt", new AnalyzerScriptParameter(Name.identifier("num"), KotlinBuiltIns.getInstance().getIntType())); @@ -44,184 +37,53 @@ public class ScriptGenTest extends CodegenTestCase { protected void setUp() throws Exception { super.setUp(); createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); - } - - @Override - protected void tearDown() throws Exception { - scriptInstance = null; - super.tearDown(); - } - - private void blackBoxScript(String filename) { - loadFile(filename); - - try { - FqName fqName = ScriptNameUtil.classNameForScript(myFiles.getPsiFile().getScript()); - Class scriptClass = generateClass(fqName.asString()); - - Constructor constructor = getConstructor(scriptClass); - scriptInstance = constructor.newInstance(myFiles.getScriptParameterValues().toArray()); - - assertFalse("expecting at least one expectation", myFiles.getExpectedValues().isEmpty()); - - for (Pair nameValue : myFiles.getExpectedValues()) { - String fieldName = nameValue.first; - String expectedValue = nameValue.second; - - if (expectedValue.equals("")) { - try { - scriptClass.getDeclaredField(fieldName); - fail("must have no field " + fieldName); - } catch (NoSuchFieldException e) { - continue; - } - } - - Field field = scriptClass.getDeclaredField(fieldName); - field.setAccessible(true); - Object result = field.get(scriptInstance); - String resultString = result != null ? result.toString() : "null"; - assertEquals("comparing field " + fieldName, expectedValue, resultString); - } - } - catch (Throwable e) { - System.out.println(generateToText()); - throw UtilsPackage.rethrow(e); - } - } - - @NotNull - protected static Constructor getConstructor(@NotNull Class clazz) { - Constructor[] constructors = clazz.getConstructors(); - if (constructors.length != 1) { - throw new IllegalArgumentException("Script class should have one constructor: " + clazz); - } - return constructors[0]; - } - - public void testHelloWorld() { - blackBoxScript("script/helloWorld.kts"); - } - - public void testString() { - blackBoxScript("script/string.kts"); - } - - public void testTopLevelFunction() throws Exception { - blackBoxScript("script/topLevelFunction.kts"); - Method method = scriptInstance.getClass().getMethod("factorial", int.class); - Object r = method.invoke(scriptInstance, 4); - assertEquals(24, r); - } - - public void testTopLevelFunctionClosure() { - blackBoxScript("script/topLevelFunctionClosure.kts"); - } - - public void testSecondLevelFunction() { - blackBoxScript("script/secondLevelFunction.kts"); - } - - public void testSecondLevelFunctionClosure() { - blackBoxScript("script/secondLevelFunctionClosure.kts"); - } - - public void testSecondLevelVal() { - blackBoxScript("script/secondLevelVal.kts"); - } - - public void testTopLevelProperty() { - blackBoxScript("script/topLevelProperty.kts"); - } - - public void testScriptParameter() { - blackBoxScript("script/parameter.kts"); - } - - public void testScriptParameterLong() { - blackBoxScript("script/parameterLong.kts"); - } - - public void testScriptParameterArray() { - blackBoxScript("script/parameterArray.kts"); - } - - public void testScriptParameterClosure() { - blackBoxScript("script/parameterClosure.kts"); - } - - public void testEmpty() { - blackBoxScript("script/empty.kts"); - } - - public void testLanguage() { JetScriptDefinitionProvider.getInstance(myEnvironment.getProject()).addScriptDefinition(FIB_SCRIPT_DEFINITION); - loadFile("script/fib.lang.kt"); + } + + public void testLanguage() throws Exception { + loadFile("scriptCustom/fib.lang.kt"); Class aClass = generateClass("Fib"); - try { - Constructor constructor = aClass.getConstructor(int.class); - Field result = aClass.getDeclaredField("result"); - result.setAccessible(true); - Object script = constructor.newInstance(5); - assertEquals(8,result.get(script)); - } - catch (Exception e) { - throw new RuntimeException(e); - } + Constructor constructor = aClass.getConstructor(int.class); + Field result = aClass.getDeclaredField("result"); + result.setAccessible(true); + Object script = constructor.newInstance(5); + assertEquals(8, result.get(script)); } - public void testLanguageWithPackage() { - JetScriptDefinitionProvider.getInstance(myEnvironment.getProject()).addScriptDefinition(FIB_SCRIPT_DEFINITION); - loadFile("script/fibwp.lang.kt"); + public void testLanguageWithPackage() throws Exception { + loadFile("scriptCustom/fibwp.lang.kt"); Class aClass = generateClass("test.Fibwp"); - try { - Constructor constructor = aClass.getConstructor(int.class); - Field result = aClass.getDeclaredField("result"); - result.setAccessible(true); - Object script = constructor.newInstance(5); - assertEquals(8,result.get(script)); - } - catch (Exception e) { - throw new RuntimeException(e); - } + Constructor constructor = aClass.getConstructor(int.class); + Field result = aClass.getDeclaredField("result"); + result.setAccessible(true); + Object script = constructor.newInstance(5); + assertEquals(8, result.get(script)); } - public void testDependentScripts() { - JetScriptDefinitionProvider.getInstance(myEnvironment.getProject()).addScriptDefinition(FIB_SCRIPT_DEFINITION); - loadFiles("script/fibwp.lang.kt", "script/fibwprunner.kts"); + public void testDependentScripts() throws Exception { + loadFiles("scriptCustom/fibwp.lang.kt", "scriptCustom/fibwprunner.kts"); Class aClass = generateClass("Fibwprunner"); - try { - Constructor constructor = aClass.getConstructor(); - Field result = aClass.getDeclaredField("result"); - result.setAccessible(true); - Method resultMethod = aClass.getDeclaredMethod("getResult"); - assertTrue((resultMethod.getModifiers() & Opcodes.ACC_FINAL) != 0); - assertTrue((resultMethod.getModifiers() & Opcodes.ACC_PUBLIC) != 0); - Field rv = aClass.getField("rv"); - assertTrue((result.getModifiers() & Opcodes.ACC_PRIVATE) != 0); - Object script = constructor.newInstance(); - assertEquals(12,rv.get(script)); - assertEquals(8,result.get(script)); - assertEquals(8,resultMethod.invoke(script)); - } - catch (Exception e) { - throw new RuntimeException(e); - } + Constructor constructor = aClass.getConstructor(); + Field result = aClass.getDeclaredField("result"); + result.setAccessible(true); + Method resultMethod = aClass.getDeclaredMethod("getResult"); + assertTrue((resultMethod.getModifiers() & Opcodes.ACC_FINAL) != 0); + assertTrue((resultMethod.getModifiers() & Opcodes.ACC_PUBLIC) != 0); + Field rv = aClass.getField("rv"); + assertTrue((result.getModifiers() & Opcodes.ACC_PRIVATE) != 0); + Object script = constructor.newInstance(); + assertEquals(12, rv.get(script)); + assertEquals(8, result.get(script)); + assertEquals(8, resultMethod.invoke(script)); } - public void testScriptWhereMethodHasClosure() { - JetScriptDefinitionProvider.getInstance(myEnvironment.getProject()).addScriptDefinition(FIB_SCRIPT_DEFINITION); - loadFile("script/methodWithClosure.lang.kt"); + public void testScriptWhereMethodHasClosure() throws Exception { + loadFile("scriptCustom/methodWithClosure.lang.kt"); Class aClass = generateClass("MethodWithClosure"); - try { - Constructor constructor = aClass.getConstructor(int.class); - Object script = constructor.newInstance(239); - Method fib = aClass.getMethod("method"); - Object invoke = fib.invoke(script); - assertEquals(239, ((Integer)invoke)/2); - } - catch (Exception e) { - throw new RuntimeException(e); - } + Constructor constructor = aClass.getConstructor(int.class); + Object script = constructor.newInstance(239); + Method fib = aClass.getMethod("method"); + Object invoke = fib.invoke(script); + assertEquals(239, ((Integer) invoke) / 2); } } diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index 16a68411dcf..a568427dd92 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -116,6 +116,7 @@ import org.jetbrains.jet.jps.build.AbstractIncrementalJpsTest import org.jetbrains.jet.asJava.AbstractKotlinLightClassTest import org.jetbrains.jet.lang.resolve.java.AbstractJavaTypeSubstitutorTest import org.jetbrains.jet.plugin.intentions.declarations.AbstractJoinLinesTest +import org.jetbrains.jet.codegen.AbstractScriptCodegenTest fun main(args: Array) { System.setProperty("java.awt.headless", "true") @@ -180,6 +181,10 @@ fun main(args: Array) { model("codegen/boxWithStdlib", testMethod = "doTestWithStdlib") } + testClass(javaClass()) { + model("codegen/script", extension = "kts") + } + testClass(javaClass()) { model("codegen/bytecodeText") }