Commit 7c30fcf2 authored by machenbach's avatar machenbach Committed by Commit bot

[foozzie] Fix mock variables that leaked into the global object

Also tidy some JS style in the file.

BUG=chromium:673246
NOTRY=true

Review-Url: https://codereview.chromium.org/2650353004
Cr-Commit-Position: refs/heads/master@{#42659}
parent 1c1742f3
...@@ -12,57 +12,55 @@ ...@@ -12,57 +12,55 @@
// This will be overridden in the test cases. The override can be minimized. // This will be overridden in the test cases. The override can be minimized.
var __PrettyPrint = function __PrettyPrint(msg) { print(msg); }; var __PrettyPrint = function __PrettyPrint(msg) { print(msg); };
// All calls to f.arguments are replaced by f.mock_arguments by an external
// script.
Object.prototype.mock_arguments = ['x', 'y']
// Mock Math.random. // Mock Math.random.
var __magic_index_for_mocked_random = 0 (function () {
Math.random = function(){ var index = 0
__magic_index_for_mocked_random = (__magic_index_for_mocked_random + 1) % 10 Math.random = function() {
return __magic_index_for_mocked_random / 10.0; index = (index + 1) % 10;
} return index / 10.0;
}
})();
// Mock Date. // Mock Date.
var __magic_index_for_mocked_date = 0 (function () {
var __magic_mocked_date = 1477662728696 var index = 0
__magic_mocked_date_now = function(){ var mockDate = 1477662728696
__magic_index_for_mocked_date = (__magic_index_for_mocked_date + 1) % 10 var mockDateNow = function() {
__magic_mocked_date = __magic_mocked_date + __magic_index_for_mocked_date + 1 index = (index + 1) % 10
return __magic_mocked_date mockDate = mockDate + index + 1
} return mockDate
}
var __original_date = Date; var origDate = Date;
__magic_mock_date_handler = { var handler = {
construct: function(target, args, newTarget) { construct: function(target, args, newTarget) {
if (args.length > 0) { if (args.length > 0) {
return new (Function.prototype.bind.apply(__original_date, [null].concat(args))); return new (
} else { Function.prototype.bind.apply(origDate, [null].concat(args)));
return new __original_date(__magic_mocked_date_now()); } else {
} return new origDate(mockDateNow());
}, }
get: function(target, property, receiver) { },
if (property == "now") { get: function(target, property, receiver) {
return __magic_mocked_date_now; if (property == "now") {
} return mockDateNow;
}, }
} },
}
Date = new Proxy(Date, __magic_mock_date_handler); Date = new Proxy(Date, handler);
})();
// Mock stack traces. // Mock stack traces.
Error.prepareStackTrace = function (error, structuredStackTrace) { Error.prepareStackTrace = function (error, structuredStackTrace) {
return ""; return "";
} };
// Mock buffer access in float typed arrays because of varying NaN patterns. // Mock buffer access in float typed arrays because of varying NaN patterns.
// Note, for now we just use noop forwarding proxies, because they already // Note, for now we just use noop forwarding proxies, because they already
// turn off optimizations. // turn off optimizations.
function __MockTypedArray(arrayType) { function __MockTypedArray(arrayType) {
array_creation_handler = { var array_creation_handler = {
construct: function(target, args) { construct: function(target, args) {
return new Proxy(new arrayType(args), {}); return new Proxy(new arrayType(args), {});
}, },
...@@ -74,23 +72,25 @@ Float32Array = __MockTypedArray(Float32Array); ...@@ -74,23 +72,25 @@ Float32Array = __MockTypedArray(Float32Array);
Float64Array = __MockTypedArray(Float64Array); Float64Array = __MockTypedArray(Float64Array);
// Mock Worker. // Mock Worker.
var __magic_index_for_mocked_worker = 0 (function () {
// TODO(machenbach): Randomize this for each test case, but keep stable during var index = 0;
// comparison. Also data and random above. // TODO(machenbach): Randomize this for each test case, but keep stable
var __magic_mocked_worker_messages = [ // during comparison. Also data and random above.
undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0} var workerMessages = [
] undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0}
Worker = function(code){ ];
try { Worker = function(code){
__PrettyPrint(eval(code)); try {
} catch(e) { __PrettyPrint(eval(code));
__PrettyPrint(e); } catch(e) {
} __PrettyPrint(e);
this.getMessage = function(){ }
__magic_index_for_mocked_worker = (__magic_index_for_mocked_worker + 1) % 10 this.getMessage = function(){
return __magic_mocked_worker_messages[__magic_index_for_mocked_worker]; index = (index + 1) % 10;
} return workerMessages[index];
this.postMessage = function(msg){ }
__PrettyPrint(msg); this.postMessage = function(msg){
} __PrettyPrint(msg);
} }
};
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment