-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
131 lines (102 loc) · 3.07 KB
/
test.js
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
'use strict';
require('mocha');
var assert = require('assert');
var getter = require('./');
describe('set-getter', function() {
it('should export a function', function() {
assert.equal(typeof getter, 'function');
});
it('should set a getter on an object', function() {
var obj = {};
getter(obj, 'foo', function() {
return 'bar';
});
assert('foo' in obj);
assert.equal(typeof obj.foo, 'string');
assert.equal(obj.foo, 'bar');
});
it('should only call the getter once', function() {
var calls = 0;
var cache = {}, obj = {};
getter(obj, 'foo', function() {
if (cache.foo) return cache.foo;
calls++;
return (cache.foo = 'bar');
});
assert.equal(calls, 0);
assert('foo' in obj);
assert.equal(calls, 0);
assert.equal(typeof obj.foo, 'string');
assert.equal(calls, 1);
assert.equal(obj.foo, 'bar');
assert.equal(calls, 1);
});
it('should expose the object as "this" in the getter', function() {
var obj = {abc: 'xyz'};
getter(obj, 'foo', function() {
assert.equal(this.abc, 'xyz');
return 'bar';
});
assert('foo' in obj);
assert.equal(typeof obj.foo, 'string');
assert.equal(obj.foo, 'bar');
});
it('should add getter to an object hierarchy using dot notation', function() {
var obj = {};
getter(obj, 'foo.bar', function() {
return 'beep';
});
getter(obj, 'foo.baz', function() {
return 'boop';
});
assert('foo' in obj);
assert.equal(typeof obj.foo, 'object');
assert('bar' in obj.foo);
assert('baz' in obj.foo);
assert.equal(typeof obj.foo.bar, 'string');
assert.equal(typeof obj.foo.baz, 'string');
assert.equal(obj.foo.bar, 'beep');
assert.equal(obj.foo.baz, 'boop');
});
it('should add getter to an object hierarchy using array notation', function() {
var obj = {};
getter(obj, ['foo', 'bar'], function() {
return 'beep';
});
getter(obj, ['foo', 'baz'], function() {
return 'boop';
});
assert('foo' in obj);
assert.equal(typeof obj.foo, 'object');
assert('bar' in obj.foo);
assert('baz' in obj.foo);
assert.equal(typeof obj.foo.bar, 'string');
assert.equal(typeof obj.foo.baz, 'string');
assert.equal(obj.foo.bar, 'beep');
assert.equal(obj.foo.baz, 'boop');
});
it('should not pollute the prototype when using dot notation', function() {
var obj = {};
assert(!('polluted' in {}));
assert(!('polluted' in obj));
getter(obj, '__proto__.polluted', function() {
return true;
});
assert(!('polluted' in {}));
assert('polluted' in obj);
assert({}.polluted !== true);
assert(obj.polluted === true);
});
it('should not pollute the prototype when using array notation', function() {
var obj = {};
assert(!('polluted' in {}));
assert(!('polluted' in obj));
getter(obj, ['__proto__', 'polluted'], function() {
return true;
});
assert(!('polluted' in {}));
assert('polluted' in obj);
assert({}.polluted !== true);
assert(obj.polluted === true);
});
});