Skip to content

Commit 38958e6

Browse files
committed
Update SH tests in JFlexXrefTest
Also: - Extract an override of assertLinesEqual() that takes strings and splits them, and simplify current callers to use the new override.
1 parent b42298e commit 38958e6

File tree

9 files changed

+54
-61
lines changed

9 files changed

+54
-61
lines changed

test/org/opensolaris/opengrok/analysis/JFlexXrefTest.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import javax.xml.parsers.DocumentBuilderFactory;
3737
import org.apache.lucene.document.Document;
3838
import org.junit.AfterClass;
39+
import static org.junit.Assert.assertEquals;
40+
import static org.junit.Assert.assertTrue;
3941
import org.junit.BeforeClass;
4042
import org.junit.Test;
4143
import org.opensolaris.opengrok.analysis.c.CXref;
@@ -56,11 +58,10 @@
5658
import org.opensolaris.opengrok.analysis.tcl.TclXref;
5759
import org.opensolaris.opengrok.analysis.uue.UuencodeXref;
5860
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
61+
import static org.opensolaris.opengrok.util.CustomAssertions.assertLinesEqual;
5962
import org.opensolaris.opengrok.util.TestRepository;
6063
import org.xml.sax.InputSource;
6164

62-
import static org.junit.Assert.*;
63-
6465
/**
6566
* Unit tests for JFlexXref.
6667
*/
@@ -183,18 +184,18 @@ private void bug15890Anchor(Class<? extends JFlexXref> klass, String path)
183184
@Test
184185
public void testBug14663() throws Exception {
185186
// \" should not start a new string literal
186-
assertXrefLine(ShXref.class, "echo \\\"", "<b>echo</b> \\\"");
187+
assertXrefLine(ShXref.class, "echo \\\"", "<b>echo</b> \\&quot;");
187188
// \" should not terminate a string literal
188189
assertXrefLine(ShXref.class, "echo \"\\\"\"",
189-
"<b>echo</b> <span class=\"s\">\"\\\"\"</span>");
190+
"<b>echo</b> <span class=\"s\">&quot;\\&quot;&quot;</span>");
190191
// \` should not start a command substitution
191192
assertXrefLine(ShXref.class, "echo \\`", "<b>echo</b> \\`");
192193
// \` should not start command substitution inside a string
193194
assertXrefLine(ShXref.class, "echo \"\\`\"",
194-
"<b>echo</b> <span class=\"s\">\"\\`\"</span>");
195+
"<b>echo</b> <span class=\"s\">&quot;\\`&quot;</span>");
195196
// \` should not terminate command substitution
196197
assertXrefLine(ShXref.class, "echo `\\``",
197-
"<b>echo</b> <span>`\\``</span>");
198+
"<b>echo</b> `\\``");
198199
// $# should not start a comment
199200
assertXrefLine(ShXref.class, "$#", "$#");
200201
}
@@ -216,7 +217,8 @@ private void assertXrefLine(Class<? extends JFlexXref> xrefClass,
216217
StringWriter output = new StringWriter();
217218
xref.write(output);
218219

219-
assertEquals(FIRST_LINE_PREAMBLE + expectedOutput, output.toString());
220+
assertLinesEqual("xref " + xrefClass.getSimpleName(),
221+
FIRST_LINE_PREAMBLE + expectedOutput, output.toString());
220222
}
221223

222224
/**
@@ -227,25 +229,25 @@ private void assertXrefLine(Class<? extends JFlexXref> xrefClass,
227229
*/
228230
@Test
229231
public void bug16883() throws Exception {
232+
final String ECHO_QUOT_XYZ = "echo \"xyz";
230233
// Analyze a script with broken syntax (unterminated string literal)
231-
ShXref xref = new ShXref(new StringReader("echo \"xyz"));
234+
ShXref xref = new ShXref(new StringReader(ECHO_QUOT_XYZ));
232235
StringWriter out = new StringWriter();
233236
xref.write(out);
234-
assertEquals(
235-
FIRST_LINE_PREAMBLE +
236-
"<b>echo</b> <span class=\"s\">\"xyz</span>",
237-
out.toString());
237+
assertLinesEqual("Unterminated string:\n" + ECHO_QUOT_XYZ,
238+
FIRST_LINE_PREAMBLE +
239+
"<b>echo</b> <span class=\"s\">&quot;xyz</span>", out.toString());
238240

239241
// Reuse the xref and verify that the broken syntax in the previous
240242
// file doesn't cause broken highlighting in the next file
241243
out = new StringWriter();
242244
String contents = "echo \"hello\"";
243245
xref.reInit(contents.toCharArray(), contents.length());
244246
xref.write(out);
245-
assertEquals(
246-
FIRST_LINE_PREAMBLE +
247-
"<b>echo</b> <span class=\"s\">\"hello\"</span>",
248-
out.toString());
247+
assertLinesEqual("reused ShXref after broken syntax",
248+
FIRST_LINE_PREAMBLE +
249+
"<b>echo</b> <span class=\"s\">&quot;hello&quot;</span>",
250+
out.toString());
249251
}
250252

251253
/**
@@ -315,24 +317,27 @@ public void testCxxXrefTemplateParameters() throws Exception {
315317
*/
316318
@Test
317319
public void testShXrefHeredoc() throws IOException {
320+
final String SH_HERE_DOC = "cat<<EOF\n" +
321+
"This shouldn't cause any problem.\n" +
322+
"EOF\n" +
323+
"var='some string'\n";
318324
StringReader in = new StringReader(
319-
"cat<<EOF\n" +
320-
"This shouldn't cause any problem.\n" +
321-
"EOF\n" +
322-
"var='some string'\n");
325+
SH_HERE_DOC);
323326

324327
ShXref xref = new ShXref(in);
325328
StringWriter out = new StringWriter();
326329
xref.write(out);
327330

328-
String[] result = out.toString().split("\n");
331+
String xout = out.toString();
332+
String[] result = xout.split("\n");
329333

330334
// The single-quote on line 2 shouldn't start a string literal.
331-
assertTrue(result[1].endsWith("This shouldn't cause any problem."));
335+
assertTrue("Line 2 of:\n" + xout, result[1].endsWith(
336+
"This shouldn&apos;t cause any problem."));
332337

333338
// The string literal on line 4 should be recognized as one.
334-
assertTrue(
335-
result[3].endsWith("=<span class=\"s\">'some string'</span>"));
339+
assertTrue("Line 4 of:\n" + xout,
340+
result[3].endsWith("=<span class=\"s\">&apos;some string&apos;</span>"));
336341
}
337342

338343
/**

test/org/opensolaris/opengrok/analysis/ada/AdaXrefTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ public void sampleTest() throws IOException {
6464
baos.close();
6565

6666
String ostr = new String(baos.toByteArray(), "UTF-8");
67-
String gotten[] = ostr.split("\n");
68-
6967
String estr = new String(baosExp.toByteArray(), "UTF-8");
70-
String expected[] = estr.split("\n");
71-
72-
assertLinesEqual("Ada xref", expected, gotten);
68+
assertLinesEqual("Ada xref", estr, ostr);
7369
}
7470

7571
private void writeAdaXref(InputStream iss, PrintStream oss) throws IOException {

test/org/opensolaris/opengrok/analysis/c/CXrefTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,8 @@ public void sampleTest() throws IOException {
6868
baos.close();
6969

7070
String ostr = new String(baos.toByteArray(), "UTF-8");
71-
String gotten[] = ostr.split("\n");
72-
7371
String estr = new String(baosExp.toByteArray(), "UTF-8");
74-
String expected[] = estr.split("\n");
75-
76-
assertLinesEqual("C xref", expected, gotten);
72+
assertLinesEqual("C xref", estr, ostr);
7773
}
7874

7975
private void writeCXref(InputStream iss, PrintStream oss)

test/org/opensolaris/opengrok/analysis/c/CxxXrefTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,8 @@ public void sampleTest() throws IOException {
6767
baos.close();
6868

6969
String ostr = new String(baos.toByteArray(), "UTF-8");
70-
String gotten[] = ostr.split("\n");
71-
7270
String estr = new String(baosExp.toByteArray(), "UTF-8");
73-
String expected[] = estr.split("\n");
74-
75-
assertLinesEqual("Cxx xref", expected, gotten);
71+
assertLinesEqual("Cxx xref", estr, ostr);
7672
}
7773

7874
private void writeCxxXref(InputStream iss, PrintStream oss)

test/org/opensolaris/opengrok/analysis/fortran/FortranXrefTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,8 @@ private void writeAndCompare(String sourceResource, String resultResource,
8383
baos.close();
8484

8585
String ostr = new String(baos.toByteArray(), "UTF-8");
86-
String gotten[] = ostr.split("\n");
87-
8886
String estr = new String(baosExp.toByteArray(), "UTF-8");
89-
String expected[] = estr.split("\n");
90-
91-
assertLinesEqual("CSharp xref", expected, gotten);
87+
assertLinesEqual("Fortran xref", estr, ostr);
9288
}
9389

9490
private void writeFortranXref(PrintStream oss, InputStream iss,

test/org/opensolaris/opengrok/analysis/perl/PerlXrefTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ public void sampleTest() throws IOException {
6464
baos.close();
6565

6666
String ostr = new String(baos.toByteArray(), "UTF-8");
67-
String gotten[] = ostr.split("\n");
68-
6967
String estr = new String(baosExp.toByteArray(), "UTF-8");
70-
String expected[] = estr.split("\n");
71-
72-
assertLinesEqual("Perl xref", expected, gotten);
68+
assertLinesEqual("Perl xref", estr, ostr);
7369
}
7470

7571
private void writePerlXref(InputStream iss, PrintStream oss) throws IOException {

test/org/opensolaris/opengrok/analysis/ruby/RubyXrefTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,8 @@ public void sampleTest() throws IOException {
6767
baos.close();
6868

6969
String ostr = new String(baos.toByteArray(), "UTF-8");
70-
String gotten[] = ostr.split("\n");
71-
7270
String estr = new String(baosExp.toByteArray(), "UTF-8");
73-
String expected[] = estr.split("\n");
74-
75-
assertLinesEqual("Ruby xref", expected, gotten);
71+
assertLinesEqual("Ruby xref", estr, ostr);
7672
}
7773

7874
private void writeRubyXref(InputStream iss, PrintStream oss)

test/org/opensolaris/opengrok/analysis/sh/ShXrefTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,8 @@ private void writeAndCompare(String sourceResource, String resultResource,
8383
baos.close();
8484

8585
String ostr = new String(baos.toByteArray(), "UTF-8");
86-
String gotten[] = ostr.split("\n");
87-
8886
String estr = new String(baosExp.toByteArray(), "UTF-8");
89-
String expected[] = estr.split("\n");
90-
91-
assertLinesEqual("sh xref", expected, gotten);
87+
assertLinesEqual("sh xref", estr, ostr);
9288
}
9389

9490
private void writeShXref(PrintStream oss, InputStream iss,

test/org/opensolaris/opengrok/util/CustomAssertions.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ public class CustomAssertions {
4343
protected CustomAssertions() {
4444
}
4545

46+
/**
47+
* Asserts the specified strings have equal contents, comparing line-wise
48+
* after splitting on LFs.
49+
* @param messagePrefix a message prefixed to line-specific or length-
50+
* specific errors
51+
* @param expected the expected content
52+
* @param actual the actual content
53+
*/
54+
public static void assertLinesEqual(String messagePrefix, String expected,
55+
String actual) {
56+
57+
String expecteds[] = expected.split("\n");
58+
String gotten[] = actual.split("\n");
59+
assertLinesEqual(messagePrefix, expecteds, gotten);
60+
}
61+
4662
/**
4763
* Asserts the specified lines arrays have equal contents.
4864
* @param messagePrefix a message prefixed to line-specific or length-
@@ -77,8 +93,8 @@ public static void assertLinesEqual(String messagePrefix,
7793
diffLines.clear();
7894
}
7995

80-
assertTrue("should have no diffs", ndiffs == 0);
81-
assertEquals(messagePrefix + ":number of lines", expecteds.length,
96+
assertTrue(messagePrefix + "--should have no diffs", ndiffs == 0);
97+
assertEquals(messagePrefix + "--number of lines", expecteds.length,
8298
actuals.length);
8399
}
84100

0 commit comments

Comments
 (0)