Skip to content

Commit a2b7fb3

Browse files
committed
Add JsonXrefTest with one non-passing for dangling span
1 parent df247ee commit a2b7fb3

File tree

7 files changed

+289
-0
lines changed

7 files changed

+289
-0
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
158158
<exclude>*.java</exclude>
159159
</excludes>
160160
</testResource>
161+
<testResource>
162+
<targetPath>org/opensolaris/opengrok/analysis/json/</targetPath>
163+
<directory>../test/org/opensolaris/opengrok/analysis/json/</directory>
164+
<excludes>
165+
<exclude>*.java</exclude>
166+
</excludes>
167+
</testResource>
161168
<testResource>
162169
<targetPath>org/opensolaris/opengrok/analysis/perl/</targetPath>
163170
<directory>../test/org/opensolaris/opengrok/analysis/perl/</directory>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.json;
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+
37+
import org.opensolaris.opengrok.analysis.CtagsReader;
38+
import org.opensolaris.opengrok.analysis.Definitions;
39+
import org.opensolaris.opengrok.analysis.FileAnalyzer;
40+
import org.opensolaris.opengrok.analysis.WriteXrefArgs;
41+
import org.junit.Test;
42+
import static org.junit.Assert.assertNotNull;
43+
import static org.opensolaris.opengrok.util.CustomAssertions.assertLinesEqual;
44+
45+
/**
46+
* Tests the {@link JsonXref} class.
47+
*/
48+
public class JsonXrefTest {
49+
50+
@Test
51+
public void sampleTest() throws IOException {
52+
writeAndCompare("org/opensolaris/opengrok/analysis/json/sample.json",
53+
"org/opensolaris/opengrok/analysis/json/sample_xref.html",
54+
getTagsDefinitions());
55+
}
56+
57+
@Test
58+
public void shouldCloseTruncatedStringSpan() throws IOException {
59+
writeAndCompare("org/opensolaris/opengrok/analysis/json/truncated.json",
60+
"org/opensolaris/opengrok/analysis/json/truncated_xref.html",
61+
null);
62+
}
63+
64+
private void writeAndCompare(String sourceResource, String resultResource,
65+
Definitions defs)
66+
throws IOException {
67+
68+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
69+
ByteArrayOutputStream baosExp = new ByteArrayOutputStream();
70+
71+
InputStream res = getClass().getClassLoader().getResourceAsStream(
72+
sourceResource);
73+
assertNotNull(sourceResource + " should get-as-stream", res);
74+
writeJsonXref(new PrintStream(baos), res, defs);
75+
res.close();
76+
77+
InputStream exp = getClass().getClassLoader().getResourceAsStream(
78+
resultResource);
79+
assertNotNull(resultResource + " should get-as-stream", exp);
80+
copyStream(exp, baosExp);
81+
exp.close();
82+
baosExp.close();
83+
baos.close();
84+
85+
String ostr = new String(baos.toByteArray(), "UTF-8");
86+
String gotten[] = ostr.split("\n");
87+
88+
String estr = new String(baosExp.toByteArray(), "UTF-8");
89+
String expected[] = estr.split("\n");
90+
91+
assertLinesEqual("Json xref", expected, gotten);
92+
}
93+
94+
private void writeJsonXref(PrintStream oss, InputStream iss,
95+
Definitions defs)
96+
throws IOException {
97+
98+
oss.print(getHtmlBegin());
99+
100+
Writer sw = new StringWriter();
101+
JsonAnalyzerFactory fac = new JsonAnalyzerFactory();
102+
FileAnalyzer analyzer = fac.getAnalyzer();
103+
analyzer.setScopesEnabled(true);
104+
analyzer.setFoldingEnabled(true);
105+
WriteXrefArgs wargs = new WriteXrefArgs(
106+
new InputStreamReader(iss, "UTF-8"), sw);
107+
wargs.setDefs(defs);
108+
analyzer.writeXref(wargs);
109+
oss.print(sw.toString());
110+
111+
oss.print(getHtmlEnd());
112+
}
113+
114+
private void copyStream(InputStream iss, OutputStream oss)
115+
throws IOException {
116+
117+
byte buffer[] = new byte[8192];
118+
int read;
119+
do {
120+
read = iss.read(buffer, 0, buffer.length);
121+
if (read > 0) {
122+
oss.write(buffer, 0, read);
123+
}
124+
} while (read >= 0);
125+
}
126+
127+
private Definitions getTagsDefinitions() throws IOException {
128+
InputStream res = getClass().getClassLoader().getResourceAsStream(
129+
"org/opensolaris/opengrok/analysis/json/sampletags");
130+
assertNotNull("though sampletags should stream,", res);
131+
132+
BufferedReader in = new BufferedReader(new InputStreamReader(
133+
res, "UTF-8"));
134+
135+
CtagsReader rdr = new CtagsReader();
136+
String line;
137+
while ((line = in.readLine()) != null) {
138+
rdr.readLine(line);
139+
}
140+
return rdr.getDefinitions();
141+
}
142+
143+
private static String getHtmlBegin() {
144+
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
145+
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" +
146+
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
147+
"<html xmlns=\"http://www.w3.org/1999/xhtml\"" +
148+
" xml:lang=\"en\" lang=\"en\"\n" +
149+
" class=\"xref\">\n" +
150+
"<head>\n" +
151+
"<title>sampleFile - OpenGrok cross reference" +
152+
" for /sampleFile</title></head><body>\n";
153+
}
154+
155+
private static String getHtmlEnd() {
156+
return "</body>\n" +
157+
"</html>\n";
158+
}
159+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"lic01" : "CDDL HEADER START",
3+
"lic02" : "",
4+
"lic03" : "The contents of this file are subject to the terms of the",
5+
"lic04" : "Common Development and Distribution License (the \"License\").",
6+
"lic05" : "You may not use this file except in compliance with the License.",
7+
"lic06" : "",
8+
"lic07" : "See LICENSE.txt included in this distribution for the specific",
9+
"lic08" : "language governing permissions and limitations under the License.",
10+
"lic09" : "",
11+
"lic10" : "When distributing Covered Code, include this CDDL HEADER in each",
12+
"lic11" : "file and include the License file at LICENSE.txt.",
13+
"lic12" : "If applicable, add the following below this CDDL HEADER, with the",
14+
"lic13" : "fields enclosed by brackets \"[]\" replaced with your own identifying",
15+
"lic14" : "information: Portions Copyright [yyyy] [name of copyright owner]",
16+
"lic15" : "",
17+
"lic16" : "CDDL HEADER END",
18+
"spc01" : null,
19+
"cpy01" : "Copyright (c) 2017, Chris Fraire <[email protected]>.",
20+
"strings" : ["\u002F", "\u002f", "\/", "/", "\uD834\uDD1E", "\"", "\'",
21+
"\\"],
22+
"spc02" : null,
23+
"numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],
24+
"spc03" : null,
25+
"booleans" : [ true, false ],
26+
"END" : ""
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
5+
class="xref">
6+
<head>
7+
<title>sampleFile - OpenGrok cross reference for /sampleFile</title></head><body>
8+
<script type="text/javascript">/* <![CDATA[ */
9+
function get_sym_list(){return [];} /* ]]> */</script><a class="l" name="1" href="#1">1</a>{
10+
<a class="l" name="2" href="#2">2</a> <span class="s">"lic01"</span> : <span class="s">"CDDL HEADER START"</span>,
11+
<a class="l" name="3" href="#3">3</a> <span class="s">"lic02"</span> : <span class="s">""</span>,
12+
<a class="l" name="4" href="#4">4</a> <span class="s">"lic03"</span> : <span class="s">"The contents of this file are subject to the terms of the"</span>,
13+
<a class="l" name="5" href="#5">5</a> <span class="s">"lic04"</span> : <span class="s">"Common Development and Distribution License (the \"License\")."</span>,
14+
<a class="l" name="6" href="#6">6</a> <span class="s">"lic05"</span> : <span class="s">"You may not use this file except in compliance with the License."</span>,
15+
<a class="l" name="7" href="#7">7</a> <span class="s">"lic06"</span> : <span class="s">""</span>,
16+
<a class="l" name="8" href="#8">8</a> <span class="s">"lic07"</span> : <span class="s">"See <a href="/source/s?path=LICENSE.txt">LICENSE.txt</a> included in this distribution for the specific"</span>,
17+
<a class="l" name="9" href="#9">9</a> <span class="s">"lic08"</span> : <span class="s">"language governing permissions and limitations under the License."</span>,
18+
<a class="hl" name="10" href="#10">10</a> <span class="s">"lic09"</span> : <span class="s">""</span>,
19+
<a class="l" name="11" href="#11">11</a> <span class="s">"lic10"</span> : <span class="s">"When distributing Covered Code, include this CDDL HEADER in each"</span>,
20+
<a class="l" name="12" href="#12">12</a> <span class="s">"lic11"</span> : <span class="s">"file and include the License file at <a href="/source/s?path=LICENSE.txt">LICENSE.txt</a>."</span>,
21+
<a class="l" name="13" href="#13">13</a> <span class="s">"lic12"</span> : <span class="s">"If applicable, add the following below this CDDL HEADER, with the"</span>,
22+
<a class="l" name="14" href="#14">14</a> <span class="s">"lic13"</span> : <span class="s">"fields enclosed by brackets \"[]\" replaced with your own identifying"</span>,
23+
<a class="l" name="15" href="#15">15</a> <span class="s">"lic14"</span> : <span class="s">"information: Portions Copyright [yyyy] [name of copyright owner]"</span>,
24+
<a class="l" name="16" href="#16">16</a> <span class="s">"lic15"</span> : <span class="s">""</span>,
25+
<a class="l" name="17" href="#17">17</a> <span class="s">"lic16"</span> : <span class="s">"CDDL HEADER END"</span>,
26+
<a class="l" name="18" href="#18">18</a> <span class="s">"spc01"</span> : <b>null</b>,
27+
<a class="l" name="19" href="#19">19</a> <span class="s">"cpy01"</span> : <span class="s">"Copyright (c) 2017, Chris Fraire &lt;[email protected]&gt;."</span>,
28+
<a class="hl" name="20" href="#20">20</a> <span class="s">"strings"</span> : [<span class="s">"\u002F"</span>, <span class="s">"\u002f"</span>, <span class="s">"\/"</span>, <span class="s">"/"</span>, <span class="s">"\uD834\uDD1E"</span>, <span class="s">"\""</span>, <span class="s">"\'"</span>,
29+
<a class="l" name="21" href="#21">21</a> <span class="s">"\\"</span>],
30+
<a class="l" name="22" href="#22">22</a> <span class="s">"spc02"</span> : <b>null</b>,
31+
<a class="l" name="23" href="#23">23</a> <span class="s">"numbers"</span> : [ <span class="n">0</span>, -<span class="n">1</span>, <span class="n">1.2</span>, -<span class="n">2.4</span>, -<span class="n">1e5</span>, <span class="n">1e-6</span>, <span class="n">20.5e97</span>, -<span class="n">33.2e-4</span> ],
32+
<a class="l" name="24" href="#24">24</a> <span class="s">"spc03"</span> : <b>null</b>,
33+
<a class="l" name="25" href="#25">25</a> <span class="s">"booleans"</span> : [ <b>true</b>, <b>false</b> ],
34+
<a class="l" name="26" href="#26">26</a> <span class="s">"END"</span> : <span class="s">""</span>
35+
<a class="l" name="27" href="#27">27</a>}
36+
<a class="l" name="28" href="#28">28</a></body>
37+
</html>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
2+
!_TAG_FILE_SORTED 0 /0=unsorted, 1=sorted, 2=foldcase/
3+
!_TAG_PROGRAM_AUTHOR Universal Ctags Team //
4+
!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
5+
!_TAG_PROGRAM_URL https://ctags.io/ /official site/
6+
!_TAG_PROGRAM_VERSION 0.0.0 /b13cb551/
7+
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
8+
lic01 grok.json /^ "lic01" : "CDDL HEADER START",$/;" string line:2
9+
lic02 grok.json /^ "lic02" : "",$/;" string line:3
10+
lic03 grok.json /^ "lic03" : "The contents of this file are subject to the terms of the",$/;" string line:4
11+
lic04 grok.json /^ "lic04" : "Common Development and Distribution License (the \\"License\\").",$/;" string line:5
12+
lic05 grok.json /^ "lic05" : "You may not use this file except in compliance with the License.",$/;" string line:6
13+
lic06 grok.json /^ "lic06" : "",$/;" string line:7
14+
lic07 grok.json /^ "lic07" : "See LICENSE.txt included in this distribution for the specific",$/;" string line:8
15+
lic08 grok.json /^ "lic08" : "language governing permissions and limitations under the License.",$/;" string line:9
16+
lic09 grok.json /^ "lic09" : "",$/;" string line:10
17+
lic10 grok.json /^ "lic10" : "When distributing Covered Code, include this CDDL HEADER in each",$/;" string line:11
18+
lic11 grok.json /^ "lic11" : "file and include the License file at LICENSE.txt.",$/;" string line:12
19+
lic12 grok.json /^ "lic12" : "If applicable, add the following below this CDDL HEADER, with the",$/;" string line:13
20+
lic13 grok.json /^ "lic13" : "fields enclosed by brackets \\"[]\\" replaced with your own identifying",$/;" string line:14
21+
lic14 grok.json /^ "lic14" : "information: Portions Copyright [yyyy] [name of copyright owner]",$/;" string line:15
22+
lic15 grok.json /^ "lic15" : "",$/;" string line:16
23+
lic16 grok.json /^ "lic16" : "CDDL HEADER END",$/;" string line:17
24+
spc01 grok.json /^ "spc01" : null,$/;" null line:18
25+
cpy01 grok.json /^ "cpy01" : "Copyright (c) 2017, Chris Fraire <[email protected]>.",$/;" string line:19
26+
0 grok.json /^ "strings" : ["\\u002F", "\\u002f", "\\\/", "\/", "\\uD834\\uDD1E", "\\"", "\\'",$/;" string line:20 array:strings
27+
1 grok.json /^ "strings" : ["\\u002F", "\\u002f", "\\\/", "\/", "\\uD834\\uDD1E", "\\"", "\\'",$/;" string line:20 array:strings
28+
2 grok.json /^ "strings" : ["\\u002F", "\\u002f", "\\\/", "\/", "\\uD834\\uDD1E", "\\"", "\\'",$/;" string line:20 array:strings
29+
3 grok.json /^ "strings" : ["\\u002F", "\\u002f", "\\\/", "\/", "\\uD834\\uDD1E", "\\"", "\\'",$/;" string line:20 array:strings
30+
4 grok.json /^ "strings" : ["\\u002F", "\\u002f", "\\\/", "\/", "\\uD834\\uDD1E", "\\"", "\\'",$/;" string line:20 array:strings
31+
5 grok.json /^ "strings" : ["\\u002F", "\\u002f", "\\\/", "\/", "\\uD834\\uDD1E", "\\"", "\\'",$/;" string line:20 array:strings
32+
6 grok.json /^ "strings" : ["\\u002F", "\\u002f", "\\\/", "\/", "\\uD834\\uDD1E", "\\"", "\\'",$/;" string line:20 array:strings
33+
7 grok.json /^ "\\\\"],$/;" string line:21 array:strings
34+
strings grok.json /^ "strings" : ["\\u002F", "\\u002f", "\\\/", "\/", "\\uD834\\uDD1E", "\\"", "\\'",$/;" array line:20
35+
spc02 grok.json /^ "spc02" : null,$/;" null line:22
36+
0 grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" number line:23 array:numbers
37+
1 grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" number line:23 array:numbers
38+
2 grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" number line:23 array:numbers
39+
3 grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" number line:23 array:numbers
40+
4 grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" number line:23 array:numbers
41+
5 grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" number line:23 array:numbers
42+
6 grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" number line:23 array:numbers
43+
7 grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" number line:23 array:numbers
44+
numbers grok.json /^ "numbers" : [ 0, -1, 1.2, -2.4, -1e5, 1e-6, 20.5e97, -33.2e-4 ],$/;" array line:23
45+
spc03 grok.json /^ "spc03" : null,$/;" null line:24
46+
0 grok.json /^ "booleans" : [ true, false ],$/;" bool line:25 array:booleans
47+
1 grok.json /^ "booleans" : [ true, false ],$/;" bool line:25 array:booleans
48+
booleans grok.json /^ "booleans" : [ true, false ],$/;" array line:25
49+
END grok.json /^ "END" : ""$/;" string line:26
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Oops this string is not ter
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
5+
class="xref">
6+
<head>
7+
<title>sampleFile - OpenGrok cross reference for /sampleFile</title></head><body>
8+
<a class="l" name="1" href="#1">1</a> <span class="s">"Oops this string is not ter</span></body>
9+
</html>

0 commit comments

Comments
 (0)