Skip to content

Commit 5e4231e

Browse files
committed
Add ClojureXrefTest with one non-passing for dangling span
1 parent 91d0e02 commit 5e4231e

File tree

7 files changed

+684
-0
lines changed

7 files changed

+684
-0
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
9595
<exclude>*.java</exclude>
9696
</excludes>
9797
</testResource>
98+
<testResource>
99+
<targetPath>org/opensolaris/opengrok/analysis/clojure/</targetPath>
100+
<directory>../test/org/opensolaris/opengrok/analysis/clojure/</directory>
101+
<excludes>
102+
<exclude>*.java</exclude>
103+
</excludes>
104+
</testResource>
98105
<testResource>
99106
<targetPath>org/opensolaris/opengrok/analysis/document/</targetPath>
100107
<directory>../test/org/opensolaris/opengrok/analysis/document/</directory>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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.clojure;
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 ClojureXref} class.
47+
*/
48+
public class ClojureXrefTest {
49+
50+
@Test
51+
public void sampleTest() throws IOException {
52+
writeAndCompare("org/opensolaris/opengrok/analysis/clojure/sample.clj",
53+
"org/opensolaris/opengrok/analysis/clojure/sample_xref.html",
54+
getTagsDefinitions());
55+
}
56+
57+
@Test
58+
public void shouldCloseTruncatedStringSpan() throws IOException {
59+
writeAndCompare("org/opensolaris/opengrok/analysis/clojure/truncated.clj",
60+
"org/opensolaris/opengrok/analysis/clojure/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+
writeClojureXref(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("Clojure xref", expected, gotten);
92+
}
93+
94+
private void writeClojureXref(PrintStream oss, InputStream iss,
95+
Definitions defs)
96+
throws IOException {
97+
98+
oss.print(getHtmlBegin());
99+
100+
Writer sw = new StringWriter();
101+
ClojureAnalyzerFactory fac = new ClojureAnalyzerFactory();
102+
FileAnalyzer analyzer = fac.getAnalyzer();
103+
WriteXrefArgs wargs = new WriteXrefArgs(
104+
new InputStreamReader(iss, "UTF-8"), sw);
105+
wargs.setDefs(defs);
106+
analyzer.writeXref(wargs);
107+
oss.print(sw.toString());
108+
109+
oss.print(getHtmlEnd());
110+
}
111+
112+
private void copyStream(InputStream iss, OutputStream oss)
113+
throws IOException {
114+
115+
byte buffer[] = new byte[8192];
116+
int read;
117+
do {
118+
read = iss.read(buffer, 0, buffer.length);
119+
if (read > 0) {
120+
oss.write(buffer, 0, read);
121+
}
122+
} while (read >= 0);
123+
}
124+
125+
private Definitions getTagsDefinitions() throws IOException {
126+
InputStream res = getClass().getClassLoader().getResourceAsStream(
127+
"org/opensolaris/opengrok/analysis/clojure/sampletags");
128+
assertNotNull("though sampletags should stream,", res);
129+
130+
BufferedReader in = new BufferedReader(new InputStreamReader(
131+
res, "UTF-8"));
132+
133+
CtagsReader rdr = new CtagsReader();
134+
String line;
135+
while ((line = in.readLine()) != null) {
136+
rdr.readLine(line);
137+
}
138+
return rdr.getDefinitions();
139+
}
140+
141+
private static String getHtmlBegin() {
142+
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
143+
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" +
144+
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
145+
"<html xmlns=\"http://www.w3.org/1999/xhtml\"" +
146+
" xml:lang=\"en\" lang=\"en\"\n" +
147+
" class=\"xref\">\n" +
148+
"<head>\n" +
149+
"<title>sampleFile - OpenGrok cross reference" +
150+
" for /sampleFile</title></head><body>\n";
151+
}
152+
153+
private static String getHtmlEnd() {
154+
return "</body>\n" +
155+
"</html>\n";
156+
}
157+
}

0 commit comments

Comments
 (0)