Skip to content
Permalink
Browse files
7902638: TestResult may count lines incorrectly
  • Loading branch information
dbessono committed Apr 24, 2020
1 parent e583a71 commit 35de1377bd5ed86a66e01c37b3ed2c091ca425f4
Showing with 104 additions and 19 deletions.
  1. +5 −3 src/com/sun/javatest/TestResult.java
  2. +99 −16 unit-tests/com/sun/javatest/TestResult_TextScanResult.java
@@ -2814,10 +2814,12 @@ public static TextScanResult scan(String text) {
tsr.needsFinalNewline = false;

// scan for newlines and characters requiring escapes
int lastCharPos = text.length() - 1;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 32) {
if (c == '\n') {
char nextChar = (i == lastCharPos) ? 0 : text.charAt(i + 1);
if (c < 32) { // if it's "\r\n" sep we skip the \r and would count only the \n on the next step
if ((c == '\n') || (c == '\r' && nextChar != '\n')) {
tsr.numLines++;
} else if (c != '\t' && c != '\r') {
tsr.numNonASCII++;
@@ -2837,7 +2839,7 @@ public static TextScanResult scan(String text) {
// Note this must match the check when reading the text back in,
// when we also check for just '\n' and not line.separator, because
// line.separator now, and line.separator then, might be different.
if (!text.isEmpty() && !text.endsWith("\n")) {
if (!text.isEmpty() && !text.endsWith("\n") && !text.endsWith("\r") && !text.endsWith("\r\n")) {
tsr.needsFinalNewline = true;
tsr.numLines++;
}
@@ -69,7 +69,7 @@ public void r() {
assertEquals(1, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
assertFalse(scan.needsFinalNewline);
}

@Test
@@ -89,7 +89,7 @@ public void space_r() {
assertEquals(1, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
assertFalse(scan.needsFinalNewline);
}

@Test
@@ -102,14 +102,24 @@ public void space_rn() {
assertFalse(scan.needsFinalNewline);
}

@Test
public void space_nr() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan(" \n\r");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
}

@Test
public void rr() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("\r\r");
assertEquals(0, scan.numBackslashes);
assertEquals(1, scan.numLines);
assertEquals(2, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
assertFalse(scan.needsFinalNewline);
}

@Test
@@ -122,6 +132,16 @@ public void rn() {
assertFalse(scan.needsFinalNewline);
}

@Test
public void nr() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("\n\r");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
}

@Test
public void rnrn() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("\r\n\r\n");
@@ -132,6 +152,29 @@ public void rnrn() {
assertFalse(scan.needsFinalNewline);
}

/**
* n, rn, r
*/
@Test
public void nrnr() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("\n\r\n\r");
assertEquals(0, scan.numBackslashes);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
}

@Test
public void nnnn() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("\n\n\n\n");
assertEquals(0, scan.numBackslashes);
assertEquals(4, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
}

@Test
public void nn() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("\n\n");
@@ -196,7 +239,7 @@ public void twoLines_n() {
public void twoLines_r() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\rsecond");
assertEquals(0, scan.numBackslashes);
assertEquals(1, scan.numLines);
assertEquals(2, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
@@ -222,24 +265,34 @@ public void twoLines_n_endsWith_n() {
assertFalse(scan.needsFinalNewline);
}

@Test
public void twoLines_n_endsWith_rn() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\nsecond\r\n");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
}

@Test
public void twoLines_n_endsWith_r() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\nsecond\r");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
assertFalse(scan.needsFinalNewline);
}

@Test
public void twoLines_r_endsWith_r() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\rsecond\r");
assertEquals(0, scan.numBackslashes);
assertEquals(1, scan.numLines);
assertEquals(2, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
assertFalse(scan.needsFinalNewline);
}

@Test
@@ -273,11 +326,21 @@ public void threeLines_n() {
assertTrue(scan.needsFinalNewline);
}

@Test
public void threeLines_n_endsWith_n() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\nsecond\nthird\n");
assertEquals(0, scan.numBackslashes);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
}

@Test
public void threeLines_n_r() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\nsecond\rthird");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
@@ -287,7 +350,7 @@ public void threeLines_n_r() {
public void threeLines_r_n() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\rsecond\nthird");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
@@ -297,17 +360,27 @@ public void threeLines_r_n() {
public void threeLines_r_n_endsWith_r() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\rsecond\nthird\r");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
assertFalse(scan.needsFinalNewline);
}

@Test
public void threeLines_r_n_endsWith_n() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\rsecond\nthird\n");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
}

@Test
public void threeLines_n_n_endsWith_n() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\rsecond\nthird\n");
assertEquals(0, scan.numBackslashes);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
@@ -317,7 +390,7 @@ public void threeLines_r_n_endsWith_n() {
public void threeLines_r_n_endsWith_rn() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\rsecond\nthird\r\n");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertFalse(scan.needsFinalNewline);
@@ -327,7 +400,7 @@ public void threeLines_r_n_endsWith_rn() {
public void threeLines_r_r() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\rsecond\rthird");
assertEquals(0, scan.numBackslashes);
assertEquals(1, scan.numLines);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
@@ -357,7 +430,17 @@ public void threeLines_rn_rn() {
public void threeLines_rn_r() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\r\nsecond\rthird");
assertEquals(0, scan.numBackslashes);
assertEquals(2, scan.numLines);
assertEquals(3, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);
}

@Test
public void fourLines_nr_r() {
TestResult.TextScanResult scan = TestResult.TextScanResult.scan("first\n\rsecond\rthird");
assertEquals(0, scan.numBackslashes);
assertEquals(4, scan.numLines);
assertEquals(0, scan.numNonASCII);
assertFalse(scan.needsEscape);
assertTrue(scan.needsFinalNewline);

0 comments on commit 35de137

Please sign in to comment.