aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/propertyreplacer_test.html
blob: 6ad2eff400a1b51d61d91fd073c333a4d5095b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.testing.PropertyReplacer</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.asserts');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

// Test PropertyReplacer with JavaScript objects.
function testSetJsProperties() {
  var stubs = new goog.testing.PropertyReplacer();
  var x = {a: 1, b: undefined};

  // Setting simple values.
  stubs.set(x, 'num', 1);
  assertEquals('x.num = 1', 1, x.num);
  stubs.set(x, 'undef', undefined);
  assertTrue('x.undef = undefined', 'undef' in x && x.undef === undefined);
  stubs.set(x, 'null', null);
  assertTrue('x["null"] = null', x['null'] === null);

  // Setting a simple value that existed originally.
  stubs.set(x, 'b', null);
  assertTrue('x.b = null', x.b === null);

  // Setting a complex value.
  stubs.set(x, 'obj', {});
  assertEquals('x.obj = {}', 'object', typeof x.obj);
  stubs.set(x.obj, 'num', 2);
  assertEquals('x.obj.num = 2', 2, x.obj.num);

  // Overwriting a leaf.
  stubs.set(x.obj, 'num', 3);
  assertEquals('x.obj.num = 3', 3, x.obj.num);

  // Overwriting a non-leaf.
  stubs.set(x, 'obj', {});
  assertFalse('x.obj = {} again', 'num' in x.obj);

  // Setting a function.
  stubs.set(x, 'func', function(n) { return n + 1; });
  assertEquals('x.func = lambda n: n+1', 11, x.func(10));

  // Setting a constructor and a prototype method.
  stubs.set(x, 'Class', function(num) { this.num = num; });
  stubs.set(x.Class.prototype, 'triple', function() { return this.num * 3; });
  assertEquals('prototype method', 12, (new x.Class(4)).triple());

  // Cleaning up with UnsetAll() twice. The second run should have no effect.
  for (var i = 0; i < 2; i++) {
    stubs.reset();
    assertEquals('x.a preserved', 1, x.a);
    assertTrue('x.b reset', 'b' in x && x.b === undefined);
    assertFalse('x.num removed', 'num' in x);
    assertFalse('x.undef removed', 'undef' in x);
    assertFalse('x["null"] removed', 'null' in x);
    assertFalse('x.obj removed', 'obj' in x);
    assertFalse('x.func removed', 'func' in x);
    assertFalse('x.Class removed', 'Class' in x);
  }
}

// Test removing JavaScript object properties.
function testRemoveJsProperties() {
  var stubs = new goog.testing.PropertyReplacer();
  var orig = {'a': 1, 'b': undefined};
  var x = {'a': 1, 'b': undefined};

  stubs.remove(x, 'a');
  assertFalse('x.a removed', 'a' in x);
  assertTrue('x.b not removed', 'b' in x);
  stubs.reset();
  assertObjectEquals('x.a reset', x, orig);

  stubs.remove(x, 'b');
  assertFalse('x.b removed', 'b' in x);
  stubs.reset();
  assertObjectEquals('x.b reset', x, orig);

  stubs.set(x, 'c', 2);
  stubs.remove(x, 'c');
  assertObjectEquals('x.c added then removed', x, orig);
  stubs.reset();
  assertObjectEquals('x.c reset', x, orig);

  stubs.remove(x, 'b');
  stubs.set(x, 'b', undefined);
  assertObjectEquals('x.b removed then added', x, orig);
  stubs.reset();
  assertObjectEquals('x.b reset', x, orig);

  stubs.remove(x, 'd');
  assertObjectEquals('removing non-existing key', x, orig);
  stubs.reset();
  assertObjectEquals('reset removing non-existing key', x, orig);
}

// Test PropertyReplacer with prototype chain.
function testPrototype() {
  var stubs = new goog.testing.PropertyReplacer();

  // Simple inheritance.
  var a = {a: 0};
  function B() {};
  B.prototype = a;
  var b = new B();

  stubs.set(a, 0, 1);
  stubs.set(b, 0, 2);
  stubs.reset();
  assertEquals('a.a == 0', 0, a['a']);
  assertEquals('b.a == 0', 0, b['a']);

  // Inheritance with goog.inherits.
  var c = {a: 0};
  function C() {};
  C.prototype = c;
  function D() {};
  goog.inherits(D, C);
  var d = new D();

  var stubs = new goog.testing.PropertyReplacer();
  stubs.set(c, 'a', 1);
  stubs.set(d, 'a', 2);
  stubs.reset();
  assertEquals('c.a == 0', 0, c['a']);
  assertEquals('d.a == 0', 0, d['a']);

  // Setting prototype fields.
  stubs.set(B.prototype, 'c', 'z');
  assertEquals('b.c=="z"', 'z', b.c);
  stubs.reset();
  assertFalse('b.c deleted', 'c' in b);

  // Setting Object.prototype's fields.
  stubs.set(Object.prototype, 'one', 1);
  assertEquals('b.one==1', 1, b.one);
  stubs.reset();
  assertFalse('b.one deleted', 'one' in b);

  stubs.set(Object.prototype, 'two', 2);
  stubs.remove(b, 'two');
  assertEquals('b.two==2', 2, b.two);
  stubs.remove(Object.prototype, 'two');
  assertFalse('b.two deleted', 'two' in b);
  stubs.reset();
  assertFalse('Object prototype reset', 'two' in b);
}

// Test replacing function properties.
function testFunctionProperties() {
  var stubs = new goog.testing.PropertyReplacer();
  stubs.set(Array, 'x', 1);
  assertEquals('Array.x==1', 1, Array.x);
  stubs.reset();
  assertFalse('Array.x deleted', 'x' in Array);

  stubs.set(Math.random, 'x', 1);
  assertEquals('Math.random.x==1', 1, Math.random.x);
  stubs.reset();
  assertFalse('Math.random.x deleted', 'x' in Math.random);
}

// Test the hasKey_ private method.
function testHasKey() {
  f = goog.testing.PropertyReplacer.hasKey_;

  assertFalse('{}.a', f({}, 'a'));
  assertTrue('{a:0}.a', f({a: 0}, 'a'));

  function C() {};
  C.prototype.a = 0;
  assertFalse('C.prototype.a set, is C.a own?', f(C, 'a'));
  assertTrue('C.prototype.a', f(C.prototype, 'a'));
  assertFalse('C.a not set', f(C, 'a'));
  C.a = 0;
  assertTrue('C.a set', f(C, 'a'));

  var c = new C();
  assertFalse('C().a, inherited', f(c, 'a'));
  c.a = 0;
  assertTrue('C().a, own', f(c, 'a'));

  assertFalse('window, invalid key', f(window, 'no such key'));
  assertTrue('window, global variable', f(window, 'goog'));
  assertTrue('window, build-in key', f(window, 'location'))

  assertFalse('document, invalid key', f(document, 'no such key'));
  assertTrue('document.body', f(document, 'body'));

  var div = document.createElement('div');
  assertFalse('div, invalid key', f(div, 'no such key'));
  assertTrue('div.className', f(div, 'className'));
  div['a'] = 0;
  assertTrue('div, key added by JS', f(div, 'a'));

  assertFalse('Date().getTime', f(new Date(), 'getTime'));
  assertTrue('Date.prototype.getTime', f(Date.prototype, 'getTime'));

  assertFalse('Math, invalid key', f(Math, 'no such key'));
  assertTrue('Math.random', f(Math, 'random'));

  function Parent() {};
  Parent.prototype.a = 0;
  function Child() {};
  goog.inherits(Child, Parent);
  assertFalse('goog.inherits, parent prototype', f(new Child, 'a'));
  Child.prototype.a = 1;
  assertFalse('goog.inherits, child prototype', f(new Child, 'a'));

  function OverwrittenProto() {};
  OverwrittenProto.prototype = {a: 0};
  assertFalse(f(new OverwrittenProto, 'a'));
}

// Test PropertyReplacer with DOM objects' built in attributes.
function testDomBuiltInAttributes() {
  var div = document.createElement('div');
  div.id = 'old-id';

  var stubs = new goog.testing.PropertyReplacer();
  stubs.set(div, 'id', 'new-id');
  stubs.set(div, 'className', 'test-class');
  assertEquals('div.id == "new-id"', 'new-id', div.id);
  assertEquals('div.className == "test-class"', 'test-class', div.className);

  stubs.remove(div, 'className');
  // '' in Firefox, undefined in Chrome.
  assertEvaluatesToFalse('div.className is empty', div.className);

  stubs.reset();
  assertEquals('div.id == "old-id"', 'old-id', div.id);
  assertEquals('div.name == ""', '', div.className);
}

// Test PropertyReplacer with DOM objects' custom attributes.
function testDomCustomAttributes() {
  var div = document.createElement('div');
  div.attr1 = 'old';

  var stubs = new goog.testing.PropertyReplacer();
  stubs.set(div, 'attr1', 'new');
  stubs.set(div, 'attr2', 'new');
  assertEquals('div.attr1 == "new"', 'new', div.attr1);
  assertEquals('div.attr2 == "new"', 'new', div.attr2);

  stubs.set(div, 'attr3', 'new');
  stubs.remove(div, 'attr3');
  assertEquals('div.attr3 == undefined', undefined, div.attr3);

  stubs.reset();
  assertEquals('div.attr1 == "old"', 'old', div.attr1);
  assertEquals('div.attr2 == undefined', undefined, div.attr2);
}

// Test PropertyReplacer overriding Math.random.
function testMathRandom() {
  var stubs = new goog.testing.PropertyReplacer();
  stubs.set(Math, 'random', function() { return -1; });
  assertEquals('mocked Math.random', -1, Math.random());

  stubs.reset();
  assertNotEquals('original Math.random', -1, Math.random());
}

// Tests the replace method of PropertyReplacer.
function testReplace() {
  var stubs = new goog.testing.PropertyReplacer();
  function C() {
    this.a = 1;
  };
  C.prototype.b = 1;
  C.prototype.toString = function() {
    return 'obj';
  };
  var obj = new C();

  stubs.replace(obj, 'a', 2);
  assertEquals('successfully replaced the own property of an object', 2, obj.a);

  stubs.replace(obj, 'b', 2);
  assertEquals('successfully replaced the property in the prototype', 2, obj.b);

  var error = assertThrows('cannot replace missing key',
      goog.bind(stubs.replace, stubs, obj, 'unknown', 1));
  // Using assertContains instead of assertEquals because Opera 10.0 adds
  // the stack trace to the error message.
  assertEquals('error message for missing key',
      'Cannot replace missing property "unknown" in obj', error.message);
  assertFalse('object not touched', 'unknown' in obj);

  var error = assertThrows('cannot change value type',
      goog.bind(stubs.replace, stubs, obj, 'a', '3'));
  assertContains('error message for type mismatch',
      'Cannot replace property "a" in obj with a value of different type',
      error.message);
}

// Tests altering complete namespace paths.
function testSetPath() {
  goog.global.a = {b: {}};
  var stubs = new goog.testing.PropertyReplacer();

  stubs.setPath('a.b.c.d', 1);
  assertObjectEquals('a.b.c.d=1', {b: {c: {d: 1}}}, goog.global.a);
  stubs.setPath('a.b.e', 2);
  assertObjectEquals('a.b.e=2', {b: {c: {d: 1}, e: 2}}, goog.global.a);
  stubs.setPath('a.f', 3);
  assertObjectEquals('a.f=3', {b: {c: {d: 1}, e: 2}, f: 3}, goog.global.a);
  stubs.setPath('a.f.g', 4);
  assertObjectEquals('a.f.g=4', {b: {c: {d: 1}, e: 2}, f: {g: 4}},
                     goog.global.a);
  stubs.setPath('a', 5);
  assertEquals('a=5', 5, goog.global.a);

  stubs.setPath('x.y.z', 5);
  assertObjectEquals('x.y.z=5', {y: {z: 5}}, goog.global.x);

  stubs.reset();
  assertObjectEquals('a.* reset', {b: {}}, goog.global.a);
  // NOTE: it's impossible to delete global variables in Internet Explorer,
  // so ('x' in goog.global) would be true.
  assertUndefined('x.* reset', goog.global.x);
}

// Tests altering paths with functions in them.
function testSetPathWithFunction() {
  var f = function() {};
  goog.global.a = {b: f};
  var stubs = new goog.testing.PropertyReplacer();

  stubs.setPath('a.b.c', 1);
  assertEquals('a.b.c=1, f kept', f, goog.global.a.b);
  assertEquals('a.b.c=1, c set', 1, goog.global.a.b.c);

  stubs.setPath('a.b.prototype.d', 2);
  assertEquals('a.b.prototype.d=2, b kept', f, goog.global.a.b);
  assertEquals('a.b.prototype.d=2, c kept', 1, goog.global.a.b.c);
  assertFalse('a.b.prototype.d=2, a.b.d not set', 'd' in goog.global.a.b);
  assertTrue('a.b.prototype.d=2, proto set', 'd' in goog.global.a.b.prototype);
  assertEquals('a.b.prototype.d=2, d set', 2, new goog.global.a.b().d);

  var invalidSetPath = function() {
    stubs.setPath('a.prototype.e', 3);
  };
  assertThrows('setting the prototype of a non-function', invalidSetPath);

  stubs.reset();
  assertObjectEquals('a.b.c reset', {b: f}, goog.global.a);
  assertObjectEquals('a.b.prototype reset', {}, goog.global.a.b.prototype);
}

</script>
</body>
</html>