2323import com .google .gwt .dev .js .ast .JsProgram ;
2424import com .google .gwt .dev .js .ast .JsStatement ;
2525import com .google .gwt .dev .util .DefaultTextOutput ;
26- import com .google .gwt .thirdparty .guava .common .base .Joiner ;
2726import com .google .gwt .thirdparty .guava .common .collect .Lists ;
2827
2928import junit .framework .TestCase ;
3837 * JavaScript. (Doesn't check that they point to the right place.)
3938 */
4039public class JsReportGenerationVisitorTest extends TestCase {
40+ boolean compact = false ;
4141 JsProgram program ;
4242
4343 // TODO(skybrian) don't generate ranges for all of these nodes. Just do executable code.
@@ -51,13 +51,14 @@ public void testEmpty() throws Exception {
5151 public void testOneStatement () throws Exception {
5252 program = parseJs ("x = 1" );
5353 checkMappings (
54- "x=1; " ,
54+ "x = 1; \n " ,
5555 "x" ,
5656 "1"
5757 );
5858 }
5959
60- public void testTwoStatements () throws Exception {
60+ public void testTwoStatementsCompact () throws Exception {
61+ compact = true ;
6162 program = parseJs ("x = 1; y = 2" );
6263 checkMappings (
6364 "x=1;y=2;" ,
@@ -70,7 +71,21 @@ public void testTwoStatements() throws Exception {
7071 );
7172 }
7273
73- public void testIfStatement () throws Exception {
74+ public void testTwoStatementsPretty () throws Exception {
75+ program = parseJs ("x = 1; y = 2" );
76+ checkMappings (
77+ "x = 1;\n y = 2;\n " ,
78+ "x = 1;\n " ,
79+ "x" ,
80+ "1" ,
81+ "y = 2;\n " ,
82+ "y" ,
83+ "2"
84+ );
85+ }
86+
87+ public void testIfStatementCompact () throws Exception {
88+ compact = true ;
7489 program = parseJs ("if(true) { x=1 } else { y=2 }" );
7590 checkMappings (
7691 "if(true){x=1}else{y=2}" ,
@@ -86,6 +101,44 @@ public void testIfStatement() throws Exception {
86101 );
87102 }
88103
104+ public void testIfStatementPretty () throws Exception {
105+ program = parseJs ("if(true) { x=1 } else { y=2 }" );
106+ checkMappings (
107+ "if (true) {\n x = 1;\n }\n else {\n y = 2;\n }\n " ,
108+ "true" ,
109+ "{\n x = 1;\n }\n " ,
110+ " x = 1;\n " ,
111+ " x" ,
112+ "1" ,
113+ "{\n y = 2;\n }\n " ,
114+ " y = 2;\n " ,
115+ " y" ,
116+ "2"
117+ );
118+ }
119+
120+ public void testFunctionCompact () throws Exception {
121+ compact = true ;
122+ program = parseJs ("function f() { return 42; }" );
123+ checkMappings ("function f(){return 42}\n " ,
124+ "function f(){return 42}" ,
125+ "{return 42}" ,
126+ "return 42" ,
127+ "42"
128+ );
129+ }
130+
131+ public void testFunctionPretty () throws Exception {
132+ program = parseJs ("function f() { return 42; }" );
133+ checkMappings (
134+ "function f(){\n return 42;\n }\n \n " ,
135+ "function f(){\n return 42;\n }\n " ,
136+ "{\n return 42;\n }\n " ,
137+ " return 42;\n " ,
138+ "42"
139+ );
140+ }
141+
89142 private JsProgram parseJs (String js ) throws IOException , JsParserException {
90143 JsProgram program = new JsProgram ();
91144 SourceInfo info = SourceOrigin .create (0 , js .length (), 123 , "test.js" );
@@ -97,14 +150,20 @@ private JsProgram parseJs(String js) throws IOException, JsParserException {
97150
98151 private void checkMappings (String ...expectedLines )
99152 throws IOException , JsParserException {
100- DefaultTextOutput text = new DefaultTextOutput (true );
153+ DefaultTextOutput text = new DefaultTextOutput (compact );
101154 JsReportGenerationVisitor generator = new JsReportGenerationVisitor (text ,
102155 JavaToJavaScriptMap .EMPTY );
103156 generator .accept (program );
104157 String actual = dumpMappings (text .toString (), generator .getSourceInfoMap ());
105158
106- String expected = expectedLines .length == 0 ? "" : Joiner .on ("\n " ).join (expectedLines ) + "\n " ;
107- assertEquals ("Mappings:\n " + expected , actual );
159+ StringBuilder expected = new StringBuilder ();
160+ expected .append ("Mappings:\n " );
161+ for (String line : expectedLines ) {
162+ expected .append (escape (line ));
163+ expected .append ("\n " );
164+ }
165+
166+ assertEquals (expected .toString (), actual );
108167 }
109168
110169 private String dumpMappings (String javascript , JsSourceMap mappings ) {
@@ -115,9 +174,16 @@ private String dumpMappings(String javascript, JsSourceMap mappings) {
115174 out .append ("Mappings:\n " );
116175 for (Range r : ranges ) {
117176 String js = javascript .substring (r .getStart (), r .getEnd ());
118- out .append (js );
177+ out .append (escape ( js ) );
119178 out .append ("\n " );
120179 }
121180 return out .toString ();
122181 }
182+
183+ /**
184+ * Escape newlines in a readable way so that each range is on one line for comparison.
185+ */
186+ private String escape (String js ) {
187+ return js .replace ("\\ n" , "\\ \\ n" ).replace ("\n " , "\\ n" );
188+ }
123189}
0 commit comments