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 @@
// This will be overridden in the test cases. The override can be minimized.
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.
var __magic_index_for_mocked_random = 0
Math.random = function(){
__magic_index_for_mocked_random = (__magic_index_for_mocked_random + 1) % 10
return __magic_index_for_mocked_random / 10.0;
}
(function () {
var index = 0
Math.random = function() {
index = (index + 1) % 10;
return index / 10.0;
}
})();
// Mock Date.
var __magic_index_for_mocked_date = 0
var __magic_mocked_date = 1477662728696
__magic_mocked_date_now = function(){
__magic_index_for_mocked_date = (__magic_index_for_mocked_date + 1) % 10
__magic_mocked_date = __magic_mocked_date + __magic_index_for_mocked_date + 1
return __magic_mocked_date
}
(function () {
var index = 0
var mockDate = 1477662728696
var mockDateNow = function() {
index = (index + 1) % 10
mockDate = mockDate + index + 1
return mockDate
}
var __original_date = Date;
__magic_mock_date_handler = {
construct: function(target, args, newTarget) {
if (args.length > 0) {
return new (Function.prototype.bind.apply(__original_date, [null].concat(args)));
} else {
return new __original_date(__magic_mocked_date_now());
}
},
get: function(target, property, receiver) {
if (property == "now") {
return __magic_mocked_date_now;
}
},
}
var origDate = Date;
var handler = {
construct: function(target, args, newTarget) {
if (args.length > 0) {
return new (
Function.prototype.bind.apply(origDate, [null].concat(args)));
} else {
return new origDate(mockDateNow());
}
},
get: function(target, property, receiver) {
if (property == "now") {
return mockDateNow;
}
},
}
Date = new Proxy(Date, __magic_mock_date_handler);
Date = new Proxy(Date, handler);
})();
// Mock stack traces.
Error.prepareStackTrace = function (error, structuredStackTrace) {
return "";
}
};
// Mock buffer access in float typed arrays because of varying NaN patterns.
// Note, for now we just use noop forwarding proxies, because they already
// turn off optimizations.
function __MockTypedArray(arrayType) {
array_creation_handler = {
var array_creation_handler = {
construct: function(target, args) {
return new Proxy(new arrayType(args), {});
},
......@@ -74,23 +72,25 @@ Float32Array = __MockTypedArray(Float32Array);
Float64Array = __MockTypedArray(Float64Array);
// Mock Worker.
var __magic_index_for_mocked_worker = 0
// TODO(machenbach): Randomize this for each test case, but keep stable during
// comparison. Also data and random above.
var __magic_mocked_worker_messages = [
undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0}
]
Worker = function(code){
try {
__PrettyPrint(eval(code));
} catch(e) {
__PrettyPrint(e);
}
this.getMessage = function(){
__magic_index_for_mocked_worker = (__magic_index_for_mocked_worker + 1) % 10
return __magic_mocked_worker_messages[__magic_index_for_mocked_worker];
}
this.postMessage = function(msg){
__PrettyPrint(msg);
}
}
(function () {
var index = 0;
// TODO(machenbach): Randomize this for each test case, but keep stable
// during comparison. Also data and random above.
var workerMessages = [
undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0}
];
Worker = function(code){
try {
__PrettyPrint(eval(code));
} catch(e) {
__PrettyPrint(e);
}
this.getMessage = function(){
index = (index + 1) % 10;
return workerMessages[index];
}
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