Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: tools/foozzie/v8_mock.js

Issue 2650353004: [foozzie] Fix mock variables that leaked into the global object (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
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 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This is intended for permanent JS behavior changes for mocking out 5 // This is intended for permanent JS behavior changes for mocking out
6 // non-deterministic behavior. For temporary suppressions, please refer to 6 // non-deterministic behavior. For temporary suppressions, please refer to
7 // v8_suppressions.js. 7 // v8_suppressions.js.
8 // This file is loaded before each correctness test cases and won't get 8 // This file is loaded before each correctness test cases and won't get
9 // minimized. 9 // minimized.
10 10
11 11
12 // This will be overridden in the test cases. The override can be minimized. 12 // This will be overridden in the test cases. The override can be minimized.
13 var __PrettyPrint = function __PrettyPrint(msg) { print(msg); }; 13 var __PrettyPrint = function __PrettyPrint(msg) { print(msg); };
14 14
15
16 // All calls to f.arguments are replaced by f.mock_arguments by an external
17 // script.
18 Object.prototype.mock_arguments = ['x', 'y']
Michael Achenbach 2017/01/25 14:25:44 This is unused. The 2-3 test cases that lead to f.
19
20
21 // Mock Math.random. 15 // Mock Math.random.
22 var __magic_index_for_mocked_random = 0 16 (function () {
23 Math.random = function(){ 17 var index = 0
Michael Achenbach 2017/01/25 14:25:44 I shortened all these cryptic variable names.
24 __magic_index_for_mocked_random = (__magic_index_for_mocked_random + 1) % 10 18 Math.random = function() {
25 return __magic_index_for_mocked_random / 10.0; 19 index = (index + 1) % 10;
26 } 20 return index / 10.0;
27 21 }
22 })();
28 23
29 // Mock Date. 24 // Mock Date.
30 var __magic_index_for_mocked_date = 0 25 (function () {
31 var __magic_mocked_date = 1477662728696 26 var index = 0
32 __magic_mocked_date_now = function(){ 27 var mockDate = 1477662728696
33 __magic_index_for_mocked_date = (__magic_index_for_mocked_date + 1) % 10 28 var mockDateNow = function() {
34 __magic_mocked_date = __magic_mocked_date + __magic_index_for_mocked_date + 1 29 index = (index + 1) % 10
35 return __magic_mocked_date 30 mockDate = mockDate + index + 1
36 } 31 return mockDate
32 }
37 33
38 var __original_date = Date; 34 var origDate = Date;
39 __magic_mock_date_handler = { 35 var handler = {
40 construct: function(target, args, newTarget) { 36 construct: function(target, args, newTarget) {
41 if (args.length > 0) { 37 if (args.length > 0) {
42 return new (Function.prototype.bind.apply(__original_date, [null].concat(a rgs))); 38 return new (
43 } else { 39 Function.prototype.bind.apply(origDate, [null].concat(args)));
44 return new __original_date(__magic_mocked_date_now()); 40 } else {
45 } 41 return new origDate(mockDateNow());
46 }, 42 }
47 get: function(target, property, receiver) { 43 },
48 if (property == "now") { 44 get: function(target, property, receiver) {
49 return __magic_mocked_date_now; 45 if (property == "now") {
50 } 46 return mockDateNow;
51 }, 47 }
52 } 48 },
49 }
53 50
54 Date = new Proxy(Date, __magic_mock_date_handler); 51 Date = new Proxy(Date, handler);
52 })();
55 53
56 // Mock stack traces. 54 // Mock stack traces.
57 Error.prepareStackTrace = function (error, structuredStackTrace) { 55 Error.prepareStackTrace = function (error, structuredStackTrace) {
58 return ""; 56 return "";
59 } 57 };
60 58
61 // Mock buffer access in float typed arrays because of varying NaN patterns. 59 // Mock buffer access in float typed arrays because of varying NaN patterns.
62 // Note, for now we just use noop forwarding proxies, because they already 60 // Note, for now we just use noop forwarding proxies, because they already
63 // turn off optimizations. 61 // turn off optimizations.
64 function __MockTypedArray(arrayType) { 62 function __MockTypedArray(arrayType) {
65 array_creation_handler = { 63 var array_creation_handler = {
66 construct: function(target, args) { 64 construct: function(target, args) {
67 return new Proxy(new arrayType(args), {}); 65 return new Proxy(new arrayType(args), {});
68 }, 66 },
69 }; 67 };
70 return new Proxy(arrayType, array_creation_handler); 68 return new Proxy(arrayType, array_creation_handler);
71 } 69 }
72 70
73 Float32Array = __MockTypedArray(Float32Array); 71 Float32Array = __MockTypedArray(Float32Array);
74 Float64Array = __MockTypedArray(Float64Array); 72 Float64Array = __MockTypedArray(Float64Array);
75 73
76 // Mock Worker. 74 // Mock Worker.
77 var __magic_index_for_mocked_worker = 0 75 (function () {
78 // TODO(machenbach): Randomize this for each test case, but keep stable during 76 var index = 0;
79 // comparison. Also data and random above. 77 // TODO(machenbach): Randomize this for each test case, but keep stable
80 var __magic_mocked_worker_messages = [ 78 // during comparison. Also data and random above.
81 undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0} 79 var workerMessages = [
82 ] 80 undefined, 0, -1, "", "foo", 42, [], {}, [0], {"x": 0}
83 Worker = function(code){ 81 ];
84 try { 82 Worker = function(code){
85 __PrettyPrint(eval(code)); 83 try {
86 } catch(e) { 84 __PrettyPrint(eval(code));
87 __PrettyPrint(e); 85 } catch(e) {
88 } 86 __PrettyPrint(e);
89 this.getMessage = function(){ 87 }
90 __magic_index_for_mocked_worker = (__magic_index_for_mocked_worker + 1) % 10 88 this.getMessage = function(){
91 return __magic_mocked_worker_messages[__magic_index_for_mocked_worker]; 89 index = (index + 1) % 10;
92 } 90 return workerMessages[index];
93 this.postMessage = function(msg){ 91 }
94 __PrettyPrint(msg); 92 this.postMessage = function(msg){
95 } 93 __PrettyPrint(msg);
96 } 94 }
95 };
96 })();
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 408576698