Debian Codegraph @

closure-library/closure/goog/testing/propertyreplacer_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.PropertyReplacerTest');
16 goog.setTestOnly('goog.testing.PropertyReplacerTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.testing.PropertyReplacer');
21 goog.require('goog.testing.asserts');
22 goog.require('goog.testing.jsunit');
23 goog.require('goog.userAgent.product');
24 goog.require('goog.userAgent.product.isVersion');
25
26
27 function isSafari8() {
28 return goog.userAgent.product.SAFARI &&
29 goog.userAgent.product.isVersion('8.0');
30 }
31
32 // Test PropertyReplacer with JavaScript objects.
33 function testSetJsProperties() {
34 var stubs = new goog.testing.PropertyReplacer();
35 var x = {a: 1, b: undefined};
36
37 // Setting simple values.
38 stubs.set(x, 'num', 1);
39 assertEquals('x.num = 1', 1, x.num);
40 stubs.set(x, 'undef', undefined);
41 assertTrue('x.undef = undefined', 'undef' in x && x.undef === undefined);
42 stubs.set(x, 'null', null);
43 assertTrue('x["null"] = null', x['null'] === null);
44
45 // Setting a simple value that existed originally.
46 stubs.set(x, 'b', null);
47 assertTrue('x.b = null', x.b === null);
48
49 // Setting a complex value.
50 stubs.set(x, 'obj', {});
51 assertEquals('x.obj = {}', 'object', typeof x.obj);
52 stubs.set(x.obj, 'num', 2);
53 assertEquals('x.obj.num = 2', 2, x.obj.num);
54
55 // Overwriting a leaf.
56 stubs.set(x.obj, 'num', 3);
57 assertEquals('x.obj.num = 3', 3, x.obj.num);
58
59 // Overwriting a non-leaf.
60 stubs.set(x, 'obj', {});
61 assertFalse('x.obj = {} again', 'num' in x.obj);
62
63 // Setting a function.
64 stubs.set(x, 'func', function(n) { return n + 1; });
65 assertEquals('x.func = lambda n: n+1', 11, x.func(10));
66
67 // Setting a constructor and a prototype method.
68 stubs.set(x, 'Class', function(num) { this.num = num; });
69 stubs.set(x.Class.prototype, 'triple', function() { return this.num * 3; });
70 assertEquals('prototype method', 12, (new x.Class(4)).triple());
71
72 // Cleaning up with UnsetAll() twice. The second run should have no effect.
73 for (var i = 0; i < 2; i++) {
74 stubs.reset();
75 assertEquals('x.a preserved', 1, x.a);
76 assertTrue('x.b reset', 'b' in x && x.b === undefined);
77 assertFalse('x.num removed', 'num' in x);
78 assertFalse('x.undef removed', 'undef' in x);
79 assertFalse('x["null"] removed', 'null' in x);
80 assertFalse('x.obj removed', 'obj' in x);
81 assertFalse('x.func removed', 'func' in x);
82 assertFalse('x.Class removed', 'Class' in x);
83 }
84 }
85
86 // Test removing JavaScript object properties.
87 function testRemoveJsProperties() {
88 var stubs = new goog.testing.PropertyReplacer();
89 var orig = {'a': 1, 'b': undefined};
90 var x = {'a': 1, 'b': undefined};
91
92 stubs.remove(x, 'a');
93 assertFalse('x.a removed', 'a' in x);
94 assertTrue('x.b not removed', 'b' in x);
95 stubs.reset();
96 assertObjectEquals('x.a reset', x, orig);
97
98 stubs.remove(x, 'b');
99 assertFalse('x.b removed', 'b' in x);
100 stubs.reset();
101 assertObjectEquals('x.b reset', x, orig);
102
103 stubs.set(x, 'c', 2);
104 stubs.remove(x, 'c');
105 assertObjectEquals('x.c added then removed', x, orig);
106 stubs.reset();
107 assertObjectEquals('x.c reset', x, orig);
108
109 stubs.remove(x, 'b');
110 stubs.set(x, 'b', undefined);
111 assertObjectEquals('x.b removed then added', x, orig);
112 stubs.reset();
113 assertObjectEquals('x.b reset', x, orig);
114
115 stubs.remove(x, 'd');
116 assertObjectEquals('removing non-existing key', x, orig);
117 stubs.reset();
118 assertObjectEquals('reset removing non-existing key', x, orig);
119 }
120
121 // Test PropertyReplacer with prototype chain.
122 function testPrototype() {
123 var stubs = new goog.testing.PropertyReplacer();
124
125 // Simple inheritance.
126 var a = {a: 0};
127 function B() {}
128 B.prototype = a;
129 var b = new B();
130
131 stubs.set(a, 0, 1);
132 stubs.set(b, 0, 2);
133 stubs.reset();
134 assertEquals('a.a == 0', 0, a['a']);
135 assertEquals('b.a == 0', 0, b['a']);
136
137 // Inheritance with goog.inherits.
138 var c = {a: 0};
139 function C() {}
140 C.prototype = c;
141 function D() {}
142 goog.inherits(D, C);
143 var d = new D();
144
145 stubs = new goog.testing.PropertyReplacer();
146 stubs.set(c, 'a', 1);
147 stubs.set(d, 'a', 2);
148 stubs.reset();
149 assertEquals('c.a == 0', 0, c['a']);
150 assertEquals('d.a == 0', 0, d['a']);
151
152 // Setting prototype fields.
153 stubs.set(B.prototype, 'c', 'z');
154 assertEquals('b.c=="z"', 'z', b.c);
155 stubs.reset();
156 assertFalse('b.c deleted', 'c' in b);
157
158 // Setting Object.prototype's fields.
159 stubs.set(Object.prototype, 'one', 1);
160 assertEquals('b.one==1', 1, b.one);
161 stubs.reset();
162 assertFalse('b.one deleted', 'one' in b);
163
164 stubs.set(Object.prototype, 'two', 2);
165 stubs.remove(b, 'two');
166 assertEquals('b.two==2', 2, b.two);
167 stubs.remove(Object.prototype, 'two');
168 assertFalse('b.two deleted', 'two' in b);
169 stubs.reset();
170 assertFalse('Object prototype reset', 'two' in b);
171 }
172
173 // Test replacing function properties.
174 function testFunctionProperties() {
175 var stubs = new goog.testing.PropertyReplacer();
176 stubs.set(Array, 'x', 1);
177 assertEquals('Array.x==1', 1, Array.x);
178 stubs.reset();
179 assertFalse('Array.x deleted', 'x' in Array);
180
181 stubs.set(Math.random, 'x', 1);
182 assertEquals('Math.random.x==1', 1, Math.random.x);
183 stubs.reset();
184 assertFalse('Math.random.x deleted', 'x' in Math.random);
185 }
186
187 // Test the hasKey_ private method.
188 function testHasKey() {
189 f = goog.testing.PropertyReplacer.hasKey_;
190
191 assertFalse('{}.a', f({}, 'a'));
192 assertTrue('{a:0}.a', f({a: 0}, 'a'));
193
194 function C() {}
195 C.prototype.a = 0;
196 assertFalse('C.prototype.a set, is C.a own?', f(C, 'a'));
197 assertTrue('C.prototype.a', f(C.prototype, 'a'));
198 assertFalse('C.a not set', f(C, 'a'));
199 C.a = 0;
200 assertTrue('C.a set', f(C, 'a'));
201
202 var c = new C();
203 assertFalse('C().a, inherited', f(c, 'a'));
204 c.a = 0;
205 assertTrue('C().a, own', f(c, 'a'));
206
207 assertFalse('window, invalid key', f(window, 'no such key'));
208 assertTrue('window, global variable', f(window, 'goog'));
209 assertTrue('window, build-in key', f(window, 'location'));
210
211 assertFalse('document, invalid key', f(document, 'no such key'));
212
213 var div = goog.dom.createElement(goog.dom.TagName.DIV);
214 assertFalse('div, invalid key', f(div, 'no such key'));
215 div['a'] = 0;
216 assertTrue('div, key added by JS', f(div, 'a'));
217
218 // hasKey_ returns false for these DOM properties on Safari 8. See b/22044928.
219 if (!isSafari8()) {
220 assertTrue('div.className', f(div, 'className'));
221 assertTrue('document.body', f(document, 'body'));
222 }
223
224 assertFalse('Date().getTime', f(new Date(), 'getTime'));
225 assertTrue('Date.prototype.getTime', f(Date.prototype, 'getTime'));
226
227 assertFalse('Math, invalid key', f(Math, 'no such key'));
228 assertTrue('Math.random', f(Math, 'random'));
229
230 function Parent() {}
231 Parent.prototype.a = 0;
232 function Child() {}
233 goog.inherits(Child, Parent);
234 assertFalse('goog.inherits, parent prototype', f(new Child, 'a'));
235 Child.prototype.a = 1;
236 assertFalse('goog.inherits, child prototype', f(new Child, 'a'));
237
238 function OverwrittenProto() {}
239 OverwrittenProto.prototype = {a: 0};
240 assertFalse(f(new OverwrittenProto, 'a'));
241 }
242
243 // Test PropertyReplacer with DOM objects' built in attributes.
244 function testDomBuiltInAttributes() {
245 var div = goog.dom.createElement(goog.dom.TagName.DIV);
246 div.id = 'old-id';
247
248 var stubs = new goog.testing.PropertyReplacer();
249 stubs.set(div, 'id', 'new-id');
250 stubs.set(div, 'className', 'test-class');
251 assertEquals('div.id == "new-id"', 'new-id', div.id);
252 assertEquals('div.className == "test-class"', 'test-class', div.className);
253
254 stubs.remove(div, 'className');
255
256 // Removal of these DOM properties is not supported in Safari 8. See
257 // b/22044928.
258 if (!isSafari8()) {
259 // '' in Firefox, undefined in Chrome.
260 assertEvaluatesToFalse('div.className is empty', div.className);
261 stubs.reset();
262 assertEquals('div.id == "old-id"', 'old-id', div.id);
263 assertEquals('div.name == ""', '', div.className);
264 }
265 }
266
267 // Test PropertyReplacer with DOM objects' custom attributes.
268 function testDomCustomAttributes() {
269 var div = goog.dom.createElement(goog.dom.TagName.DIV);
270 div.attr1 = 'old';
271
272 var stubs = new goog.testing.PropertyReplacer();
273 stubs.set(div, 'attr1', 'new');
274 stubs.set(div, 'attr2', 'new');
275 assertEquals('div.attr1 == "new"', 'new', div.attr1);
276 assertEquals('div.attr2 == "new"', 'new', div.attr2);
277
278 stubs.set(div, 'attr3', 'new');
279 stubs.remove(div, 'attr3');
280 assertEquals('div.attr3 == undefined', undefined, div.attr3);
281
282 stubs.reset();
283 assertEquals('div.attr1 == "old"', 'old', div.attr1);
284 assertEquals('div.attr2 == undefined', undefined, div.attr2);
285 }
286
287 // Test PropertyReplacer trying to override a read-only property.
288 function testReadOnlyProperties() {
289 var stubs = new goog.testing.PropertyReplacer();
290
291 // Function.prototype.length should be read-only.
292 var foo = function(_) {};
293 assertThrows(
294 'Trying to set a read-only property fails silently.',
295 goog.bind(stubs.set, stubs, foo, 'length', 10));
296 assertThrows(
297 'Trying to replace a read-only property fails silently.',
298 goog.bind(stubs.replace, stubs, foo, 'length', 10));
299
300 // Array length should be undeletable.
301 var a = [1, 2, 3];
302 assertThrows(
303 'Trying to remove a read-only property fails silently.',
304 goog.bind(stubs.remove, stubs, a, 'length'));
305
306 window.foo = foo;
307 assertThrows(
308 'Trying to set a read-only property by path fails silently.',
309 goog.bind(stubs.setPath, stubs, 'window.foo.length', 10));
310 window.foo = undefined;
311 }
312
313 // Test PropertyReplacer trying to override a style property doesn't trigger
314 // read-only exception.
315 function testSettingStyleProperties() {
316 var stubs = new goog.testing.PropertyReplacer();
317
318 var div = document.createElement('div');
319 // Ensures setting a pixel style value doesn't trigger the read-only property
320 // exception, considering div.style.margin will return "0px" instead of just
321 // "0".
322 assertNotThrows(
323 'Trying to set a read-only property fails silently.',
324 goog.bind(stubs.set, stubs, div.style, 'margin', '0'));
325 }
326
327 // Test PropertyReplacer trying to override a sealed property.
328 function testSealedProperties() {
329 if (!goog.isFunction(Object.seal)) {
330 return;
331 }
332
333 var stubs = new goog.testing.PropertyReplacer();
334 var sealed = Object.seal({a: 1});
335 assertThrows(
336 'Trying to set a new sealed property fails silently.',
337 goog.bind(stubs.set, stubs, sealed, 'b', 2));
338 assertNotThrows(
339 'Trying to remove a new sealed property fails.',
340 goog.bind(stubs.remove, stubs, sealed, 'b'));
341 assertNotThrows(
342 'Trying to remove a sealed property fails.',
343 goog.bind(stubs.remove, stubs, sealed, 'a'));
344
345 window.sealed = sealed;
346 assertThrows(
347 'Trying to set a new sealed property by path fails silently in strict ' +
348 'mode.',
349 goog.bind(stubs.setPath, stubs, 'window.sealed.b', 2));
350
351 (function() {
352 // Test Object.seal() in strict mode, where the assignment itself throws the
353 // error rather than our explicit consistency check.
354 'use strict';
355
356 var sealed = Object.seal({a: 1});
357 assertThrows(
358 'Trying to set a new sealed property fails silently in strict mode.',
359 goog.bind(stubs.set, stubs, sealed, 'b', 2));
360 assertNotThrows(
361 'Trying to remove a new sealed property fails in strict mode.',
362 goog.bind(stubs.remove, stubs, sealed, 'b'));
363 assertNotThrows(
364 'Trying to remove a sealed property fails in strict mode.',
365 goog.bind(stubs.remove, stubs, sealed, 'a'));
366
367 window.sealed = sealed;
368 assertThrows(
369 'Trying to set a new sealed property by path fails silently in ' +
370 'strict mode.',
371 goog.bind(stubs.setPath, stubs, 'window.sealed.b', 2));
372 })();
373
374 delete window.sealed;
375 }
376
377 // Test PropertyReplacer trying to override a frozen property.
378 function testFrozenProperty() {
379 if (!goog.isFunction(Object.freeze)) {
380 return;
381 }
382
383 var stubs = new goog.testing.PropertyReplacer();
384 var frozen = Object.freeze({a: 1});
385 assertThrows(
386 'Trying to set a new frozen property fails silently.',
387 goog.bind(stubs.set, stubs, frozen, 'b', 2));
388 assertThrows(
389 'Trying to set a frozen property fails silently.',
390 goog.bind(stubs.set, stubs, frozen, 'a', 2));
391 assertThrows(
392 'Trying to replace a frozen property fails silently.',
393 goog.bind(stubs.replace, stubs, frozen, 'a', 2));
394 assertNotThrows(
395 'Trying to remove a new frozen property fails.',
396 goog.bind(stubs.remove, stubs, frozen, 'b'));
397 assertThrows(
398 'Trying to remove a frozen property fails silently.',
399 goog.bind(stubs.remove, stubs, frozen, 'a'));
400
401
402 window.frozen = frozen;
403 assertThrows(
404 'Trying to set a frozen property by path fails silently.',
405 goog.bind(stubs.setPath, stubs, 'window.frozen.a', 2));
406
407 (function() {
408 // Test Object.freeze() in strict mode, where the assignment itself throws
409 // the error rather than our explicit consistency check.
410 'use strict';
411
412 var frozen = Object.freeze({a: 1});
413 assertThrows(
414 'Trying to set a new frozen property fails silently in strict mode.',
415 goog.bind(stubs.set, stubs, frozen, 'b', 2));
416 assertThrows(
417 'Trying to ovewrite a frozen property fails silently in strict mode.',
418 goog.bind(stubs.set, stubs, frozen, 'a', 2));
419 assertThrows(
420 'Trying to replace a frozen property fails silently in strict mode.',
421 goog.bind(stubs.replace, stubs, frozen, 'a', 2));
422 assertNotThrows(
423 'Trying to remove a new frozen property fails in strict mode.',
424 goog.bind(stubs.remove, stubs, frozen, 'b'));
425 assertThrows(
426 'Trying to remove a frozen property fails silently in strict mode.',
427 goog.bind(stubs.remove, stubs, frozen, 'a'));
428
429 window.frozen = frozen;
430 assertThrows(
431 'Trying to set a new frozen property by path fails silently in ' +
432 'strict mode.',
433 goog.bind(stubs.setPath, stubs, 'window.frozen.b', 2));
434 assertThrows(
435 'Trying to set a frozen property by path fails silently in strict ' +
436 'mode.',
437 goog.bind(stubs.setPath, stubs, 'window.frozen.a', 2));
438 })();
439
440 delete window.frozen;
441 }
442
443 // Test PropertyReplacer overriding Math.random.
444 function testMathRandom() {
445 var stubs = new goog.testing.PropertyReplacer();
446 stubs.set(Math, 'random', function() { return -1; });
447 assertEquals('mocked Math.random', -1, Math.random());
448
449 stubs.reset();
450 assertNotEquals('original Math.random', -1, Math.random());
451 }
452
453 // Tests the replace method of PropertyReplacer.
454 function testReplace() {
455 var stubs = new goog.testing.PropertyReplacer();
456 function C() {
457 this.a = 1;
458 }
459 C.prototype.b = 1;
460 C.prototype.toString = function() { return 'obj'; };
461 var obj = new C();
462
463 stubs.replace(obj, 'a', 2);
464 assertEquals('successfully replaced the own property of an object', 2, obj.a);
465
466 stubs.replace(obj, 'b', 2);
467 assertEquals('successfully replaced the property in the prototype', 2, obj.b);
468
469 var error = assertThrows(
470 'cannot replace missing key',
471 goog.bind(stubs.replace, stubs, obj, 'unknown', 1));
472 // Using assertContains instead of assertEquals because Opera 10.0 adds
473 // the stack trace to the error message.
474 assertEquals(
475 'error message for missing key',
476 'Cannot replace missing property "unknown" in obj', error.message);
477 assertFalse('object not touched', 'unknown' in obj);
478
479 error = assertThrows(
480 'cannot change value type',
481 goog.bind(stubs.replace, stubs, obj, 'a', '3'));
482 assertContains(
483 'error message for type mismatch',
484 'Cannot replace property "a" in obj with a value of different type',
485 error.message);
486
487 assertThrows(
488 'cannot change value type to null',
489 goog.bind(stubs.replace, stubs, obj, 'a', null));
490
491 assertThrows(
492 'cannot change value type to undefined',
493 goog.bind(stubs.replace, stubs, obj, 'a', undefined));
494 }
495
496 function testReplaceAllowNullOrUndefined() {
497 var stubs = new goog.testing.PropertyReplacer();
498 var obj = {value: 1, zero: 0, emptyString: ''};
499
500 stubs.replace(obj, 'value', undefined, true);
501 assertUndefined(
502 'Expected int value to be replaced with undefined', obj.value);
503
504 stubs.replace(obj, 'value', 'b', true);
505 assertEquals(
506 'Expected undefined value to be replace with string', 'b', obj.value);
507
508 stubs.replace(obj, 'value', null, true);
509 assertNull('Expected string value to be replaced with null', obj.value);
510
511 stubs.replace(obj, 'value', 1, true);
512 assertEquals(
513 'Expected null value to be replaced with non-null', 1, obj.value);
514
515 // Replacing 0 with a string or empty string with a number is not allowed.
516 assertThrows(
517 'Cannot change value type',
518 goog.bind(stubs.replace, stubs, obj, 'zero', 'a', true));
519
520 assertThrows(
521 'Cannot change value type',
522 goog.bind(stubs.replace, stubs, obj, 'emptyString', 1, true));
523 }
524
525 // Tests altering complete namespace paths.
526 function testSetPath() {
527 goog.global.a = {b: {}};
528 var stubs = new goog.testing.PropertyReplacer();
529
530 stubs.setPath('a.b.c.d', 1);
531 assertObjectEquals('a.b.c.d=1', {b: {c: {d: 1}}}, goog.global.a);
532 stubs.setPath('a.b.e', 2);
533 assertObjectEquals('a.b.e=2', {b: {c: {d: 1}, e: 2}}, goog.global.a);
534 stubs.setPath('a.f', 3);
535 assertObjectEquals('a.f=3', {b: {c: {d: 1}, e: 2}, f: 3}, goog.global.a);
536 stubs.setPath('a.f.g', 4);
537 assertObjectEquals(
538 'a.f.g=4', {b: {c: {d: 1}, e: 2}, f: {g: 4}}, goog.global.a);
539 stubs.setPath('a', 5);
540 assertEquals('a=5', 5, goog.global.a);
541
542 stubs.setPath('x.y.z', 5);
543 assertObjectEquals('x.y.z=5', {y: {z: 5}}, goog.global.x);
544
545 stubs.reset();
546 assertObjectEquals('a.* reset', {b: {}}, goog.global.a);
547 // NOTE: it's impossible to delete global variables in Internet Explorer,
548 // so ('x' in goog.global) would be true.
549 assertUndefined('x.* reset', goog.global.x);
550 }
551
552 // Tests altering paths with functions in them.
553 function testSetPathWithFunction() {
554 var f = function() {};
555 goog.global.a = {b: f};
556 var stubs = new goog.testing.PropertyReplacer();
557
558 stubs.setPath('a.b.c', 1);
559 assertEquals('a.b.c=1, f kept', f, goog.global.a.b);
560 assertEquals('a.b.c=1, c set', 1, goog.global.a.b.c);
561
562 stubs.setPath('a.b.prototype.d', 2);
563 assertEquals('a.b.prototype.d=2, b kept', f, goog.global.a.b);
564 assertEquals('a.b.prototype.d=2, c kept', 1, goog.global.a.b.c);
565 assertFalse('a.b.prototype.d=2, a.b.d not set', 'd' in goog.global.a.b);
566 assertTrue('a.b.prototype.d=2, proto set', 'd' in goog.global.a.b.prototype);
567 assertEquals('a.b.prototype.d=2, d set', 2, new goog.global.a.b().d);
568
569 var invalidSetPath = function() { stubs.setPath('a.prototype.e', 3); };
570 assertThrows('setting the prototype of a non-function', invalidSetPath);
571
572 stubs.reset();
573 assertObjectEquals('a.b.c reset', {b: f}, goog.global.a);
574 assertObjectEquals('a.b.prototype reset', {}, goog.global.a.b.prototype);
575 }
576
577 // Tests restoring original attribute values with restore() rather than reset().
578 function testRestore() {
579 var stubs = new goog.testing.PropertyReplacer();
580 var x = {a: 1, b: undefined};
581
582 // Setting simple value.
583 stubs.set(x, 'num', 1);
584 assertEquals('x.num = 1', 1, x.num);
585 stubs.restore(x, 'num');
586 assertFalse('x.num removed', 'num' in x);
587
588 // Setting undefined value.
589 stubs.set(x, 'undef', undefined);
590 assertTrue('x.undef = undefined', 'undef' in x && x.undef === undefined);
591 stubs.restore(x, 'undef');
592 assertFalse('x.undef removed', 'undef' in x);
593
594 // Setting null value.
595 stubs.set(x, 'null', null);
596 assertTrue('x["null"] = null', x['null'] === null);
597 stubs.restore(x, 'null');
598 assertFalse('x["null"] removed', 'null' in x);
599
600 // Setting a simple value that existed originally.
601 stubs.set(x, 'b', null);
602 assertTrue('x.b = null', x.b === null);
603
604 // Setting a complex value.
605 stubs.set(x, 'obj', {});
606 assertEquals('x.obj = {}', 'object', typeof x.obj);
607 stubs.set(x.obj, 'num', 2);
608 assertEquals('x.obj.num = 2', 2, x.obj.num);
609 stubs.restore(x.obj, 'num');
610 assertFalse('x.obj.num removed', 'num' in x.obj);
611 stubs.restore(x, 'obj');
612 assertFalse('x.obj removed', 'obj' in x);
613
614 // Setting a function.
615 stubs.set(x, 'func', function(n) { return n + 1; });
616 assertEquals('x.func = lambda n: n+1', 11, x.func(10));
617 stubs.restore(x, 'func');
618 assertFalse('x.func removed', 'func' in x);
619
620 // Setting a constructor and a prototype method.
621 stubs.set(x, 'Class', function(num) { this.num = num; });
622 stubs.set(x.Class.prototype, 'triple', function() { return this.num * 3; });
623 assertEquals('prototype method', 12, (new x.Class(4)).triple());
624 stubs.restore(x, 'Class');
625 assertFalse('x.Class removed', 'Class' in x);
626
627 // Final cleanup with reset(). This should have no effect:
628 // all assertions about the original state shall still hold.
629 stubs.reset();
630 assertEquals('x.a preserved', 1, x.a);
631 assertTrue('x.b reset', 'b' in x && x.b === undefined);
632 assertFalse('x.num removed', 'num' in x);
633 assertFalse('x.undef removed', 'undef' in x);
634 assertFalse('x["null"] removed', 'null' in x);
635 assertFalse('x.obj removed', 'obj' in x);
636 assertFalse('x.func removed', 'func' in x);
637 assertFalse('x.Class removed', 'Class' in x);
638 }
639
640 // Tests restore() with invalid arguments.
641 function testRestoreWithInvalidArguments() {
642 var stubs = new goog.testing.PropertyReplacer();
643 var x = {a: 1, b: undefined};
644 var y = {a: 1};
645
646 stubs.set(x, 'a', 42);
647
648 assertThrows(
649 'Trying to restore state of an unmodified property',
650 goog.bind(stubs.restore, stubs, x, 'b'));
651 assertThrows(
652 'Trying to restore state of a non-existing property',
653 goog.bind(stubs.restore, stubs, x, 'not_here'));
654 assertThrows(
655 'Trying to restore state of an unmodified object',
656 goog.bind(stubs.restore, stubs, y, 'a'));
657 }