Generated codegen tests for ranges.

This commit is contained in:
Evgeny Gerashchenko
2013-01-30 18:46:09 +04:00
parent 21c37951d6
commit 57e985b7f1
37 changed files with 2504 additions and 0 deletions
@@ -0,0 +1,148 @@
/*
* 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.generators.tests;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.LineSeparator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GenerateRangesCodegenTestData {
private static final File TEST_DATA_DIR = new File("compiler/testData/codegen/ranges");
private static final File AS_LITERAL_DIR = new File(TEST_DATA_DIR, "literal");
private static final File AS_EXPRESSION_DIR = new File(TEST_DATA_DIR, "expression");
private static final File SOURCE_TEST_FILE = new File("libraries/stdlib/test/language/RangeIterationTest.kt");
private static final Pattern TEST_FUN_PATTERN = Pattern.compile("test fun (\\w+)\\(\\) \\{.+?}", Pattern.DOTALL);
private static final Pattern SUBTEST_INVOCATION_PATTERN = Pattern.compile("doTest\\(([^,]+), [^,]+, [^,]+, [^,]+,\\s+listOf[\\w<>]*\\(([^\\n]*)\\)\\)", Pattern.DOTALL);
public static final String LITERAL_TEMPLATE = " val $LIST = ArrayList<$TYPE>()\n" +
" for (i in $RANGE_EXPR) {\n" +
" $LIST.add(i)\n" +
" }\n" +
" if ($LIST != listOf<$TYPE>($LIST_ELEMENTS)) {\n" +
" return \"Wrong elements for $RANGE_EXPR: $$LIST\"\n" +
" }\n" +
"\n";
public static final String EXPRESSION_TEMPLATE = " val $LIST = ArrayList<$TYPE>()\n" +
" val $RANGE = $RANGE_EXPR\n" +
" for (i in $RANGE) {\n" +
" $LIST.add(i)\n" +
" }\n" +
" if ($LIST != listOf<$TYPE>($LIST_ELEMENTS)) {\n" +
" return \"Wrong elements for $RANGE_EXPR: $$LIST\"\n" +
" }\n" +
"\n";
private static String detectElementType(String rangeExpression) {
Matcher matcher = Pattern.compile("\\.to(\\w+)").matcher(rangeExpression);
if (matcher.find()) {
return matcher.group(1);
}
if (rangeExpression.contains("'")) {
return "Char";
}
if (Pattern.compile("\\d\\.\\d").matcher(rangeExpression).find()) {
return "Double";
}
return "Int";
}
private static String renderTemplate(String template, int number, String elementType, String rangeExpression, String expectedListElements) {
return template
.replace("$RANGE_EXPR", rangeExpression)
.replace("$LIST_ELEMENTS", expectedListElements)
.replace("$LIST", "list" + number)
.replace("$RANGE", "range" + number)
.replace("$TYPE", elementType)
.replace("\n", LineSeparator.getSystemLineSeparator().getSeparatorString());
}
private static void writeToFile(File file, CharSequence generatedBody) {
PrintWriter out;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
out = new PrintWriter(file);
}
catch (FileNotFoundException e) {
throw new AssertionError(e);
}
out.println("// Auto-generated by " + GenerateRangesCodegenTestData.class.getName() + ". DO NOT EDIT!");
out.println("import java.util.ArrayList");
out.println();
out.println("fun box(): String {");
out.print(generatedBody);
out.println(" return \"OK\"");
out.println("}");
out.close();
System.out.println("Written to " + file);
}
public static void main(String[] args) {
try {
FileUtil.delete(TEST_DATA_DIR);
//noinspection ResultOfMethodCallIgnored
AS_LITERAL_DIR.mkdirs();
//noinspection ResultOfMethodCallIgnored
AS_EXPRESSION_DIR.mkdirs();
String sourceContent = FileUtil.loadFile(SOURCE_TEST_FILE);
Matcher testFunMatcher = TEST_FUN_PATTERN.matcher(sourceContent);
while (testFunMatcher.find()) {
String testFunName = testFunMatcher.group(1);
if (testFunName.equals("emptyConstant")) {
continue;
}
String testFunText = testFunMatcher.group();
StringBuilder asLiteralBody = new StringBuilder();
StringBuilder asExpressionBody = new StringBuilder();
int index = 0;
Matcher matcher = SUBTEST_INVOCATION_PATTERN.matcher(testFunText);
while (matcher.find()) {
index++;
String rangeExpression = matcher.group(1);
String expectedListElements = matcher.group(2);
String elementType = detectElementType(rangeExpression);
asLiteralBody.append(renderTemplate(LITERAL_TEMPLATE, index, elementType, rangeExpression, expectedListElements));
asExpressionBody.append(renderTemplate(EXPRESSION_TEMPLATE, index, elementType, rangeExpression, expectedListElements));
}
String fileName = testFunName + ".kt";
writeToFile(new File(AS_LITERAL_DIR, fileName), asLiteralBody);
writeToFile(new File(AS_EXPRESSION_DIR, fileName), asExpressionBody);
}
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
private GenerateRangesCodegenTestData() {
}
}
@@ -25,6 +25,7 @@ import org.jetbrains.jet.codegen.AbstractDataClassCodegenTest;
import org.jetbrains.jet.codegen.defaultConstructor.AbstractDefaultConstructorCodegenTest;
import org.jetbrains.jet.codegen.flags.AbstractWriteFlagsTest;
import org.jetbrains.jet.codegen.generated.AbstractBlackBoxCodegenTest;
import org.jetbrains.jet.codegen.generated.AbstractRangesCodegenTest;
import org.jetbrains.jet.jvm.compiler.*;
import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveDescriptorRendererTest;
import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparingTest;
@@ -70,6 +71,15 @@ public class GenerateTests {
testModel("compiler/testData/codegen/box", "blackBoxFileByFullPath")
);
GenerateRangesCodegenTestData.main(args);
generateTest(
"compiler/tests/",
"RangesCodegenTestGenerated",
AbstractRangesCodegenTest.class,
testModel("compiler/testData/codegen/ranges", "blackBoxFileByFullPath")
);
generateTest(
"compiler/tests/",
"BlackBoxWithJavaCodegenTestGenerated",