Skip to content

Commit 97a83d7

Browse files
committed
Diagnostic Based Test Results
1 parent ffef6f5 commit 97a83d7

6 files changed

Lines changed: 48 additions & 79 deletions

File tree

liquidjava-example/src/main/java/testSuite/classes/resultset_forward_correct/ResultSetTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int login(Connection con, String username, String password) throws SQLException
2424
con.prepareStatement("select typeid from users where username=? and password=?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
2525
ResultSet rs = pstat.executeQuery();
2626

27-
rs.beforeFirst(); // Expect: State Refinement Error
27+
rs.beforeFirst();
2828

2929
return typeID;
3030
}
@@ -40,7 +40,7 @@ int login2(Connection con, String username, String password) throws SQLException
4040
while (rs.next()) {
4141
rowCount++;
4242
}
43-
rs.beforeFirst(); // Expect: State Refinement Error
43+
rs.beforeFirst();
4444
if (rowCount >= 1) {
4545
while (rs.next()) {
4646
typeID = rs.getInt(1);

liquidjava-example/src/main/java/testSuite/math/errorAbs/MathRefinements.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public interface MathRefinements {
1616
public int abs(int arg0);
1717

1818
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
19-
public int abs(long arg0);
19+
public int abs(long arg0); // Expect: Warning
2020

2121
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
22-
public int abs(float arg0);
22+
public int abs(float arg0); // Expect: Warning
2323

2424
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
25-
public int abs(double arg0);
25+
public int abs(double arg0); // Expect: Warning
2626

2727
@Refinement(" _ == a+b")
2828
public int addExact(int a, int b);
@@ -43,13 +43,13 @@ public interface MathRefinements {
4343
public int decrementExact(int a);
4444

4545
@Refinement("_ == (a-1)")
46-
public int decrementExact(long a);
46+
public int decrementExact(long a); // Expect: Warning
4747

4848
@Refinement("_ == (a+1)")
4949
public int incrementExact(int a);
5050

5151
@Refinement("_ == (a+1)")
52-
public int incrementExact(long a);
52+
public int incrementExact(long a); // Expect: Warning
5353

5454
@Refinement("(a > b)? (_ == a):(_ == b)")
5555
public int max(int a, int b);
@@ -58,7 +58,7 @@ public interface MathRefinements {
5858
public int min(int a, int b);
5959

6060
@Refinement(" _ > 0.0 && _ < 1.0")
61-
public long random(long a, long b);
61+
public long random(long a, long b); // Expect: Warning
6262

6363
@Refinement("((sig > 0)?(_ > 0):(_ < 0)) && (( _ == arg)||(_ == -arg))")
6464
public float copySign(float arg, float sig);

liquidjava-example/src/main/java/testSuite/math/errorMax/MathRefinements.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public interface MathRefinements {
1616
public int abs(int arg0);
1717

1818
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
19-
public int abs(long arg0);
19+
public int abs(long arg0); // Expect: Warning
2020

2121
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
22-
public int abs(float arg0);
22+
public int abs(float arg0); // Expect: Warning
2323

2424
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
25-
public int abs(double arg0);
25+
public int abs(double arg0); // Expect: Warning
2626

2727
@Refinement(" _ == a+b")
2828
public int addExact(int a, int b);
@@ -43,13 +43,13 @@ public interface MathRefinements {
4343
public int decrementExact(int a);
4444

4545
@Refinement("_ == (a-1)")
46-
public int decrementExact(long a);
46+
public int decrementExact(long a); // Expect: Warning
4747

4848
@Refinement("_ == (a+1)")
4949
public int incrementExact(int a);
5050

5151
@Refinement("_ == (a+1)")
52-
public int incrementExact(long a);
52+
public int incrementExact(long a); // Expect: Warning
5353

5454
@Refinement("(a > b)? (_ == a):(_ == b)")
5555
public int max(int a, int b);
@@ -58,7 +58,7 @@ public interface MathRefinements {
5858
public int min(int a, int b);
5959

6060
@Refinement(" _ > 0.0 && _ < 1.0")
61-
public long random(long a, long b);
61+
public long random(long a, long b); // Expect: Warning
6262

6363
@Refinement("((sig > 0)?(_ > 0):(_ < 0)) && (( _ == arg)||(_ == -arg))")
6464
public float copySign(float arg, float sig);

liquidjava-example/src/main/java/testSuite/math/errorMultiplyExact/MathRefinements.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public interface MathRefinements {
1616
public int abs(int arg0);
1717

1818
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
19-
public int abs(long arg0);
19+
public int abs(long arg0); // Expect: Warning
2020

2121
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
22-
public int abs(float arg0);
22+
public int abs(float arg0); // Expect: Warning
2323

2424
@Refinement("(arg0 > 0)?( _ == arg0):(_ == -arg0)")
25-
public int abs(double arg0);
25+
public int abs(double arg0); // Expect: Warning
2626

2727
@Refinement(" _ == a+b")
2828
public int addExact(int a, int b);
@@ -43,13 +43,13 @@ public interface MathRefinements {
4343
public int decrementExact(int a);
4444

4545
@Refinement("_ == (a-1)")
46-
public int decrementExact(long a);
46+
public int decrementExact(long a); // Expect: Warning
4747

4848
@Refinement("_ == (a+1)")
4949
public int incrementExact(int a);
5050

5151
@Refinement("_ == (a+1)")
52-
public int incrementExact(long a);
52+
public int incrementExact(long a); // Expect: Warning
5353

5454
@Refinement("(a > b)? (_ == a):(_ == b)")
5555
public int max(int a, int b);
@@ -58,7 +58,7 @@ public interface MathRefinements {
5858
public int min(int a, int b);
5959

6060
@Refinement(" _ > 0.0 && _ < 1.0")
61-
public long random(long a, long b);
61+
public long random(long a, long b); // Expect: Warning
6262

6363
@Refinement("((sig > 0)?(_ > 0):(_ < 0)) && (( _ == arg)||(_ == -arg))")
6464
public float copySign(float arg, float sig);

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

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.file.Files;
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
11+
import java.util.ArrayList;
1112
import java.util.Collection;
1213
import java.util.List;
1314
import java.util.stream.Stream;
@@ -40,32 +41,11 @@ public void testPath(final Path path) {
4041

4142
List<Pair<String, Integer>> expectedWarnings = isDirectory ? getExpectedWarningsFromDirectory(path)
4243
: getExpectedWarningsFromFile(path);
44+
List<Pair<String, Integer>> expectedErrors = isDirectory ? getExpectedErrorsFromDirectory(path)
45+
: getExpectedErrorsFromFile(path);
4346

44-
if (shouldWarn(pathName)) {
45-
checkExpectedDiagnostics(pathName, diagnostics.getWarnings(), expectedWarnings,
46-
diagnostics.getWarningOutput());
47-
}
48-
49-
// verification should pass, check if any errors were found
50-
if (shouldPass(pathName) && diagnostics.foundError()) {
51-
System.out.println("Error in: " + pathName + " --- should pass but an error was found. \n"
52-
+ diagnostics.getErrorOutput());
53-
fail();
54-
}
55-
// verification should fail, check if it failed as expected (multiple errors can be found)
56-
else if (shouldFail(pathName)) {
57-
if (!diagnostics.foundError()) {
58-
System.out.println("Error in: " + pathName + " --- should fail but no errors were found. \n"
59-
+ diagnostics.getErrorOutput());
60-
fail();
61-
} else {
62-
// check if expected error was found
63-
List<Pair<String, Integer>> expectedErrors = isDirectory ? getExpectedErrorsFromDirectory(path)
64-
: getExpectedErrorsFromFile(path);
65-
checkExpectedDiagnostics(pathName, diagnostics.getErrors(), expectedErrors,
66-
diagnostics.getErrorOutput());
67-
}
68-
}
47+
checkExpectedDiagnostics(pathName, diagnostics.getErrors(), expectedErrors, diagnostics.getErrorOutput());
48+
checkExpectedDiagnostics(pathName, diagnostics.getWarnings(), expectedWarnings, diagnostics.getWarningOutput());
6949
}
7050

7151
/**
@@ -78,19 +58,21 @@ private static void checkExpectedDiagnostics(String pathName, Collection<? exten
7858
+ expected.size() + ". \n" + output);
7959
fail();
8060
}
81-
if (expected.isEmpty()) {
82-
System.out.println("No expected diagnostic messages found for: " + pathName);
83-
System.out.println(
84-
"Please specify each expected diagnostic in the test file as a comment on the line where it should be reported.");
85-
fail();
86-
}
61+
List<Pair<String, Integer>> unmatched = new ArrayList<>(expected);
8762
for (LJDiagnostic diagnostic : found) {
88-
boolean match = expected.stream().anyMatch(expectedDiagnostic -> matches(diagnostic, expectedDiagnostic));
89-
if (!match) {
63+
int match = -1;
64+
for (int i = 0; i < unmatched.size(); i++) {
65+
if (matches(diagnostic, unmatched.get(i))) {
66+
match = i;
67+
break;
68+
}
69+
}
70+
if (match < 0) {
9071
System.out.println(
9172
"Unexpected diagnostic in: " + pathName + " --- expected: " + expected + ". \n" + output);
9273
fail();
9374
}
75+
unmatched.remove(match);
9476
}
9577
}
9678

@@ -107,19 +89,16 @@ private static Stream<Path> sourcePaths() throws IOException {
10789
return Files.find(Paths.get("../liquidjava-example/src/main/java/testSuite/"), Integer.MAX_VALUE,
10890
(filePath, fileAttr) -> {
10991
String name = filePath.getFileName().toString();
110-
// Files that start with "Correct", "Error" or "Warning"
111-
boolean isFileStartingWithCorrectOrError = fileAttr.isRegularFile()
112-
&& (shouldPass(name) || shouldFail(name) || shouldWarn(name));
113-
114-
// Directories that contain "correct", "error" or "warning"
115-
boolean isDirectoryWithCorrectOrError = fileAttr.isDirectory()
116-
&& (shouldPass(name) || shouldFail(name) || shouldWarn(name));
117-
118-
// Return true if either condition matches
119-
return isFileStartingWithCorrectOrError || isDirectoryWithCorrectOrError;
92+
return (fileAttr.isRegularFile() || fileAttr.isDirectory()) && isTestPath(name);
12093
});
12194
}
12295

96+
private static boolean isTestPath(String path) {
97+
String lowerCasePath = path.toLowerCase();
98+
return lowerCasePath.contains("correct") || lowerCasePath.contains("error")
99+
|| lowerCasePath.contains("warning");
100+
}
101+
123102
/**
124103
* Verifies that multiple correct inputs can be processed together
125104
*/
@@ -128,9 +107,10 @@ public void testMultiplePaths() {
128107
String[] paths = { "../liquidjava-example/src/main/java/testSuite/CorrectSimple.java",
129108
"../liquidjava-example/src/main/java/testSuite/classes/arraylist_correct", };
130109
CommandLineLauncher.launch(paths);
131-
// Check if any of the paths that should be correct found an error
132-
if (diagnostics.foundError()) {
133-
System.out.println("Error found in files that should be correct. \n" + diagnostics.getErrorOutput());
110+
// The inputs have no expected diagnostics.
111+
if (diagnostics.foundError() || !diagnostics.getWarnings().isEmpty()) {
112+
System.out.println(
113+
"Unexpected diagnostic found. \n" + diagnostics.getErrorOutput() + diagnostics.getWarningOutput());
134114
fail();
135115
}
136116
}

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ public class TestUtils {
2121
private final static Factory factory = new Launcher().getFactory();
2222
private final static Context context = Context.getInstance();
2323

24-
public static boolean shouldPass(String path) {
25-
return path.toLowerCase().contains("correct");
26-
}
27-
28-
public static boolean shouldFail(String path) {
29-
return path.toLowerCase().contains("error");
30-
}
31-
32-
public static boolean shouldWarn(String path) {
33-
return path.toLowerCase().contains("warning");
34-
}
35-
3624
public static List<Pair<String, Integer>> getExpectedErrorsFromFile(Path filePath) {
3725
return getExpectedDiagnosticsFromFile(filePath, "error");
3826
}
@@ -81,6 +69,7 @@ private static List<Pair<String, Integer>> getExpectedDiagnosticsFromDirectory(P
8169
}
8270

8371
public static void addIntVariableToContext(String name) {
84-
context.addVarToContext(name, factory.Type().INTEGER_PRIMITIVE, new Predicate(), factory.Code().createCodeSnippetStatement(""));
72+
context.addVarToContext(name, factory.Type().INTEGER_PRIMITIVE, new Predicate(),
73+
factory.Code().createCodeSnippetStatement(""));
8574
}
8675
}

0 commit comments

Comments
 (0)