diff options
Diffstat (limited to 'test/core')
-rw-r--r-- | test/core/src/lombok/AbstractRunTests.java | 95 | ||||
-rw-r--r-- | test/core/src/lombok/DirectoryRunner.java | 59 | ||||
-rw-r--r-- | test/core/src/lombok/LombokTestSource.java | 239 | ||||
-rw-r--r-- | test/core/src/lombok/RunAllTests.java | 4 | ||||
-rw-r--r-- | test/core/src/lombok/RunTestsViaDelombok.java | 2 | ||||
-rw-r--r-- | test/core/src/lombok/RunTestsViaEcj.java | 2 |
6 files changed, 291 insertions, 110 deletions
diff --git a/test/core/src/lombok/AbstractRunTests.java b/test/core/src/lombok/AbstractRunTests.java index e84aec0d..34f6cbf4 100644 --- a/test/core/src/lombok/AbstractRunTests.java +++ b/test/core/src/lombok/AbstractRunTests.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2013 The Project Lombok Authors. + * Copyright (C) 2009-2014 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,21 +25,25 @@ import static org.junit.Assert.*; import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; +import org.junit.Assert; + +import lombok.core.AST; +import lombok.core.LombokConfiguration; +import lombok.core.LombokImmutableList; +import lombok.core.configuration.ConfigurationKeysLoader; +import lombok.core.configuration.ConfigurationResolver; +import lombok.core.configuration.ConfigurationResolverFactory; import lombok.javac.CapturingDiagnosticListener.CompilerMessage; public abstract class AbstractRunTests { @@ -50,39 +54,27 @@ public abstract class AbstractRunTests { } public boolean compareFile(DirectoryRunner.TestParams params, File file) throws Throwable { + ConfigurationKeysLoader.LoaderLoader.loadAllConfigurationKeys(); + final LombokTestSource sourceDirectives = LombokTestSource.readDirectives(file); + if (sourceDirectives.isIgnore() || !sourceDirectives.versionWithinLimit(params.getVersion())) return false; + + String fileName = file.getName(); + LombokTestSource expected = LombokTestSource.read(params.getAfterDirectory(), params.getMessagesDirectory(), fileName); + + if (expected.isIgnore() || !expected.versionWithinLimit(params.getVersion())) return false; + LinkedHashSet<CompilerMessage> messages = new LinkedHashSet<CompilerMessage>(); StringWriter writer = new StringWriter(); - transformCode(messages, writer, file); - String expectedFile = readFile(params.getAfterDirectory(), file, false); - List<CompilerMessageMatcher> expectedMessages = Collections.emptyList(); - if (params.getMessagesDirectory() != null) { - try { - InputStream in = new FileInputStream(new File(params.getMessagesDirectory(), file.getName() + ".messages")); - try { - expectedMessages = CompilerMessageMatcher.readAll(in); - } finally { - in.close(); - } - } catch (FileNotFoundException ex) { - // That's okay - then we expect no messages, and expectedMessages already gets initialized to the empty list. - } - } - if (expectedFile != null) { - StringReader r = new StringReader(expectedFile); - BufferedReader br = new BufferedReader(r); - String firstLine = br.readLine(); - if (firstLine != null && (firstLine.startsWith("//ignore") || params.shouldIgnoreBasedOnVersion(firstLine))) return false; - } + LombokConfiguration.overrideConfigurationResolverFactory(new ConfigurationResolverFactory() { + @Override public ConfigurationResolver createResolver(AST<?, ?, ?> ast) { + return sourceDirectives.getConfiguration(); + } + }); - compare( - file.getName(), - expectedFile, - writer.toString(), - expectedMessages, - messages, - params.printErrors()); + transformCode(messages, writer, file); + compare(file.getName(), expected, writer.toString(), messages, params.printErrors(), sourceDirectives.isSkipCompareContent() || expected.isSkipCompareContent()); return true; } @@ -105,11 +97,6 @@ public abstract class AbstractRunTests { return result.toString(); } - private String readFile(File dir, File file, boolean messages) throws IOException { - if (dir == null) return null; - return readFile(new File(dir, file.getName() + (messages ? ".messages" : ""))); - } - private static File findPlaceToDumpActualFiles() { String location = System.getProperty("lombok.tests.dump_actual_files"); if (location != null) { @@ -141,17 +128,15 @@ public abstract class AbstractRunTests { } } - private void compare(String name, String expectedFile, String actualFile, List<CompilerMessageMatcher> expectedMessages, LinkedHashSet<CompilerMessage> actualMessages, boolean printErrors) throws Throwable { - if (expectedFile == null && expectedMessages.isEmpty()) expectedFile = ""; - - if (expectedFile != null) try { - compareContent(name, expectedFile, actualFile); + private void compare(String name, LombokTestSource expected, String actualFile, LinkedHashSet<CompilerMessage> actualMessages, boolean printErrors, boolean skipCompareContent) throws Throwable { + if (!skipCompareContent) try { + compareContent(name, expected.getContent(), actualFile); } catch (Throwable e) { if (printErrors) { System.out.println("***** " + name + " *****"); System.out.println(e.getMessage()); System.out.println("**** Expected ******"); - System.out.println(expectedFile); + System.out.println(expected.getContent()); System.out.println("**** Actual ******"); System.out.println(actualFile); if (actualMessages != null && !actualMessages.isEmpty()) { @@ -169,13 +154,13 @@ public abstract class AbstractRunTests { } try { - compareMessages(name, expectedMessages, actualMessages); + compareMessages(name, expected.getMessages(), actualMessages); } catch (Throwable e) { if (printErrors) { System.out.println("***** " + name + " *****"); System.out.println(e.getMessage()); System.out.println("**** Expected ******"); - for (CompilerMessageMatcher expectedMessage : expectedMessages) { + for (CompilerMessageMatcher expectedMessage : expected.getMessages()) { System.out.println(expectedMessage); } System.out.println("**** Actual ******"); @@ -191,7 +176,7 @@ public abstract class AbstractRunTests { } } - private static void compareMessages(String name, List<CompilerMessageMatcher> expected, LinkedHashSet<CompilerMessage> actual) { + private static void compareMessages(String name, LombokImmutableList<CompilerMessageMatcher> expected, LinkedHashSet<CompilerMessage> actual) { Iterator<CompilerMessageMatcher> expectedIterator = expected.iterator(); Iterator<CompilerMessage> actualIterator = actual.iterator(); @@ -215,15 +200,23 @@ public abstract class AbstractRunTests { private static void compareContent(String name, String expectedFile, String actualFile) { String[] expectedLines = expectedFile.split("(\\r?\\n)"); String[] actualLines = actualFile.split("(\\r?\\n)"); - if (expectedLines[0].startsWith("// Generated by delombok at ")) { - expectedLines[0] = ""; + + for (int i = 0; i < expectedLines.length; i++) { + if (expectedLines[i].isEmpty() || expectedLines[i].startsWith("//")) expectedLines[i] = ""; + else break; } - if (actualLines[0].startsWith("// Generated by delombok at ")) { - actualLines[0] = ""; + for (int i = 0; i < actualLines.length; i++) { + if (actualLines[i].isEmpty() || actualLines[i].startsWith("//")) actualLines[i] = ""; + else break; } expectedLines = removeBlanks(expectedLines); actualLines = removeBlanks(actualLines); + int size = Math.min(expectedLines.length, actualLines.length); + if (size == 0 && expectedLines.length + actualLines.length > 0) { + Assert.fail("Missing / empty expected file."); + } + for (int i = 0; i < size; i++) { String expected = trimRight(expectedLines[i]); String actual = trimRight(actualLines[i]); diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index 5325c1e3..9062f523 100644 --- a/test/core/src/lombok/DirectoryRunner.java +++ b/test/core/src/lombok/DirectoryRunner.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2013 The Project Lombok Authors. + * Copyright (C) 2009-2014 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -21,16 +21,10 @@ */ package lombok; -import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; -import java.io.FileNotFoundException; -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 lombok.eclipse.Eclipse; import lombok.javac.Javac; @@ -75,46 +69,8 @@ public class DirectoryRunner extends Runner { 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() { @Override public boolean accept(File file) { return file.isFile() && file.getName().endsWith(".java"); @@ -180,9 +136,7 @@ public class DirectoryRunner extends Runner { private boolean runTest(String fileName) throws Throwable { File file = new File(params.getBeforeDirectory(), fileName); - if (mustIgnore(file)) { - return false; - } + switch (params.getCompiler()) { case DELOMBOK: return new RunTestsViaDelombok().compareFile(params, file); @@ -193,11 +147,4 @@ public class DirectoryRunner extends Runner { throw new UnsupportedOperationException(); } } - - private boolean mustIgnore(File file) throws FileNotFoundException, IOException { - BufferedReader reader = new BufferedReader(new FileReader(file)); - String line = reader.readLine(); - reader.close(); - return line != null && (line.startsWith("//ignore") || params.shouldIgnoreBasedOnVersion(line)); - } } diff --git a/test/core/src/lombok/LombokTestSource.java b/test/core/src/lombok/LombokTestSource.java new file mode 100644 index 00000000..318c5885 --- /dev/null +++ b/test/core/src/lombok/LombokTestSource.java @@ -0,0 +1,239 @@ +/* + * Copyright (C) 2014 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.junit.Assert; + +import lombok.core.LombokImmutableList; +import lombok.core.configuration.BubblingConfigurationResolver; +import lombok.core.configuration.ConfigurationProblemReporter; +import lombok.core.configuration.ConfigurationResolver; +import lombok.core.configuration.StringConfigurationSource; + +public class LombokTestSource { + private final File file; + private final String content; + private final LombokImmutableList<CompilerMessageMatcher> messages; + private final boolean ignore; + private final boolean skipCompareContent; + private final int versionLowerLimit, versionUpperLimit; + private final ConfigurationResolver configuration; + + public boolean versionWithinLimit(int version) { + return version >= versionLowerLimit && version <= versionUpperLimit; + } + + public File getFile() { + return file; + } + + public String getContent() { + return content; + } + + public LombokImmutableList<CompilerMessageMatcher> getMessages() { + return messages; + } + + public boolean isIgnore() { + return ignore; + } + + public boolean isSkipCompareContent() { + return skipCompareContent; + } + + public ConfigurationResolver getConfiguration() { + return configuration; + } + + private static final Pattern VERSION_STYLE_1 = Pattern.compile("^(\\d+)$"); + private static final Pattern VERSION_STYLE_2 = Pattern.compile("^\\:(\\d+)$"); + private static final Pattern VERSION_STYLE_3 = Pattern.compile("^(\\d+):$"); + private static final Pattern VERSION_STYLE_4 = Pattern.compile("^(\\d+):(\\d+)$"); + + private int[] parseVersionLimit(String spec) { + /* Single version: '5' */ { + Matcher m = VERSION_STYLE_1.matcher(spec); + if (m.matches()) { + int v = Integer.parseInt(m.group(1)); + return new int[] {v, v}; + } + } + + /* Upper bound: ':5' (inclusive) */ { + Matcher m = VERSION_STYLE_2.matcher(spec); + if (m.matches()) return new int[] {0, Integer.parseInt(m.group(1))}; + } + + /* Lower bound '5:' (inclusive) */ { + Matcher m = VERSION_STYLE_3.matcher(spec); + if (m.matches()) return new int[] {Integer.parseInt(m.group(1)), 0}; + } + + /* Range '7:8' (inclusive) */ { + Matcher m = VERSION_STYLE_4.matcher(spec); + if (m.matches()) return new int[] {Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2))}; + } + + return null; + } + + private static final Pattern IGNORE_PATTERN = Pattern.compile("^\\s*ignore\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE); + private static final Pattern SKIP_COMPARE_CONTENT_PATTERN = Pattern.compile("^\\s*skip[- ]?compare[- ]?content\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE); + + private LombokTestSource(File file, String content, List<CompilerMessageMatcher> messages, List<String> directives) { + this.file = file; + this.content = content; + this.messages = messages == null ? LombokImmutableList.<CompilerMessageMatcher>of() : LombokImmutableList.copyOf(messages); + + StringBuilder conf = new StringBuilder(); + int versionLower = 0; + int versionUpper = Integer.MAX_VALUE; + boolean ignore = false; + boolean skipCompareContent = false; + + for (String directive : directives) { + directive = directive.trim(); + String lc = directive.toLowerCase(); + if (IGNORE_PATTERN.matcher(directive).matches()) { + ignore = true; + continue; + } + + if (SKIP_COMPARE_CONTENT_PATTERN.matcher(directive).matches()) { + skipCompareContent = true; + continue; + } + + if (lc.startsWith("version ")) { + int[] limits = parseVersionLimit(lc.substring(7).trim()); + if (limits == null) { + Assert.fail("Directive line \"" + directive + "\" in '" + file.getAbsolutePath() + "' invalid: version must be followed by a single integer."); + throw new RuntimeException(); + } + versionLower = limits[0]; + versionUpper = limits[1]; + continue; + } + + if (lc.startsWith("conf:")) { + String confLine = directive.substring(5).trim(); + conf.append(confLine).append("\n"); + continue; + } + + Assert.fail("Directive line \"" + directive + "\" in '" + file.getAbsolutePath() + "' invalid: unrecognized directive."); + throw new RuntimeException(); + } + + this.versionLowerLimit = versionLower; + this.versionUpperLimit = versionUpper; + this.ignore = ignore; + this.skipCompareContent = skipCompareContent; + ConfigurationProblemReporter reporter = new ConfigurationProblemReporter() { + @Override public void report(String sourceDescription, String problem, int lineNumber, CharSequence line) { + Assert.fail("Problem on directive line: " + problem + " at conf line #" + lineNumber + " (" + line + ")"); + } + }; + + this.configuration = new BubblingConfigurationResolver(Collections.singleton(StringConfigurationSource.forString(conf, reporter, file.getAbsolutePath()))); + } + + public static LombokTestSource readDirectives(File file) throws IOException { + List<String> directives = new ArrayList<String>(); + + { + @Cleanup val rawIn = new FileInputStream(file); + BufferedReader in = new BufferedReader(new InputStreamReader(rawIn, "UTF-8")); + for (String i = in.readLine(); i != null; i = in.readLine()) { + if (i.isEmpty()) continue; + + if (i.startsWith("//")) { + directives.add(i.substring(2)); + } else { + break; + } + } + in.close(); + rawIn.close(); + } + + return new LombokTestSource(file, "", null, directives); + } + + public static LombokTestSource read(File sourceFolder, File messagesFolder, String fileName) throws IOException { + StringBuilder content = null; + List<String> directives = new ArrayList<String>(); + + File sourceFile = new File(sourceFolder, fileName); + if (sourceFile.exists()) { + @Cleanup val rawIn = new FileInputStream(sourceFile); + BufferedReader in = new BufferedReader(new InputStreamReader(rawIn, "UTF-8")); + for (String i = in.readLine(); i != null; i = in.readLine()) { + if (content != null) { + content.append(i).append("\n"); + continue; + } + + if (i.isEmpty()) continue; + + if (i.startsWith("//")) { + directives.add(i.substring(2)); + } else { + content = new StringBuilder(); + content.append(i).append("\n"); + } + } + in.close(); + rawIn.close(); + } + + if (content == null) content = new StringBuilder(); + + List<CompilerMessageMatcher> messages = null; + if (messagesFolder != null) { + File messagesFile = new File(messagesFolder, fileName + ".messages"); + try { + @Cleanup val rawIn = new FileInputStream(messagesFile); + messages = CompilerMessageMatcher.readAll(rawIn); + rawIn.close(); + } catch (FileNotFoundException e) { + messages = null; + } + } + + return new LombokTestSource(sourceFile, content.toString(), messages, directives); + } +} diff --git a/test/core/src/lombok/RunAllTests.java b/test/core/src/lombok/RunAllTests.java index 0076e662..9f56b45b 100644 --- a/test/core/src/lombok/RunAllTests.java +++ b/test/core/src/lombok/RunAllTests.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 The Project Lombok Authors. + * Copyright (C) 2011-2014 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,6 +26,6 @@ import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses({lombok.transform.RunTransformTests.class, lombok.bytecode.RunBytecodeTests.class}) +@SuiteClasses({lombok.transform.RunTransformTests.class, lombok.bytecode.RunBytecodeTests.class, lombok.core.configuration.RunConfigurationTests.class}) public class RunAllTests { } diff --git a/test/core/src/lombok/RunTestsViaDelombok.java b/test/core/src/lombok/RunTestsViaDelombok.java index 17665173..6a08642b 100644 --- a/test/core/src/lombok/RunTestsViaDelombok.java +++ b/test/core/src/lombok/RunTestsViaDelombok.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2013 The Project Lombok Authors. + * Copyright (C) 2009-2014 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/test/core/src/lombok/RunTestsViaEcj.java b/test/core/src/lombok/RunTestsViaEcj.java index 4f3e2794..2734eb43 100644 --- a/test/core/src/lombok/RunTestsViaEcj.java +++ b/test/core/src/lombok/RunTestsViaEcj.java @@ -107,6 +107,8 @@ public class RunTestsViaEcj extends AbstractRunTests { } }; + // TODO: Create a configuration based on confLines and set this up so that this compile run will use them. + ecjCompiler.compile(new ICompilationUnit[] {sourceUnit}); CompilationResult compilationResult = compilationResult_.get(); |