mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 14:16:39 +00:00
Added a //version option to test files to restrict them to specific versions.
This commit is contained in:
1 parent
5ec852b35b
commit
0ecc23766f
2 files changed
+73
-3
No files matched your search
@@ -72,7 +72,7 @@ public abstract class AbstractRunTests {
|
||||
StringReader r = new StringReader(expectedFile);
|
||||
BufferedReader br = new BufferedReader(r);
|
||||
String firstLine = br.readLine();
|
||||
if (firstLine != null && firstLine.startsWith("//ignore")) return false;
|
||||
if (firstLine != null && (firstLine.startsWith("//ignore") || params.shouldIgnoreBasedOnVersion(firstLine))) return false;
|
||||
|
||||
compare(
|
||||
file.getName(),
|
||||
|
||||
@@ -29,15 +29,43 @@ import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runner.Runner;
|
||||
import org.junit.runner.notification.Failure;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
|
||||
import com.sun.tools.javac.main.JavaCompiler;
|
||||
|
||||
public class DirectoryRunner extends Runner {
|
||||
public enum Compiler {
|
||||
DELOMBOK, JAVAC, ECJ;
|
||||
DELOMBOK {
|
||||
@Override public int getVersion() {
|
||||
Matcher m = VERSION_PARSER.matcher(JavaCompiler.version());
|
||||
if (m.matches()) {
|
||||
int major = Integer.parseInt(m.group(1));
|
||||
int minor = Integer.parseInt(m.group(2));
|
||||
if (major == 1) return minor;
|
||||
}
|
||||
|
||||
return 6;
|
||||
}
|
||||
},
|
||||
JAVAC {
|
||||
@Override public int getVersion() {
|
||||
return DELOMBOK.getVersion();
|
||||
}
|
||||
},
|
||||
ECJ {
|
||||
@Override public int getVersion() {
|
||||
return 6;
|
||||
}
|
||||
};
|
||||
|
||||
private static final Pattern VERSION_PARSER = Pattern.compile("^(\\d+)\\.(\\d+).*$");
|
||||
public abstract int getVersion();
|
||||
}
|
||||
|
||||
public static abstract class TestParams {
|
||||
@@ -46,10 +74,52 @@ public class DirectoryRunner extends Runner {
|
||||
public abstract File getBeforeDirectory();
|
||||
public abstract File getAfterDirectory();
|
||||
public abstract File getMessagesDirectory();
|
||||
/** Version of the JDK dialect that the compiler can understand; for example, if you return '7', you should know what try-with-resources is. */
|
||||
public int getVersion() {
|
||||
return getCompiler().getVersion();
|
||||
}
|
||||
|
||||
public boolean accept(File file) {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static final Pattern P1 = Pattern.compile("^(\\d+)$");
|
||||
private static final Pattern P2 = Pattern.compile("^\\:(\\d+)$");
|
||||
private static final Pattern P3 = Pattern.compile("^(\\d+):$");
|
||||
private static final Pattern P4 = Pattern.compile("^(\\d+):(\\d+)$");
|
||||
|
||||
public boolean shouldIgnoreBasedOnVersion(String firstLine) {
|
||||
int thisVersion = getVersion();
|
||||
if (!firstLine.startsWith("//version ")) return false;
|
||||
|
||||
String spec = firstLine.substring("//version ".length());
|
||||
|
||||
/* Single version: '5' */ {
|
||||
Matcher m = P1.matcher(spec);
|
||||
if (m.matches()) return Integer.parseInt(m.group(1)) != thisVersion;
|
||||
}
|
||||
|
||||
/* Upper bound: ':5' (inclusive) */ {
|
||||
Matcher m = P2.matcher(spec);
|
||||
if (m.matches()) return Integer.parseInt(m.group(1)) < thisVersion;
|
||||
}
|
||||
|
||||
/* Lower bound '5:' (inclusive) */ {
|
||||
Matcher m = P3.matcher(spec);
|
||||
if (m.matches()) return Integer.parseInt(m.group(1)) > thisVersion;
|
||||
}
|
||||
|
||||
/* Range '7:8' (inclusive) */ {
|
||||
Matcher m = P4.matcher(spec);
|
||||
if (m.matches()) {
|
||||
if (Integer.parseInt(m.group(1)) < thisVersion) return true;
|
||||
if (Integer.parseInt(m.group(2)) > thisVersion) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Version validity spec not valid: " + spec);
|
||||
}
|
||||
}
|
||||
|
||||
private static final FileFilter JAVA_FILE_FILTER = new FileFilter() {
|
||||
@@ -135,6 +205,6 @@ public class DirectoryRunner extends Runner {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String line = reader.readLine();
|
||||
reader.close();
|
||||
return line != null && line.startsWith("//ignore");
|
||||
return line != null && (line.startsWith("//ignore") || params.shouldIgnoreBasedOnVersion(line));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user