001: /* ****************************************************************************
002: * Comment_Test.java
003: *
004: * ****************************************************************************/
005:
006: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
007: * Copyright 2006-2007 Laszlo Systems, Inc. All Rights Reserved. *
008: * Use is subject to license terms. *
009: * J_LZ_COPYRIGHT_END *********************************************************/
010:
011: package org.openlaszlo.js2doc;
012:
013: import junit.framework.*;
014: import java.util.*;
015: import java.util.regex.*;
016: import javax.xml.parsers.DocumentBuilder;
017: import javax.xml.parsers.DocumentBuilderFactory;
018: import org.custommonkey.xmlunit.*;
019: import org.w3c.dom.*;
020:
021: public class Comment_Test extends XMLTestCase {
022:
023: public void testCommentScanner() {
024: String[] tests = {
025:
026: "// nope",
027: "0",
028:
029: "// nope\n// no2",
030: "0",
031:
032: "/* c1 */",
033: "1",
034: " c1 ",
035:
036: "/* c1 ",
037: "0",
038:
039: "/* c1 *",
040: "0",
041:
042: "/* c1 */ /* c2",
043: "1",
044: " c1 ",
045:
046: " /* c1 */ ",
047: "1",
048: " c1 ",
049:
050: "/* c1 */\n/* c2 */",
051: "2",
052: " c1 ",
053: " c2 ",
054:
055: "/* c1 \n */",
056: "1",
057: " c1 \n ",
058:
059: "/* c1\n c2 */",
060: "1",
061: " c1\n c2 ",
062:
063: "/** yep */",
064: "1",
065: "* yep ",
066:
067: "/* nope */\n/** yep */",
068: "2",
069: " nope ",
070: "* yep ",
071:
072: "/** yep */\n/* nope */",
073: "2",
074: "* yep ",
075: " nope ",
076:
077: "/* yep */\n// foo\n/*\n * foo\n */",
078: "2",
079: " yep ",
080: "\n * foo\n ",
081:
082: // we shouldn't parse "//* as "/" followed by the beginning of a comment "/*".
083: "//* foo\n/* bar */",
084: "1",
085: " bar ",
086:
087: "/* -*- mode: JavaScript; c-basic-offset: 2; -*- */\n/**\n * Definition of the basic LFC Library\n *\n * @copyright Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved.\n * Use is subject to license terms.\n *\n */\n/** @keywords private */",
088: "3",
089: " -*- mode: JavaScript; c-basic-offset: 2; -*- ",
090: "*\n * Definition of the basic LFC Library\n *\n * @copyright Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved.\n * Use is subject to license terms.\n *\n ",
091: "* @keywords private ",
092:
093: };
094:
095: for (Iterator iter = Arrays.asList(tests).iterator(); iter
096: .hasNext();) {
097:
098: String cr = System.getProperty("line.separator");
099:
100: String source = (String) iter.next();
101: String normalizedSource = source.replaceAll("\n", cr);
102:
103: ArrayList matches = Comment
104: .extractRawComments(normalizedSource);
105:
106: assertTrue(source, iter.hasNext());
107: int expectedMatches = Integer
108: .parseInt((String) iter.next());
109:
110: assertEquals(source, expectedMatches, matches.size());
111:
112: final int n = matches.size();
113: for (int i = 0; i < n; i++) {
114: String actual = (String) matches.get(i);
115: assertTrue(source, iter.hasNext());
116: String expect = (String) iter.next();
117: String normalizedExpect = expect.replaceAll("\n", cr);
118: if (actual.equals(normalizedExpect) != true) {
119: System.out.println("expect: '"
120: + expect.replaceAll("\n", "\\\\n")
121: .replaceAll("\r", "\\\\r") + "'");
122: System.out.println("normalizedExpect: '"
123: + normalizedExpect
124: .replaceAll("\n", "\\\\n")
125: .replaceAll("\r", "\\\\r") + "'");
126: System.out.println("actual: '"
127: + actual.replaceAll("\n", "\\\\n")
128: .replaceAll("\r", "\\\\r") + "'");
129: }
130: assertEquals(source, normalizedExpect, actual);
131: }
132: }
133: }
134:
135: public void testjs2docExtractTextContent() {
136: String[] tests = {
137:
138: "/** yep */", "yep",
139:
140: "/** yep \n * @param foo */", "yep",
141:
142: };
143:
144: for (Iterator iter = Arrays.asList(tests).iterator(); iter
145: .hasNext();) {
146:
147: String source = (String) iter.next();
148: String cr = System.getProperty("line.separator");
149: String normalizedSource = source.replaceAll("\n", cr);
150:
151: Comment resultComment = Comment
152: .extractLastJS2DocFromCommentSequence(normalizedSource);
153:
154: String expect = (String) iter.next();
155: String normalizedExpect = expect.replaceAll("\n", cr);
156:
157: assertEquals(normalizedExpect, resultComment.textContent);
158: }
159: }
160:
161: public void testjs2docExtractFieldNames() {
162: String[] tests = {
163:
164: "/** yep */", "0",
165:
166: "/** yep \n * @param foo */", "1",
167:
168: "/** yep \n * @param foo \n * @param bar*/", "2", };
169:
170: for (Iterator iter = Arrays.asList(tests).iterator(); iter
171: .hasNext();) {
172:
173: String source = (String) iter.next();
174: String cr = System.getProperty("line.separator");
175: String normalizedSource = source.replaceAll("\n", cr);
176:
177: Comment resultComment = Comment
178: .extractLastJS2DocFromCommentSequence(normalizedSource);
179:
180: int expectedFields = Integer.parseInt((String) iter.next());
181:
182: assertEquals(source, expectedFields, resultComment.fields
183: .size());
184:
185: }
186: }
187:
188: public void testExtractAsXML() {
189: String[] tests = {
190:
191: "/** */",
192: "<js2doc><doc/></js2doc>",
193:
194: "/** jaz */",
195: "<js2doc><doc><text>jaz</text></doc></js2doc>",
196:
197: "/** @foo jaz */",
198: "<js2doc><doc><tag name=\"foo\"><text>jaz</text></tag></doc></js2doc>",
199:
200: "/** jaz\n @bar baz */",
201: "<js2doc><doc><text>jaz</text><tag name=\"bar\"><text>baz</text></tag></doc></js2doc>",
202:
203: "/** jaz < bar */",
204: "<js2doc><doc><text>jaz < bar</text></doc></js2doc>", };
205:
206: for (Iterator iter = Arrays.asList(tests).iterator(); iter
207: .hasNext();) {
208:
209: String source = (String) iter.next();
210:
211: try {
212: org.w3c.dom.Document doc = null;
213: DocumentBuilderFactory factory = DocumentBuilderFactory
214: .newInstance();
215: DocumentBuilder builder = factory.newDocumentBuilder();
216:
217: org.w3c.dom.Document actual = builder.newDocument();
218: Element actualNode = actual.createElement("js2doc");
219: actual.appendChild(actualNode);
220:
221: String cr = System.getProperty("line.separator");
222: String normalizedSource = source.replaceAll("\n", cr);
223: Comment.appendStructuredCommentsAsXML(normalizedSource,
224: actualNode);
225:
226: assertTrue(source, iter.hasNext());
227: String expect = (String) iter.next();
228:
229: org.w3c.dom.Document control = XMLUnit
230: .buildControlDocument(expect);
231:
232: Diff diff = new Diff(control, actual);
233:
234: if (diff.identical() == false) {
235: System.out
236: .println("identical: " + diff.identical());
237: System.out.println("input: " + source);
238: String actualString = JS2DocUtils
239: .xmlToString(actual);
240: System.out.println("output: " + actualString);
241: System.out.println("expect: " + expect);
242: }
243:
244: assertXMLIdentical(diff, true, "JS2Doc.toXML(\""
245: + source + "\")");
246:
247: } catch (org.xml.sax.SAXException exc) {
248: fail("JS2Doc.toXML(\"" + source + "\")");
249: exc.printStackTrace();
250: } catch (java.io.IOException exc) {
251: fail("JS2Doc.toXML(\"" + source + "\")");
252: exc.printStackTrace();
253: } catch (javax.xml.parsers.ParserConfigurationException exc) {
254: fail("JS2Doc.toXML(\"" + source + "\")");
255: exc.printStackTrace();
256: }
257:
258: }
259: }
260:
261: }
|