From 7be9699263ab03190d331f7b2cdd2290ec8140c6 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 3 Nov 2015 22:08:21 +0300 Subject: [PATCH] Make it possible to generate getTestsRoot() method with the test directory Method can be used in setUp() --- .../kotlin/test/KotlinTestUtils.java | 9 ++++ .../kotlin/generators/tests/GenerateTests.kt | 2 +- .../generator/DelegatingTestClassModel.java | 5 +- .../tests/generator/SimpleTestClassModel.java | 54 ++++++++++++++++--- .../generator/SimpleTestMethodModel.java | 6 +++ .../tests/generator/SingleClassTestModel.java | 19 ++++--- .../tests/generator/TestClassModel.java | 36 ------------- .../tests/generator/TestEntityModel.java | 26 --------- .../tests/generator/TestGenerator.java | 24 +++++---- .../tests/generator/TestMethodModel.java | 24 --------- .../generators/tests/generator/TestModel.kt | 42 +++++++++++++++ 11 files changed, 136 insertions(+), 111 deletions(-) delete mode 100644 generators/src/org/jetbrains/kotlin/generators/tests/generator/TestClassModel.java delete mode 100644 generators/src/org/jetbrains/kotlin/generators/tests/generator/TestEntityModel.java delete mode 100644 generators/src/org/jetbrains/kotlin/generators/tests/generator/TestMethodModel.java create mode 100644 generators/src/org/jetbrains/kotlin/generators/tests/generator/TestModel.kt diff --git a/compiler/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java index 4150b2d5283..72d87d852c5 100644 --- a/compiler/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -794,6 +794,15 @@ public class KotlinTestUtils { return testFile; } + public static String getTestsRoot(@NotNull TestCase testCase) { + try { + return (String) testCase.getClass().getMethod("getTestsRoot").invoke(testCase); + } + catch (Exception e) { + throw new RuntimeException("Can't call getTestsRoot() on test class", e); + } + } + public static void assertAllTestsPresentByMetadata( @NotNull Class testCaseClass, @NotNull File testDataDir, diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 94b4c2f4856..2e138321e47 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -1088,7 +1088,7 @@ private class TestGroup(val testsRoot: String, val testDataRoot: String) { } else { SimpleTestClassModel(rootFile, recursive, excludeParentDirs, - compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend, excludeDirs) + compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend, excludeDirs, false) } ) } diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/DelegatingTestClassModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/DelegatingTestClassModel.java index a3c8b231de7..f5de7c4cc67 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/DelegatingTestClassModel.java +++ b/generators/src/org/jetbrains/kotlin/generators/tests/generator/DelegatingTestClassModel.java @@ -28,6 +28,7 @@ public class DelegatingTestClassModel implements TestClassModel { this.delegate = delegate; } + @NotNull @Override public String getName() { return delegate.getName(); @@ -41,8 +42,8 @@ public class DelegatingTestClassModel implements TestClassModel { @NotNull @Override - public Collection getTestMethods() { - return delegate.getTestMethods(); + public Collection getMethods() { + return delegate.getMethods(); } @Override diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java index 366961cd634..8ba68e7dcab 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java +++ b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java @@ -46,6 +46,7 @@ public class SimpleTestClassModel implements TestClassModel { private final Pattern filenamePattern; @Nullable private final Boolean checkFilenameStartsLowerCase; + private final boolean withTestRootMethod; @NotNull private final String doTestMethodName; @NotNull @@ -57,7 +58,7 @@ public class SimpleTestClassModel implements TestClassModel { @Nullable private Collection innerTestClasses; @Nullable - private Collection testMethods; + private Collection testMethods; public SimpleTestClassModel( @NotNull File rootFile, @@ -68,7 +69,8 @@ public class SimpleTestClassModel implements TestClassModel { @NotNull String doTestMethodName, @NotNull String testClassName, @NotNull TargetBackend targetBackend, - @NotNull Collection excludeDirs + @NotNull Collection excludeDirs, + boolean withTestRootMethod ) { this.rootFile = rootFile; this.recursive = recursive; @@ -78,6 +80,7 @@ public class SimpleTestClassModel implements TestClassModel { this.testClassName = testClassName; this.targetBackend = targetBackend; this.checkFilenameStartsLowerCase = checkFilenameStartsLowerCase; + this.withTestRootMethod = withTestRootMethod; this.excludeDirs = excludeDirs.isEmpty() ? Collections.emptySet() : new LinkedHashSet(excludeDirs); } @@ -97,7 +100,8 @@ public class SimpleTestClassModel implements TestClassModel { String innerTestClassName = TestGeneratorUtil.fileNameToJavaIdentifier(file); children.add(new SimpleTestClassModel( file, true, excludeParentDirs, filenamePattern, checkFilenameStartsLowerCase, - doTestMethodName, innerTestClassName, targetBackend, excludesStripOneDirectory(file.getName())) + doTestMethodName, innerTestClassName, targetBackend, excludesStripOneDirectory(file.getName()), + withTestRootMethod) ); } } @@ -147,15 +151,15 @@ public class SimpleTestClassModel implements TestClassModel { @NotNull @Override - public Collection getTestMethods() { + public Collection getMethods() { if (testMethods == null) { if (!rootFile.isDirectory()) { - testMethods = Collections.singletonList(new SimpleTestMethodModel(rootFile, rootFile, doTestMethodName, + testMethods = Collections.singletonList(new SimpleTestMethodModel(rootFile, rootFile, doTestMethodName, filenamePattern, checkFilenameStartsLowerCase, targetBackend)); } else { - List result = Lists.newArrayList(); + List result = Lists.newArrayList(); result.add(new TestAllFilesPresentMethodModel()); @@ -174,6 +178,10 @@ public class SimpleTestClassModel implements TestClassModel { } Collections.sort(result, BY_NAME); + if (withTestRootMethod) { + result.add(new TestRootsMethodModel()); + } + testMethods = result; } } @@ -182,7 +190,8 @@ public class SimpleTestClassModel implements TestClassModel { @Override public boolean isEmpty() { - return getTestMethods().size() == 1 && getInnerTestClasses().isEmpty(); + boolean noTestMethods = withTestRootMethod ? getMethods().size() == 2 : getMethods().size() == 1; + return noTestMethods && getInnerTestClasses().isEmpty(); } @Override @@ -196,12 +205,38 @@ public class SimpleTestClassModel implements TestClassModel { return "$PROJECT_ROOT"; } + @NotNull @Override public String getName() { return testClassName; } + private class TestRootsMethodModel implements MethodModel { + @NotNull + @Override + public String getName() { + return "getTestsRoot"; + } + + @Override + public void generateBody(@NotNull Printer p) { + p.println(String.format("return \"%s\";", KotlinTestUtils.getFilePath(rootFile))); + } + + @Override + public void generateSignature(@NotNull Printer p) { + p.print(String.format("public String %s()", getName())); + } + + @Nullable + @Override + public String getDataString() { + return null; + } + } + private class TestAllFilesPresentMethodModel implements TestMethodModel { + @NotNull @Override public String getName() { return "testAllFilesPresentIn" + testClassName; @@ -226,5 +261,10 @@ public class SimpleTestClassModel implements TestClassModel { public String getDataString() { return null; } + + @Override + public void generateSignature(@NotNull Printer p) { + TestMethodModel.DefaultImpls.generateSignature(this, p); + } } } diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestMethodModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestMethodModel.java index 4781441cfb0..7f274e08673 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestMethodModel.java +++ b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SimpleTestMethodModel.java @@ -104,6 +104,7 @@ public class SimpleTestMethodModel implements TestMethodModel { } } + @NotNull @Override public String getName() { Matcher matcher = filenamePattern.matcher(file.getName()); @@ -123,4 +124,9 @@ public class SimpleTestMethodModel implements TestMethodModel { } return (isIgnored() ? "ignored" : "test") + StringUtil.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(unescapedName)); } + + @Override + public void generateSignature(@NotNull Printer p) { + TestMethodModel.DefaultImpls.generateSignature(this, p); + } } diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java index f16b130c55a..54909af3d7d 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java +++ b/generators/src/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java @@ -49,7 +49,7 @@ public class SingleClassTestModel implements TestClassModel { @NotNull private final TargetBackend targetBackend; @Nullable - private Collection testMethods; + private Collection methods; public SingleClassTestModel( @NotNull File rootFile, @@ -75,8 +75,8 @@ public class SingleClassTestModel implements TestClassModel { @NotNull @Override - public Collection getTestMethods() { - if (testMethods == null) { + public Collection getMethods() { + if (methods == null) { final List result = Lists.newArrayList(); result.add(new TestAllFilesPresentMethodModel()); @@ -99,10 +99,10 @@ public class SingleClassTestModel implements TestClassModel { } }); - testMethods = result; + methods = Lists.newArrayList(result); } - return testMethods; + return methods; } @NotNull @@ -115,7 +115,7 @@ public class SingleClassTestModel implements TestClassModel { @Override public boolean isEmpty() { // There's always one test for checking if all tests are present - return getTestMethods().size() <= 1; + return getMethods().size() <= 1; } @Override @@ -129,12 +129,14 @@ public class SingleClassTestModel implements TestClassModel { return "$PROJECT_ROOT"; } + @NotNull @Override public String getName() { return testClassName; } private class TestAllFilesPresentMethodModel implements TestMethodModel { + @NotNull @Override public String getName() { return "testAllFilesPresentIn" + testClassName; @@ -153,5 +155,10 @@ public class SingleClassTestModel implements TestClassModel { public String getDataString() { return null; } + + @Override + public void generateSignature(@NotNull Printer p) { + TestMethodModel.DefaultImpls.generateSignature(this, p); + } } } diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestClassModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestClassModel.java deleted file mode 100644 index def1cc64b16..00000000000 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestClassModel.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2010-2015 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.kotlin.generators.tests.generator; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Collection; - -public interface TestClassModel extends TestEntityModel { - - @NotNull - Collection getInnerTestClasses(); - - @NotNull - Collection getTestMethods(); - - boolean isEmpty(); - - @Nullable - String getDataPathRoot(); -} diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestEntityModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestEntityModel.java deleted file mode 100644 index 4f2e2fee231..00000000000 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestEntityModel.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2010-2015 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.kotlin.generators.tests.generator; - -import org.jetbrains.annotations.Nullable; - -public interface TestEntityModel { - String getName(); - - @Nullable - String getDataString(); -} diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestGenerator.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestGenerator.java index 6847c535a2d..f468ceee270 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestGenerator.java +++ b/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestGenerator.java @@ -102,6 +102,7 @@ public class TestGenerator { TestClassModel model; if (testClassModels.size() == 1) { model = new DelegatingTestClassModel(single(testClassModels)) { + @NotNull @Override public String getName() { return suiteClassName; @@ -118,7 +119,7 @@ public class TestGenerator { @NotNull @Override - public Collection getTestMethods() { + public Collection getMethods() { return Collections.emptyList(); } @@ -127,6 +128,7 @@ public class TestGenerator { return false; } + @NotNull @Override public String getName() { return suiteClassName; @@ -161,12 +163,12 @@ public class TestGenerator { p.println("public " + staticModifier + "class ", testClassModel.getName(), " extends ", baseTestClassName, " {"); p.pushIndent(); - Collection testMethods = testClassModel.getTestMethods(); + Collection testMethods = testClassModel.getMethods(); Collection innerTestClasses = testClassModel.getInnerTestClasses(); - for (Iterator iterator = testMethods.iterator(); iterator.hasNext(); ) { - TestMethodModel testMethodModel = iterator.next(); - generateTestMethod(p, testMethodModel); + for (Iterator iterator = testMethods.iterator(); iterator.hasNext(); ) { + MethodModel methodModel = iterator.next(); + generateTestMethod(p, methodModel); if (iterator.hasNext() || !innerTestClasses.isEmpty()) { p.println(); } @@ -186,12 +188,16 @@ public class TestGenerator { p.println("}"); } - private static void generateTestMethod(Printer p, TestMethodModel testMethodModel) { - generateMetadata(p, testMethodModel); - p.println("public void ", testMethodModel.getName(), "() throws Exception {"); + private static void generateTestMethod(Printer p, MethodModel methodModel) { + generateMetadata(p, methodModel); + + methodModel.generateSignature(p); + p.printWithNoIndent(" {"); + p.println(); + p.pushIndent(); - testMethodModel.generateBody(p); + methodModel.generateBody(p); p.popIndent(); p.println("}"); diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestMethodModel.java b/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestMethodModel.java deleted file mode 100644 index 95ad96a53f2..00000000000 --- a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestMethodModel.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2010-2015 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.kotlin.generators.tests.generator; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.utils.Printer; - -public interface TestMethodModel extends TestEntityModel { - void generateBody(@NotNull Printer p); -} diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestModel.kt b/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestModel.kt new file mode 100644 index 00000000000..9058baff036 --- /dev/null +++ b/generators/src/org/jetbrains/kotlin/generators/tests/generator/TestModel.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2015 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.kotlin.generators.tests.generator + +import org.jetbrains.kotlin.utils.Printer + +interface TestEntityModel { + val name: String + val dataString: String? +} + +interface TestClassModel : TestEntityModel { + val innerTestClasses: Collection + val methods: Collection + val isEmpty: Boolean + val dataPathRoot: String? +} + +interface MethodModel : TestEntityModel { + fun generateSignature(p: Printer) + fun generateBody(p: Printer) +} + +interface TestMethodModel : MethodModel { + override fun generateSignature(p: Printer) { + p.print("public void $name() throws Exception") + } +}