Skip to content

Commit ffba560

Browse files
committed
Add (non-passing) C(xx)XrefTests with pre-BrowseableURI output
Also: - reuse lines comparison via new CustomAssertions utility class
1 parent 36ef0c5 commit ffba560

File tree

12 files changed

+1160
-24
lines changed

12 files changed

+1160
-24
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
import java.io.Writer;
3535
import org.junit.Test;
3636

37-
import static org.junit.Assert.assertEquals;
3837
import static org.junit.Assert.assertNotNull;
3938
import org.opensolaris.opengrok.analysis.FileAnalyzer;
4039
import org.opensolaris.opengrok.analysis.WriteXrefArgs;
40+
import static org.opensolaris.opengrok.util.CustomAssertions.assertLinesEqual;
4141

4242
/**
4343
* Tests the {@link AdaXref} class.
@@ -69,11 +69,7 @@ public void sampleTest() throws IOException {
6969
String estr = new String(baosExp.toByteArray(), "UTF-8");
7070
String expected[] = estr.split("\n");
7171

72-
for (int i = 0; i < expected.length && i < gotten.length; i++) {
73-
assertEquals("line " + (i + 1) + " diff", expected[i], gotten[i]);
74-
}
75-
76-
assertEquals(expected.length, gotten.length);
72+
assertLinesEqual("Ada xref", expected, gotten);
7773
}
7874

7975
private void writeAdaXref(InputStream iss, PrintStream oss) throws IOException {
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opensolaris.opengrok.analysis.c;
26+
27+
import java.io.BufferedReader;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.io.InputStreamReader;
32+
import java.io.OutputStream;
33+
import java.io.PrintStream;
34+
import java.io.StringWriter;
35+
import java.io.Writer;
36+
import org.junit.Test;
37+
38+
import static org.junit.Assert.assertEquals;
39+
import static org.junit.Assert.assertNotNull;
40+
import org.opensolaris.opengrok.analysis.CtagsReader;
41+
import org.opensolaris.opengrok.analysis.Definitions;
42+
import org.opensolaris.opengrok.analysis.FileAnalyzer;
43+
import org.opensolaris.opengrok.analysis.WriteXrefArgs;
44+
import static org.opensolaris.opengrok.util.CustomAssertions.assertLinesEqual;
45+
46+
/**
47+
* Tests the {@link CXref} class.
48+
*/
49+
public class CXrefTest {
50+
51+
@Test
52+
public void sampleTest() throws IOException {
53+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
54+
ByteArrayOutputStream baosExp = new ByteArrayOutputStream();
55+
56+
InputStream res = getClass().getClassLoader().getResourceAsStream(
57+
"org/opensolaris/opengrok/analysis/c/sample.c");
58+
assertNotNull("though sample.c should stream,", res);
59+
writeCXref(res, new PrintStream(baos));
60+
res.close();
61+
62+
InputStream exp = getClass().getClassLoader().getResourceAsStream(
63+
"org/opensolaris/opengrok/analysis/c/c_xrefres.html");
64+
assertNotNull("c_xrefres.html should stream,", exp);
65+
copyStream(exp, baosExp);
66+
exp.close();
67+
baosExp.close();
68+
baos.close();
69+
70+
String ostr = new String(baos.toByteArray(), "UTF-8");
71+
String gotten[] = ostr.split("\n");
72+
73+
String estr = new String(baosExp.toByteArray(), "UTF-8");
74+
String expected[] = estr.split("\n");
75+
76+
assertLinesEqual("C xref", expected, gotten);
77+
}
78+
79+
private void writeCXref(InputStream iss, PrintStream oss)
80+
throws IOException {
81+
82+
oss.print(getHtmlBegin());
83+
84+
Writer sw = new StringWriter();
85+
CAnalyzerFactory fac = new CAnalyzerFactory();
86+
FileAnalyzer analyzer = fac.getAnalyzer();
87+
WriteXrefArgs wargs = new WriteXrefArgs(
88+
new InputStreamReader(iss, "UTF-8"), sw);
89+
wargs.setDefs(getTagsDefinitions());
90+
analyzer.setScopesEnabled(true);
91+
analyzer.setFoldingEnabled(true);
92+
analyzer.writeXref(wargs);
93+
94+
oss.print(sw.toString());
95+
oss.print(getHtmlEnd());
96+
}
97+
98+
private void copyStream(InputStream iss, OutputStream oss)
99+
throws IOException {
100+
byte buffer[] = new byte[8192];
101+
int read;
102+
do {
103+
read = iss.read(buffer, 0, buffer.length);
104+
if (read > 0) {
105+
oss.write(buffer, 0, read);
106+
}
107+
} while (read >= 0);
108+
}
109+
110+
private Definitions getTagsDefinitions() throws IOException {
111+
InputStream res = getClass().getClassLoader().getResourceAsStream(
112+
"org/opensolaris/opengrok/analysis/c/sampletags_c");
113+
assertNotNull("though sampletags_c should stream,", res);
114+
115+
BufferedReader in = new BufferedReader(new InputStreamReader(
116+
res, "UTF-8"));
117+
118+
CtagsReader rdr = new CtagsReader();
119+
String line;
120+
while ((line = in.readLine()) != null) {
121+
rdr.readLine(line);
122+
}
123+
return rdr.getDefinitions();
124+
}
125+
126+
private static String getHtmlBegin() {
127+
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
128+
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" +
129+
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
130+
"<html xmlns=\"http://www.w3.org/1999/xhtml\"" +
131+
" xml:lang=\"en\" lang=\"en\"\n" +
132+
" class=\"xref\">\n" +
133+
"<head>\n" +
134+
"<title>sampleFile - OpenGrok cross reference" +
135+
" for /sampleFile</title></head><body>\n";
136+
}
137+
138+
private static String getHtmlEnd() {
139+
return "</body>\n" +
140+
"</html>\n";
141+
}
142+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opensolaris.opengrok.analysis.c;
26+
27+
import java.io.BufferedReader;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.io.InputStreamReader;
32+
import java.io.OutputStream;
33+
import java.io.PrintStream;
34+
import java.io.StringWriter;
35+
import java.io.Writer;
36+
import org.junit.Test;
37+
38+
import static org.junit.Assert.assertNotNull;
39+
import org.opensolaris.opengrok.analysis.CtagsReader;
40+
import org.opensolaris.opengrok.analysis.Definitions;
41+
import org.opensolaris.opengrok.analysis.FileAnalyzer;
42+
import org.opensolaris.opengrok.analysis.WriteXrefArgs;
43+
import static org.opensolaris.opengrok.util.CustomAssertions.assertLinesEqual;
44+
45+
/**
46+
* Tests the {@link CxxXref} class.
47+
*/
48+
public class CxxXrefTest {
49+
50+
@Test
51+
public void sampleTest() throws IOException {
52+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
53+
ByteArrayOutputStream baosExp = new ByteArrayOutputStream();
54+
55+
InputStream res = getClass().getClassLoader().getResourceAsStream(
56+
"org/opensolaris/opengrok/analysis/c/sample.cc");
57+
assertNotNull("though sample.cc should stream,", res);
58+
writeCxxXref(res, new PrintStream(baos));
59+
res.close();
60+
61+
InputStream exp = getClass().getClassLoader().getResourceAsStream(
62+
"org/opensolaris/opengrok/analysis/c/cc_xrefres.html");
63+
assertNotNull("cc_xrefres.html should stream,", exp);
64+
copyStream(exp, baosExp);
65+
exp.close();
66+
baosExp.close();
67+
baos.close();
68+
69+
String ostr = new String(baos.toByteArray(), "UTF-8");
70+
String gotten[] = ostr.split("\n");
71+
72+
String estr = new String(baosExp.toByteArray(), "UTF-8");
73+
String expected[] = estr.split("\n");
74+
75+
assertLinesEqual("Cxx xref", expected, gotten);
76+
}
77+
78+
private void writeCxxXref(InputStream iss, PrintStream oss)
79+
throws IOException {
80+
81+
oss.print(getHtmlBegin());
82+
83+
Writer sw = new StringWriter();
84+
CxxAnalyzerFactory fac = new CxxAnalyzerFactory();
85+
FileAnalyzer analyzer = fac.getAnalyzer();
86+
WriteXrefArgs wargs = new WriteXrefArgs(
87+
new InputStreamReader(iss, "UTF-8"), sw);
88+
wargs.setDefs(getTagsDefinitions());
89+
analyzer.setScopesEnabled(true);
90+
analyzer.setFoldingEnabled(true);
91+
analyzer.writeXref(wargs);
92+
93+
oss.print(sw.toString());
94+
oss.print(getHtmlEnd());
95+
}
96+
97+
private void copyStream(InputStream iss, OutputStream oss)
98+
throws IOException {
99+
byte buffer[] = new byte[8192];
100+
int read;
101+
do {
102+
read = iss.read(buffer, 0, buffer.length);
103+
if (read > 0) {
104+
oss.write(buffer, 0, read);
105+
}
106+
} while (read >= 0);
107+
}
108+
109+
private Definitions getTagsDefinitions() throws IOException {
110+
InputStream res = getClass().getClassLoader().getResourceAsStream(
111+
"org/opensolaris/opengrok/analysis/c/sampletags_cc");
112+
assertNotNull("though sampletags_cc should stream,", res);
113+
114+
BufferedReader in = new BufferedReader(new InputStreamReader(
115+
res, "UTF-8"));
116+
117+
CtagsReader rdr = new CtagsReader();
118+
String line;
119+
while ((line = in.readLine()) != null) {
120+
rdr.readLine(line);
121+
}
122+
return rdr.getDefinitions();
123+
}
124+
125+
private static String getHtmlBegin() {
126+
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
127+
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" +
128+
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
129+
"<html xmlns=\"http://www.w3.org/1999/xhtml\"" +
130+
" xml:lang=\"en\" lang=\"en\"\n" +
131+
" class=\"xref\">\n" +
132+
"<head>\n" +
133+
"<title>sampleFile - OpenGrok cross reference" +
134+
" for /sampleFile</title></head><body>\n";
135+
}
136+
137+
private static String getHtmlEnd() {
138+
return "</body>\n" +
139+
"</html>\n";
140+
}
141+
}

0 commit comments

Comments
 (0)