1 | dojo.provide("shrinksafe.tests.module"); |
---|
2 | |
---|
3 | // basic helper functions for running multiple tests. |
---|
4 | shrinksafe.tests.module.getContents = function(path){ |
---|
5 | // summary: Load a file from this /tests/ path into a variable |
---|
6 | path = "../shrinksafe/tests/" + path; |
---|
7 | return readFile(path); // String |
---|
8 | } |
---|
9 | |
---|
10 | shrinksafe.tests.module.compress = function(source, stripConsole, escapeUnicode){ |
---|
11 | // summary: Shorthand to compress some String version of JS code |
---|
12 | return new String(Packages.org.dojotoolkit.shrinksafe.Compressor.compressScript(source, 0, 1, escapeUnicode, stripConsole)).toString(); |
---|
13 | } |
---|
14 | |
---|
15 | shrinksafe.tests.module.loader = function(path, stripConsole, escapeUnicode){ |
---|
16 | // summary: Simple function to load and compress some file. Returns and object |
---|
17 | // with 'original' and 'compressed' members, respectively. |
---|
18 | var s = shrinksafe.tests.module.getContents(path); |
---|
19 | return { |
---|
20 | original: s, |
---|
21 | compressed: shrinksafe.tests.module.compress(s, stripConsole, escapeUnicode || false) |
---|
22 | }; |
---|
23 | } |
---|
24 | |
---|
25 | try{ |
---|
26 | tests.register("shrinksafe", |
---|
27 | [ |
---|
28 | function forwardReference(t){ |
---|
29 | |
---|
30 | var src = shrinksafe.tests.module.loader("3241.js", null); |
---|
31 | |
---|
32 | t.assertTrue(src.original.length > src.compressed.length); |
---|
33 | t.assertTrue(src.compressed.indexOf("test") == -1) |
---|
34 | |
---|
35 | eval(src.compressed); |
---|
36 | t.assertEqual("data", result); |
---|
37 | delete result; |
---|
38 | }, |
---|
39 | |
---|
40 | function nestedReference(t){ |
---|
41 | var src = shrinksafe.tests.module.loader("5303.js", null); |
---|
42 | |
---|
43 | t.assertTrue(src.original.length > src.compressed.length); |
---|
44 | t.assertTrue(src.compressed.indexOf("say_hello") == -1) |
---|
45 | t.assertTrue(src.compressed.indexOf("callback") == -1) |
---|
46 | |
---|
47 | eval(src.compressed); |
---|
48 | // make sure it runs to completion |
---|
49 | t.assertEqual("hello worldhello world", result); |
---|
50 | // globals must not be renamed |
---|
51 | t.assertEqual("function", typeof CallMe); |
---|
52 | delete result; |
---|
53 | }, |
---|
54 | |
---|
55 | function varConflict(t){ |
---|
56 | // ensuring a shrunken variable won't overwrite an existing variable |
---|
57 | // name, regardless of scope. |
---|
58 | var src = shrinksafe.tests.module.loader("8974.js", null); |
---|
59 | |
---|
60 | t.assertTrue(src.original.length > src.compressed.length); |
---|
61 | t.assertTrue(src.compressed.indexOf("variabletwo") == -1) |
---|
62 | |
---|
63 | eval(src.compressed); |
---|
64 | t.assertEqual(-1, result); |
---|
65 | delete result; |
---|
66 | }, |
---|
67 | |
---|
68 | function varlists(t){ |
---|
69 | // test to ensure var a, b, c; always works |
---|
70 | var src = shrinksafe.tests.module.loader("1768.js", null); |
---|
71 | |
---|
72 | // ensure the things we expect to hide are hidden |
---|
73 | t.t(src.compressed.indexOf("superlong") == -1); |
---|
74 | t.t(src.compressed.indexOf("aFunction") == -1); |
---|
75 | t.t(src.compressed.indexOf("inList") == -1); |
---|
76 | |
---|
77 | // sanity checking: |
---|
78 | var boo = eval(src.compressed); |
---|
79 | t.is(4, result); |
---|
80 | delete result; |
---|
81 | |
---|
82 | }, |
---|
83 | |
---|
84 | function stripConsoleNormal(t){ |
---|
85 | var src = shrinksafe.tests.module.loader("stripconsole.js", "normal"); |
---|
86 | |
---|
87 | t.assertTrue(src.compressed.indexOf("console.debug(\"debug here!\"") == -1) |
---|
88 | t.assertTrue(src.compressed.indexOf("console.warn(\"warn here!\")") != -1) |
---|
89 | t.assertTrue(src.compressed.indexOf("console.error(\"error here!\")") != -1) |
---|
90 | |
---|
91 | eval(src.compressed); |
---|
92 | // make sure expected output occurs. |
---|
93 | t.assertEqual("WARN: warn here!ERROR: error here!notconsole debug here!notconsole warn here!", result); |
---|
94 | delete result; |
---|
95 | }, |
---|
96 | |
---|
97 | function stripConsoleWarns(t){ |
---|
98 | var src = shrinksafe.tests.module.loader("stripconsole.js", "warn"); |
---|
99 | |
---|
100 | t.assertTrue(src.original.length > src.compressed.length); |
---|
101 | t.assertTrue(src.compressed.indexOf("console.debug(\"debug here!\"") == -1) |
---|
102 | t.assertTrue(src.compressed.indexOf("console.warn(\"warn here!\")") == -1) |
---|
103 | t.assertTrue(src.compressed.indexOf("console.error(\"error here!\")") != -1) |
---|
104 | |
---|
105 | eval(src.compressed); |
---|
106 | // make sure expected output occurs. |
---|
107 | t.assertEqual("ERROR: error here!notconsole debug here!notconsole warn here!", result); |
---|
108 | delete result; |
---|
109 | }, |
---|
110 | |
---|
111 | function stripConsoleAll(t){ |
---|
112 | var src = shrinksafe.tests.module.loader("stripconsole.js", "all"); |
---|
113 | |
---|
114 | t.assertTrue(src.original.length > src.compressed.length); |
---|
115 | t.assertTrue(src.compressed.indexOf("console.debug(\"debug here!\"") == -1) |
---|
116 | t.assertTrue(src.compressed.indexOf("console.warn(\"warn here!\")") == -1) |
---|
117 | t.assertTrue(src.compressed.indexOf("console.error(\"error here!\")") == -1) |
---|
118 | |
---|
119 | eval(src.compressed); |
---|
120 | // make sure expected output occurs. |
---|
121 | t.assertEqual("notconsole debug here!notconsole warn here!", result); |
---|
122 | delete result; |
---|
123 | }, |
---|
124 | |
---|
125 | function stripConsoleComplex(t){ |
---|
126 | var src = shrinksafe.tests.module.loader("stripcomplex.js", "normal"); |
---|
127 | |
---|
128 | t.assertTrue(src.original.length > src.compressed.length); |
---|
129 | |
---|
130 | eval(src.compressed); |
---|
131 | // make sure expected output occurs. |
---|
132 | t.assertEqual("ERROR: wooosome \\ dodgy \" characters *$!?//3-3fn saw arg 'wooo'.ERROR: Error return statement.", result); |
---|
133 | delete result; |
---|
134 | }, |
---|
135 | |
---|
136 | function debuggerCall(t){ |
---|
137 | // make sure we don't die when we find a debugger; statement |
---|
138 | var src = shrinksafe.tests.module.loader("9444.js", null); |
---|
139 | t.t(src.compressed.indexOf("debugger") > -1); |
---|
140 | }, |
---|
141 | |
---|
142 | function nestedReference(t){ |
---|
143 | var src = shrinksafe.tests.module.loader("9676.js", null); |
---|
144 | eval(src.compressed); // will throw on failure |
---|
145 | t.assertEqual(6, result); |
---|
146 | delete result; |
---|
147 | }, |
---|
148 | |
---|
149 | function escapeUnicode(t){ |
---|
150 | var src = shrinksafe.tests.module.loader("escapeunicode.js", null); |
---|
151 | t.assertTrue(src.compressed.indexOf('"\u03b1";') == 0); |
---|
152 | // t.is('"\u03b1 \u03c9";', src.compressed); // extended test isn't working... encoding problem with input? |
---|
153 | |
---|
154 | src = shrinksafe.tests.module.loader("escapeunicode.js", null, true); |
---|
155 | t.assertTrue(src.compressed.indexOf('"\\u03b1";') == 0); |
---|
156 | // t.is('"\\u03b1 \\u03c9";', src.compressed); |
---|
157 | }, |
---|
158 | |
---|
159 | function mungeStrings(t){ |
---|
160 | |
---|
161 | // this test is skipped intentionally. You must manually enabled the |
---|
162 | // code in Compressor.java which enables this functionality. The functionality |
---|
163 | // is not considered completely "safe" and thus has been disabled. |
---|
164 | // simply uncomment the block in Compressor.java to reinstate functionality. |
---|
165 | // original patch came under [ccla]. See bugs.dojotoolkit.org/ticket/8828 |
---|
166 | return; |
---|
167 | |
---|
168 | var src = shrinksafe.tests.module.loader("8828.js"); |
---|
169 | |
---|
170 | t.t(src.compressed.indexOf("ab") > -1); // basic test |
---|
171 | t.t(src.compressed.indexOf('"a"+n') > -1); // basic expected miss |
---|
172 | t.t(src.compressed.indexOf('thisisatestspanning"+h') > -1); |
---|
173 | t.t(src.compressed.indexOf("testingmultiplelines") > -1); |
---|
174 | t.t(src.compressed.indexOf("testingcombined\"+") > -1); |
---|
175 | |
---|
176 | var boo = eval(src.compressed); |
---|
177 | t.is(5, result.length); |
---|
178 | |
---|
179 | t.t(result[3].indexOf("TheQuickredFox") > -1); // complex var post eval |
---|
180 | t.is(result[4], "thisisatestspanning4lines"); // multiline post eval |
---|
181 | |
---|
182 | delete result; |
---|
183 | |
---|
184 | var expected = [ |
---|
185 | "testing","testbarsimple", |
---|
186 | "testingcombinedbarvariables", |
---|
187 | "test \"+weird syntax", |
---|
188 | "testbasic", |
---|
189 | "test \"mixed\"", |
---|
190 | "testingmultiplelines", |
---|
191 | "test \"mixed\" andmunge", |
---|
192 | "test", |
---|
193 | "tesbart", |
---|
194 | "\"slightly\"+\"off\"", |
---|
195 | // fails: |
---|
196 | "falseb", "falseb", "falseb" |
---|
197 | ]; |
---|
198 | |
---|
199 | var data = string_tests(); |
---|
200 | data.forEach(function(str, i){ |
---|
201 | t.is(expected[i], str); |
---|
202 | }); |
---|
203 | |
---|
204 | delete string_tests; |
---|
205 | } |
---|
206 | ]); |
---|
207 | }catch(e){ |
---|
208 | doh.debug(e); |
---|
209 | } |
---|