OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", | 183 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", |
184 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]); | 184 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]); |
185 checkParsedScript("Script containing Unicode composite characters", | 185 checkParsedScript("Script containing Unicode composite characters", |
186 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", | 186 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", |
187 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); | 187 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); |
188 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", | 188 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", |
189 [["foo"], ["bar", "1"]]); | 189 [["foo"], ["bar", "1"]]); |
190 | 190 |
191 test.done(); | 191 test.done(); |
192 }; | 192 }; |
| 193 |
| 194 exports.testScriptCompilation = function(test) |
| 195 { |
| 196 let {compileScript, parseScript} = Snippets; |
| 197 |
| 198 let library1 = function(exports) |
| 199 { |
| 200 exports.hello = function(message) |
| 201 { |
| 202 console.log(message); |
| 203 }; |
| 204 }; |
| 205 |
| 206 let library2 = function(exports) |
| 207 { |
| 208 exports.hello = function(message) |
| 209 { |
| 210 console.log(message || "Hello."); |
| 211 }; |
| 212 }; |
| 213 |
| 214 let libraries = [library1.toString(), library2.toString()]; |
| 215 let stringifiedLibraries = JSON.stringify(libraries); |
| 216 |
| 217 let template = ` |
| 218 "use strict"; |
| 219 { |
| 220 const libraries = {{{libraries}}}; |
| 221 |
| 222 const script = {{{script}}}; |
| 223 |
| 224 let imports = Object.create(null); |
| 225 for (let library of libraries) |
| 226 new Function("exports", library)(imports); |
| 227 |
| 228 for (let [name, ...args] of script) |
| 229 { |
| 230 if (Object.prototype.hasOwnProperty.call(imports, name)) |
| 231 { |
| 232 let value = imports[name]; |
| 233 if (typeof value == "function") |
| 234 value(...args); |
| 235 } |
| 236 } |
| 237 } |
| 238 `; |
| 239 |
| 240 function verifyExecutable(script) |
| 241 { |
| 242 let parsedScript = parseScript(script); |
| 243 |
| 244 let actual = compileScript(script, libraries); |
| 245 let expected = template.replace("{{{libraries}}}", stringifiedLibraries) |
| 246 .replace("{{{script}}}", JSON.stringify(parsedScript)); |
| 247 |
| 248 // Trim surrounding spaces because of possible difference in indentation. |
| 249 test.equal(expected.trim(), actual.trim()); |
| 250 } |
| 251 |
| 252 verifyExecutable("hello 'How are you?'"); |
| 253 |
| 254 test.done(); |
| 255 }; |
OLD | NEW |