Make it possible to generate getTestsRoot() method with the test directory

Method can be used in setUp()
This commit is contained in:
Nikolay Krasko
2015-11-03 22:08:21 +03:00
committed by Nikolay Krasko
parent 178aba54dd
commit 7be9699263
11 changed files with 136 additions and 111 deletions
@@ -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,
@@ -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)
}
)
}
@@ -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<TestMethodModel> getTestMethods() {
return delegate.getTestMethods();
public Collection<MethodModel> getMethods() {
return delegate.getMethods();
}
@Override
@@ -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<TestClassModel> innerTestClasses;
@Nullable
private Collection<TestMethodModel> testMethods;
private Collection<MethodModel> 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<String> excludeDirs
@NotNull Collection<String> 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.<String>emptySet() : new LinkedHashSet<String>(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<TestMethodModel> getTestMethods() {
public Collection<MethodModel> getMethods() {
if (testMethods == null) {
if (!rootFile.isDirectory()) {
testMethods = Collections.<TestMethodModel>singletonList(new SimpleTestMethodModel(rootFile, rootFile, doTestMethodName,
testMethods = Collections.<MethodModel>singletonList(new SimpleTestMethodModel(rootFile, rootFile, doTestMethodName,
filenamePattern, checkFilenameStartsLowerCase,
targetBackend));
}
else {
List<TestMethodModel> result = Lists.newArrayList();
List<MethodModel> 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);
}
}
}
@@ -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);
}
}
@@ -49,7 +49,7 @@ public class SingleClassTestModel implements TestClassModel {
@NotNull
private final TargetBackend targetBackend;
@Nullable
private Collection<TestMethodModel> testMethods;
private Collection<MethodModel> methods;
public SingleClassTestModel(
@NotNull File rootFile,
@@ -75,8 +75,8 @@ public class SingleClassTestModel implements TestClassModel {
@NotNull
@Override
public Collection<TestMethodModel> getTestMethods() {
if (testMethods == null) {
public Collection<MethodModel> getMethods() {
if (methods == null) {
final List<TestMethodModel> result = Lists.newArrayList();
result.add(new TestAllFilesPresentMethodModel());
@@ -99,10 +99,10 @@ public class SingleClassTestModel implements TestClassModel {
}
});
testMethods = result;
methods = Lists.<MethodModel>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);
}
}
}
@@ -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<TestClassModel> getInnerTestClasses();
@NotNull
Collection<TestMethodModel> getTestMethods();
boolean isEmpty();
@Nullable
String getDataPathRoot();
}
@@ -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();
}
@@ -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<TestMethodModel> getTestMethods() {
public Collection<MethodModel> 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<TestMethodModel> testMethods = testClassModel.getTestMethods();
Collection<MethodModel> testMethods = testClassModel.getMethods();
Collection<TestClassModel> innerTestClasses = testClassModel.getInnerTestClasses();
for (Iterator<TestMethodModel> iterator = testMethods.iterator(); iterator.hasNext(); ) {
TestMethodModel testMethodModel = iterator.next();
generateTestMethod(p, testMethodModel);
for (Iterator<MethodModel> 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("}");
@@ -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);
}
@@ -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<TestClassModel>
val methods: Collection<MethodModel>
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")
}
}