Support modules in multi-file tests
This commit is contained in:
@@ -97,7 +97,17 @@ public class JetTestUtils {
|
||||
private static final Pattern KT_FILES = Pattern.compile(".*?.kt");
|
||||
private static final List<File> filesToDelete = new ArrayList<File>();
|
||||
|
||||
public static final Pattern FILE_PATTERN = Pattern.compile("//\\s*FILE:\\s*(.*)$", Pattern.MULTILINE);
|
||||
/**
|
||||
* Syntax:
|
||||
*
|
||||
* // MODULE: name(dependency1, dependency2, ...)
|
||||
*
|
||||
* // FILE: name
|
||||
*
|
||||
* Several files may follow one module
|
||||
*/
|
||||
public static final Pattern FILE_OR_MODULE_PATTERN = Pattern.compile("(?://\\s*MODULE:\\s*(\\w+)(\\(\\w+(?:, \\w+)*\\))?\\s*)?" +
|
||||
"//\\s*FILE:\\s*(.*)$", Pattern.MULTILINE);
|
||||
public static final Pattern DIRECTIVE_PATTERN = Pattern.compile("^//\\s*!(\\w+)(:\\s*(.*)$)?", Pattern.MULTILINE);
|
||||
|
||||
public static final BindingTrace DUMMY_TRACE = new BindingTrace() {
|
||||
@@ -474,26 +484,47 @@ public class JetTestUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public interface TestFileFactory<F> {
|
||||
F create(String fileName, String text, Map<String, String> directives);
|
||||
public interface TestFileFactory<M, F> {
|
||||
F createFile(@Nullable M module, String fileName, String text, Map<String, String> directives);
|
||||
M createModule(String name, List<String> dependencies);
|
||||
}
|
||||
|
||||
public static <F> List<F> createTestFiles(String testFileName, String expectedText, TestFileFactory<F> factory) {
|
||||
public static abstract class TestFileFactoryNoModules<F> implements TestFileFactory<Void,F> {
|
||||
@Override
|
||||
public final F createFile(@Nullable Void module, String fileName, String text, Map<String, String> directives) {
|
||||
return create(fileName, text, directives);
|
||||
}
|
||||
|
||||
public abstract F create(String fileName, String text, Map<String, String> directives);
|
||||
|
||||
@Override
|
||||
public Void createModule(String name, List<String> dependencies) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <M, F> List<F> createTestFiles(String testFileName, String expectedText, TestFileFactory<M, F> factory) {
|
||||
Map<String, String> directives = parseDirectives(expectedText);
|
||||
|
||||
List<F> testFiles = Lists.newArrayList();
|
||||
Matcher matcher = FILE_PATTERN.matcher(expectedText);
|
||||
Matcher matcher = FILE_OR_MODULE_PATTERN.matcher(expectedText);
|
||||
if (!matcher.find()) {
|
||||
// One file
|
||||
testFiles.add(factory.create(testFileName, expectedText, directives));
|
||||
testFiles.add(factory.createFile(null, testFileName, expectedText, directives));
|
||||
}
|
||||
else {
|
||||
int processedChars = 0;
|
||||
M module = null;
|
||||
// Many files
|
||||
while (true) {
|
||||
String fileName = matcher.group(1);
|
||||
int start = matcher.start();
|
||||
assert start == processedChars : "Characters skipped from " + processedChars + " to " + matcher.start();
|
||||
String moduleName = matcher.group(1);
|
||||
String moduleDependencies = matcher.group(2);
|
||||
if (moduleName != null) {
|
||||
module = factory.createModule(moduleName, parseDependencies(moduleDependencies));
|
||||
}
|
||||
|
||||
String fileName = matcher.group(3);
|
||||
int start = processedChars;
|
||||
|
||||
boolean nextFileExists = matcher.find();
|
||||
int end;
|
||||
@@ -506,7 +537,7 @@ public class JetTestUtils {
|
||||
String fileText = expectedText.substring(start, end);
|
||||
processedChars = end;
|
||||
|
||||
testFiles.add(factory.create(fileName, fileText, directives));
|
||||
testFiles.add(factory.createFile(module, fileName, fileText, directives));
|
||||
|
||||
if (!nextFileExists) break;
|
||||
}
|
||||
@@ -518,6 +549,17 @@ public class JetTestUtils {
|
||||
return testFiles;
|
||||
}
|
||||
|
||||
private static List<String> parseDependencies(@Nullable String dependencies) {
|
||||
if (dependencies == null) return Collections.emptyList();
|
||||
|
||||
Matcher matcher = Pattern.compile("\\w+").matcher(dependencies);
|
||||
List<String> result = new ArrayList<String>();
|
||||
while (matcher.find()) {
|
||||
result.add(matcher.group());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Map<String, String> parseDirectives(String expectedText) {
|
||||
Map<String, String> directives = Maps.newHashMap();
|
||||
@@ -546,7 +588,7 @@ public class JetTestUtils {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
List<String> files = createTestFiles("", content, new TestFileFactory<String>() {
|
||||
List<String> files = createTestFiles("", content, new TestFileFactoryNoModules<String>() {
|
||||
@Override
|
||||
public String create(String fileName, String text, Map<String, String> directives) {
|
||||
int firstLineEnd = text.indexOf('\n');
|
||||
|
||||
@@ -225,7 +225,7 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
|
||||
|
||||
List<File> srcFiles = JetTestUtils.createTestFiles(
|
||||
new File(javaFileName).getName(), FileUtil.loadFile(new File(javaFileName), true),
|
||||
new JetTestUtils.TestFileFactory<File>() {
|
||||
new TestFileFactoryNoModules<File>() {
|
||||
@Override
|
||||
public File create(String fileName, String text, Map<String, String> directives) {
|
||||
File targetFile = new File(srcDir, fileName);
|
||||
|
||||
+1
-1
@@ -60,7 +60,7 @@ public abstract class AbstractLazyResolveRecursiveComparingTest extends KotlinTe
|
||||
private void doTest(String testFileName, boolean checkPrimaryConstructors, boolean checkPropertyAccessors, boolean allowErrorTypes) throws IOException {
|
||||
List<JetFile> files = JetTestUtils
|
||||
.createTestFiles(testFileName, FileUtil.loadFile(new File(testFileName), true),
|
||||
new JetTestUtils.TestFileFactory<JetFile>() {
|
||||
new JetTestUtils.TestFileFactoryNoModules<JetFile>() {
|
||||
@Override
|
||||
public JetFile create(String fileName, String text, Map<String, String> directives) {
|
||||
return JetPsiFactory.createFile(getProject(), fileName, text);
|
||||
|
||||
@@ -65,7 +65,7 @@ public abstract class AbstractLazyResolveTest extends JetLiteFixture {
|
||||
protected void doTest(@NonNls String testFile) throws Exception {
|
||||
String text = FileUtil.loadFile(new File(testFile), true);
|
||||
|
||||
List<JetFile> files = JetTestUtils.createTestFiles("file.kt", text, new JetTestUtils.TestFileFactory<JetFile>() {
|
||||
List<JetFile> files = JetTestUtils.createTestFiles("file.kt", text, new JetTestUtils.TestFileFactoryNoModules<JetFile>() {
|
||||
@Override
|
||||
public JetFile create(String fileName, String text, Map<String, String> directives) {
|
||||
return expectedResolveData.createFileFromMarkedUpText(fileName, text);
|
||||
|
||||
@@ -52,7 +52,7 @@ public abstract class ExtensibleResolveTestCase extends JetLiteFixture {
|
||||
protected void doTest(@NonNls String filePath) throws Exception {
|
||||
File file = new File(filePath);
|
||||
String text = JetTestUtils.doLoadFile(file);
|
||||
List<JetFile> files = JetTestUtils.createTestFiles("file.kt", text, new JetTestUtils.TestFileFactory<JetFile>() {
|
||||
List<JetFile> files = JetTestUtils.createTestFiles("file.kt", text, new JetTestUtils.TestFileFactoryNoModules<JetFile>() {
|
||||
@Override
|
||||
public JetFile create(String fileName, String text, Map<String, String> directives) {
|
||||
return expectedResolveData.createFileFromMarkedUpText(fileName, text);
|
||||
|
||||
Reference in New Issue
Block a user