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