Skip to content

Commit ca52924

Browse files
committed
Code Refactoring
1 parent df5ed06 commit ca52924

2 files changed

Lines changed: 55 additions & 75 deletions

File tree

liquidjava-verifier/src/test/java/liquidjava/api/tests/TestExamples.java

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import java.nio.file.Files;
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
11+
import java.util.Collection;
1112
import java.util.List;
1213
import java.util.stream.Stream;
1314

1415
import liquidjava.api.CommandLineLauncher;
1516
import liquidjava.diagnostics.Diagnostics;
17+
import liquidjava.diagnostics.LJDiagnostic;
1618
import liquidjava.diagnostics.errors.LJError;
17-
import liquidjava.diagnostics.warnings.LJWarning;
1819
import liquidjava.utils.Pair;
1920

2021
import org.junit.Test;
@@ -45,20 +46,8 @@ public void testPath(final Path path) {
4546
: getExpectedWarningsFromFile(path);
4647

4748
if (shouldWarn(pathName)) {
48-
if (diagnostics.getWarnings().size() != expectedWarnings.size()) {
49-
System.out.println("Warnings found in: " + pathName + " --- expected exactly " + expectedWarnings.size()
50-
+ " warnings. \n" + diagnostics.getWarningOutput());
51-
fail();
52-
}
53-
for (LJWarning warning : diagnostics.getWarnings()) {
54-
int warningPosition = warning.getPosition().getLine();
55-
boolean match = expectedWarnings.stream().anyMatch(expected -> expected.second() == warningPosition);
56-
if (!match) {
57-
System.out.println("Warning in: " + pathName + " --- expected warnings: " + expectedWarnings
58-
+ ", but found one at " + warningPosition + ". \n" + diagnostics.getWarningOutput());
59-
fail();
60-
}
61-
}
49+
checkExpectedDiagnostics(pathName, diagnostics.getWarnings(), expectedWarnings,
50+
diagnostics.getWarningOutput());
6251
}
6352

6453
// verification should pass, check if any errors were found
@@ -77,35 +66,41 @@ else if (shouldFail(pathName)) {
7766
// check if expected error was found
7867
List<Pair<String, Integer>> expectedErrors = isDirectory ? getExpectedErrorsFromDirectory(path)
7968
: getExpectedErrorsFromFile(path);
80-
if (diagnostics.getErrors().size() != expectedErrors.size()) {
81-
System.out.println("Multiple errors found in: " + pathName + " --- expected exactly "
82-
+ expectedErrors.size() + " errors. \n" + diagnostics.getErrorOutput());
83-
fail();
84-
}
85-
if (!expectedErrors.isEmpty()) {
86-
for (LJError e : diagnostics.getErrors()) {
87-
String foundError = e.getTitle();
88-
int errorPosition = e.getPosition().getLine();
89-
boolean match = expectedErrors.stream().anyMatch(
90-
expected -> expected.first().equals(foundError) && expected.second() == errorPosition);
91-
92-
if (!match) {
93-
System.out.println("Error in: " + pathName + " --- expected errors: " + expectedErrors
94-
+ ", but found: " + foundError + " at " + errorPosition + ". \n"
95-
+ diagnostics.getErrorOutput());
96-
fail();
97-
}
98-
}
99-
} else {
100-
System.out.println("No expected error messages found for: " + pathName);
101-
System.out.println(
102-
"Please specify each expected error in the test file as a comment on the line where the error should be reported.");
103-
fail();
104-
}
69+
checkExpectedDiagnostics(pathName, diagnostics.getErrors(), expectedErrors,
70+
diagnostics.getErrorOutput());
71+
}
72+
}
73+
}
74+
75+
private static void checkExpectedDiagnostics(String pathName, Collection<? extends LJDiagnostic> found,
76+
List<Pair<String, Integer>> expected, String output) {
77+
if (found.size() != expected.size()) {
78+
System.out.println("Unexpected number of diagnostics found in: " + pathName + " --- expected exactly "
79+
+ expected.size() + ". \n" + output);
80+
fail();
81+
}
82+
if (expected.isEmpty()) {
83+
System.out.println("No expected diagnostic messages found for: " + pathName);
84+
System.out.println(
85+
"Please specify each expected diagnostic in the test file as a comment on the line where it should be reported.");
86+
fail();
87+
}
88+
for (LJDiagnostic diagnostic : found) {
89+
boolean match = expected.stream().anyMatch(expectedDiagnostic -> matches(diagnostic, expectedDiagnostic));
90+
if (!match) {
91+
System.out.println(
92+
"Unexpected diagnostic in: " + pathName + " --- expected: " + expected + ". \n" + output);
93+
fail();
10594
}
10695
}
10796
}
10897

98+
private static boolean matches(LJDiagnostic diagnostic, Pair<String, Integer> expected) {
99+
if (diagnostic.getPosition().getLine() != expected.second())
100+
return false;
101+
return !(diagnostic instanceof LJError) || diagnostic.getTitle().equals(expected.first());
102+
}
103+
109104
/**
110105
* Returns a Stream of paths to test files in the testSuite directory. These include files with names starting with
111106
* "Correct" or "Error", and directories containing "correct" or "error". §

liquidjava-verifier/src/test/java/liquidjava/utils/TestUtils.java

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
public class TestUtils {
1818

19+
private static final Pattern EXPECTED_DIAGNOSTIC = Pattern.compile("//\\s*(.*?\\b(Error|Warning)\\b)",
20+
Pattern.CASE_INSENSITIVE);
1921
private final static Factory factory = new Launcher().getFactory();
2022
private final static Context context = Context.getInstance();
2123

@@ -56,22 +58,25 @@ public static boolean shouldWarn(String path) {
5658
* or if there are no expected error messages in the file
5759
*/
5860
public static List<Pair<String, Integer>> getExpectedErrorsFromFile(Path filePath) {
59-
List<Pair<String, Integer>> expectedErrors = new ArrayList<>();
61+
return getExpectedDiagnosticsFromFile(filePath, "error");
62+
}
63+
64+
private static List<Pair<String, Integer>> getExpectedDiagnosticsFromFile(Path filePath, String type) {
65+
List<Pair<String, Integer>> expectedDiagnostics = new ArrayList<>();
6066
try (BufferedReader reader = Files.newBufferedReader(filePath)) {
6167
String line;
6268
int lineNumber = 0;
6369
while ((line = reader.readLine()) != null) {
6470
lineNumber++;
65-
Pattern p = Pattern.compile("//\\s*(.*?\\bError\\b)", Pattern.CASE_INSENSITIVE);
66-
Matcher m = p.matcher(line);
67-
if (m.find()) {
68-
expectedErrors.add(new Pair<>(m.group(1).trim(), lineNumber));
71+
Matcher matcher = EXPECTED_DIAGNOSTIC.matcher(line);
72+
if (matcher.find() && matcher.group(2).equalsIgnoreCase(type)) {
73+
expectedDiagnostics.add(new Pair<>(matcher.group(1).trim(), lineNumber));
6974
}
7075
}
7176
} catch (IOException e) {
7277
return List.of();
7378
}
74-
return expectedErrors;
79+
return expectedDiagnostics;
7580
}
7681

7782
/**
@@ -84,22 +89,7 @@ public static List<Pair<String, Integer>> getExpectedErrorsFromFile(Path filePat
8489
* or if there are no expected warning messages in the file
8590
*/
8691
public static List<Pair<String, Integer>> getExpectedWarningsFromFile(Path filePath) {
87-
List<Pair<String, Integer>> expectedWarnings = new ArrayList<>();
88-
try (BufferedReader reader = Files.newBufferedReader(filePath)) {
89-
String line;
90-
int lineNumber = 0;
91-
while ((line = reader.readLine()) != null) {
92-
lineNumber++;
93-
Pattern p = Pattern.compile("//\\s*(.*?\\bWarning\\b)", Pattern.CASE_INSENSITIVE);
94-
Matcher m = p.matcher(line);
95-
if (m.find()) {
96-
expectedWarnings.add(new Pair<>(m.group(1).trim(), lineNumber));
97-
}
98-
}
99-
} catch (IOException e) {
100-
return List.of();
101-
}
102-
return expectedWarnings;
92+
return getExpectedDiagnosticsFromFile(filePath, "warning");
10393
}
10494

10595
/**
@@ -111,16 +101,7 @@ public static List<Pair<String, Integer>> getExpectedWarningsFromFile(Path fileP
111101
* reading the directory or if there are no files in the directory
112102
*/
113103
public static List<Pair<String, Integer>> getExpectedErrorsFromDirectory(Path dirPath) {
114-
List<Pair<String, Integer>> expectedErrors = new ArrayList<>();
115-
try {
116-
List<Path> files = Files.list(dirPath).filter(Files::isRegularFile).toList();
117-
for (Path file : files) {
118-
expectedErrors.addAll(getExpectedErrorsFromFile(file));
119-
}
120-
} catch (IOException e) {
121-
return List.of();
122-
}
123-
return expectedErrors;
104+
return getExpectedDiagnosticsFromDirectory(dirPath, "error");
124105
}
125106

126107
/**
@@ -132,16 +113,20 @@ public static List<Pair<String, Integer>> getExpectedErrorsFromDirectory(Path di
132113
* reading the directory or if there are no files in the directory
133114
*/
134115
public static List<Pair<String, Integer>> getExpectedWarningsFromDirectory(Path dirPath) {
135-
List<Pair<String, Integer>> expectedWarnings = new ArrayList<>();
116+
return getExpectedDiagnosticsFromDirectory(dirPath, "warning");
117+
}
118+
119+
private static List<Pair<String, Integer>> getExpectedDiagnosticsFromDirectory(Path dirPath, String type) {
120+
List<Pair<String, Integer>> expectedDiagnostics = new ArrayList<>();
136121
try {
137122
List<Path> files = Files.list(dirPath).filter(Files::isRegularFile).toList();
138123
for (Path file : files) {
139-
expectedWarnings.addAll(getExpectedWarningsFromFile(file));
124+
expectedDiagnostics.addAll(getExpectedDiagnosticsFromFile(file, type));
140125
}
141126
} catch (IOException e) {
142127
return List.of();
143128
}
144-
return expectedWarnings;
129+
return expectedDiagnostics;
145130
}
146131

147132
/**

0 commit comments

Comments
 (0)