Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/_common.js

Issue 29622595: Issue 6090 - Properly use regexp for message matching and proper console override (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Properly handle non-string arguments for console.*() Created Nov. 28, 2017, 9:28 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 let resourceName = match && match[1]; 84 let resourceName = match && match[1];
85 if (resourceName && resources.hasOwnProperty(resourceName)) 85 if (resourceName && resources.hasOwnProperty(resourceName))
86 return {[resourceName]: resources[resourceName]}; 86 return {[resourceName]: resources[resourceName]};
87 87
88 throw new Error( 88 throw new Error(
89 "Attempt to import unknown JavaScript module " + resource 89 "Attempt to import unknown JavaScript module " + resource
90 ); 90 );
91 }, 91 },
92 reportError(e) {} 92 reportError(e) {}
93 }, 93 },
94 console: {
95 log: console.log.bind(console),
96 error: console.error.bind(console)
97 },
98 navigator: { 94 navigator: {
99 }, 95 },
100 onShutdown: { 96 onShutdown: {
101 add() {} 97 add() {}
102 }, 98 },
103 URL 99 URL
104 }; 100 };
105 101
106 let knownModules = new Map(); 102 let knownModules = new Map();
107 for (let dir of [path.join(__dirname, "stub-modules"), 103 for (let dir of [path.join(__dirname, "stub-modules"),
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 Date: { 410 Date: {
415 now: () => currentTime 411 now: () => currentTime
416 } 412 }
417 }; 413 };
418 }; 414 };
419 415
420 console.warn = console.log; 416 console.warn = console.log;
421 417
422 exports.silenceWarnOutput = function(test, msg) 418 exports.silenceWarnOutput = function(test, msg)
423 { 419 {
424 let warnHandler = globals.console.warn; 420 let warnHandler = console.warn;
425 globals.console.warn = s => 421 console.warn = (...args) =>
426 { 422 {
423 let s;
424 if (typeof args[0] === "string")
sergei 2017/11/29 09:31:53 To be consistent with other JS code it should not
425 s = args[0];
426 else
427 s = args[0].message;
sergei 2017/11/29 09:31:54 args[0] can be undefined and before accessing `mes
Wladimir Palant 2017/11/29 10:27:31 Yes, the better approach is: let s = (args[0] i
hub 2017/11/29 15:22:32 Done.
428
427 if (s != msg) 429 if (s != msg)
428 warnHandler(s); 430 warnHandler(args);
sergei 2017/11/29 09:31:53 strictly speaking it should be warnHandler.warn.ap
Wladimir Palant 2017/11/29 10:27:31 This should be `warnHandler(...args)` - pass the o
hub 2017/11/29 15:22:32 Done.
429 }; 431 };
430 try 432 try
431 { 433 {
432 return test(); 434 return test();
433 } 435 }
434 finally 436 finally
435 { 437 {
436 globals.console.warn = warnHandler; 438 console.warn = warnHandler;
437 } 439 }
438 }; 440 };
439 441
440 exports.silenceAssertionOutput = function(test, msg) 442 exports.silenceAssertionOutput = function(test, msg)
441 { 443 {
442 let msgMatch = new RegExp(`^Error: ${msg}[\r\n]`); 444 let msgMatch = new RegExp("^Error: (.*)[\r\n]");
443 let errorHandler = globals.console.error; 445 let errorHandler = console.error;
444 globals.console.error = s => 446 console.error = (...args) =>
445 { 447 {
446 if (!msgMatch.test(s)) 448 let s;
447 errorHandler(s); 449 if (typeof args[0] === "string")
450 s = args[0];
451 else
452 s = args[0].message;
453
454 let match = s && s.match(msgMatch);
455 if (!match || match[1] != msg)
Wladimir Palant 2017/11/29 10:27:31 So why still use the regular expression if you are
hub 2017/11/29 15:22:32 We want to silence an assert (call to our assert2(
456 errorHandler(args);
448 }; 457 };
449 try 458 try
450 { 459 {
451 return test(); 460 return test();
452 } 461 }
453 finally 462 finally
454 { 463 {
455 globals.console.error = errorHandler; 464 console.error = errorHandler;
456 } 465 }
457 }; 466 };
458 467
459 exports.setupRandomResult = function() 468 exports.setupRandomResult = function()
460 { 469 {
461 let randomResult = 0.5; 470 let randomResult = 0.5;
462 Object.defineProperty(this, "randomResult", { 471 Object.defineProperty(this, "randomResult", {
463 get: () => randomResult, 472 get: () => randomResult,
464 set: value => randomResult = value 473 set: value => randomResult = value
465 }); 474 });
466 475
467 return { 476 return {
468 Math: Object.create(Math, { 477 Math: Object.create(Math, {
469 random: { 478 random: {
470 value: () => randomResult 479 value: () => randomResult
471 } 480 }
472 }) 481 })
473 }; 482 };
474 }; 483 };
475 484
476 exports.unexpectedError = function(error) 485 exports.unexpectedError = function(error)
477 { 486 {
478 console.error(error); 487 console.error(error);
479 this.ok(false, "Unexpected error: " + error); 488 this.ok(false, "Unexpected error: " + error);
480 }; 489 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld