Skip to content

Commit

Permalink
fix(java): Fix exception when baseline has fewer issues than what is …
Browse files Browse the repository at this point in the history
…returned by scan (#2078)

* Wrong variable used in diff

* Add test with an item removed from the baseline
  • Loading branch information
tombrunet authored Oct 29, 2024
1 parent 4297d71 commit 72d5d27
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ public static DiffResult[] diffResultsWithExpected(ACReport actual, ACReport exp
DiffResult[] differences = diff(actual, expected);
if (differences != null && differences.length > 0) {
differences = Arrays.stream(differences).filter(difference -> {
return "E".equals(difference.kind)
return !("E".equals(difference.kind)
&& difference.path.length == 4
&& difference.path.length > 2 && "bounds".equals(difference.path[2])
&& Math.abs((Integer)difference.lhs-(Integer)difference.rhs) <= 1;
&& Math.abs((Integer)difference.lhs-(Integer)difference.rhs) <= 1);
}).toArray(size -> new DiffResult[size]);
if (differences.length == 0) return null;
}
Expand Down Expand Up @@ -270,7 +270,7 @@ private static DiffResult[] diff(ACReport actual, ACReport expected) {
retVal.add(new DiffResult("A", idx, null, gson.toJson(expectedRs[idx])));
}
for (int idx=expectedRs.length; idx < actualRs.length; ++idx) {
retVal.add(new DiffResult("A", idx, gson.toJson(expectedRs[idx]), null));
retVal.add(new DiffResult("A", idx, gson.toJson(actualRs[idx]), null));
}
for (int idx=0; idx<Math.min(actualRs.length, expectedRs.length); ++idx) {
Result actualR = actualRs[idx];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import org.junit.Test;
import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
Expand Down Expand Up @@ -122,15 +125,50 @@ public static class UnitTestInfo {

@Test public void baselines() throws IOException {
Paths.get("baselines", "Selenium_getComplianceTest3.json").toFile().delete();
Paths.get("baselines", "Selenium_getComplianceTest3_1.json").toFile().delete();
AccessibilityCheckerSeleniumTest.driver.get("https://altoromutual.12mc9fdq8fib.us-south.codeengine.appdomain.cloud/");
ACReport report = AccessibilityChecker.getCompliance(driver, "Selenium_getComplianceTest2");
assertEquals(eAssertResult.FAIL, AccessibilityChecker.assertCompliance(report));
new File("baselines").mkdirs();
Files.copy(Paths.get("results", "Selenium_getComplianceTest2.json").toFile(), Paths.get("baselines", "Selenium_getComplianceTest3.json").toFile());
Files.copy(Paths.get("results", "Selenium_getComplianceTest2.json").toFile(), Paths.get("baselines", "Selenium_getComplianceTest3_1.json").toFile());

report = AccessibilityChecker.getCompliance(driver, "Selenium_getComplianceTest3");
assertEquals(eAssertResult.PASS, AccessibilityChecker.assertCompliance(report));
assertEquals(eAssertResult.PASS, AccessibilityChecker.assertCompliance(report));

removeFirstItemFromBaseline(Paths.get("baselines", "Selenium_getComplianceTest3_1.json").toFile());
report = AccessibilityChecker.getCompliance(driver, "Selenium_getComplianceTest3_1");
assertEquals(eAssertResult.BASELINE_MISMATCH, AccessibilityChecker.assertCompliance(report));
Paths.get("baselines", "Selenium_getComplianceTest3.json").toFile().delete();
Paths.get("baselines", "Selenium_getComplianceTest3_1.json").toFile().delete();
}

private static void removeFirstItemFromBaseline(File f1) {
List<String> lines = new ArrayList<String>();
String line = null;

try {
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
int idx=1;
while ((line = br.readLine()) != null) {
if (idx <= 2 || idx >= 30) {
lines.add(line);
}
++idx;
}
fr.close();
br.close();

FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
for(String s : lines)
out.write(s+"\n");
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

// @Test public void getComplianceLong() {
Expand Down

0 comments on commit 72d5d27

Please sign in to comment.