-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtests.js
249 lines (215 loc) · 7.21 KB
/
tests.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
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
/*global describe:false, it:false, beforeEach:false */
var Bro = require('./brototype');
var assert = require('assert');
describe('Bro.doYouEven', function() {
it('should be defined', function() {
var a = {},
bro = Bro(a);
assert.notEqual(bro.doYouEven, undefined);
});
it('should return true for defined properties', function() {
var a = {foo: 'bar'},
bro = Bro(a);
assert.equal(bro.doYouEven('foo'), true);
});
it('should return true for nested properties', function() {
var a = {foo: {bar: 'baz'}},
bro = Bro(a);
assert.equal(bro.doYouEven('foo.bar'), true);
});
it('should return true for more than one nested property', function() {
var a = {b: {c: 'foo'},d: {e: 'bar'}},
bro = Bro(a);
assert.equal(bro.doYouEven(['b.c', 'd.x']), false);
assert.equal(bro.doYouEven(['b.c', 'd.e']), true);
});
it('should return false for undefined properties', function() {
var a = {foo: 'bar'},
bro = Bro(a);
assert.equal(bro.doYouEven('bar'), false);
});
it('should fail gracefully if the object is not defined', function() {
var a = undefined,
bro = Bro(a);
assert.equal(bro.doYouEven('foo.bar'), false);
});
it('should fail gracefully if the object is null', function() {
var a = null,
bro = Bro(a);
assert.equal(bro.doYouEven('foo.bar'), false);
});
it('should fail gracefully if a traversed subproperty is null', function(){
var a = {test: null},
bro = Bro(a);
assert.equal(bro.doYouEven('test.0.test'), false);
});
it('should pass a simple callback function', function() {
var a = {foo: 'bar'},
bro = Bro(a);
bro.doYouEven('foo', function(prop) {
assert.equal(prop, 'bar');
});
});
it('should pass callback function to more than one nested property', function() {
var a = {foo: 'bar', a: {b: 'c'}},
bro = Bro(a);
bro.doYouEven(['foo', 'b.c'], function(prop, key) {
assert.equal(prop, a[key]);
});
});
});
describe('Bro.iCanHaz', function() {
it('should return the value of the deep property', function() {
var a = {b: {c: {d: 32}}},
bro = Bro(a);
assert.equal(bro.iCanHaz('b.c.d'), 32);
});
it('should return undefined for missing property', function() {
var a = {b: 32},
bro = Bro(a);
assert.equal(bro.iCanHaz('b.c.d'), undefined);
});
it('should return an array when an array is requested', function() {
var a = {a: 'foo', b: 'bar', c: 'fred'},
values = Bro(a).iCanHaz(['a', 'b', 'c', 'd']);
assert.notEqual(values.indexOf('foo'), -1);
assert.notEqual(values.indexOf('bar'), -1);
assert.notEqual(values.indexOf('fred'), -1);
});
});
describe('Bro.giveMeProps', function() {
it('should return an object\'s keys', function() {
var a = {
"foo": 1,
"bar": 2
},
keys = Bro(a).giveMeProps();
assert.equal(keys.length, 2);
assert.notEqual(keys.indexOf('foo'), -1);
assert.notEqual(keys.indexOf('bar'), -1);
});
});
describe('Bro.comeAtMe', function() {
it('should extend first object with second object', function() {
var a = {
"foo": 1,
"bar": 2
},b = {
"bar": 3,
"baz": function(){return false;}
};
Bro(a).comeAtMe(b);
assert.equal(a.foo, 1);
assert.equal(a.bar, 3);
assert.equal(a.baz(), false);
});
});
describe('Bro.iDontAlways', function() {
var fired,
success,
param,
context,
obj = {
"foo": function() {
fired = true;
context = this;
return 91;
},
"bar": 3
},
fn = function(p) {
success = true;
param = p;
};
beforeEach(function() {
fired = false;
success = false;
param = null;
context = null;
});
it('should check that the requested method is a function', function() {
var bro = Bro(obj);
bro.iDontAlways('bar').butWhenIdo(fn);
assert.equal(success, false);
bro.iDontAlways('foo').butWhenIdo(fn);
assert.equal(success, true);
});
it('should run the requested method if a function', function() {
var bro = Bro(obj);
bro.iDontAlways('foo').butWhenIdo(fn);
assert.equal(fired, true);
});
it('should pass the method\'s return value as param to callback', function() {
var bro = Bro(obj);
bro.iDontAlways('foo').butWhenIdo(fn);
assert.equal(param, 91);
});
it('should apply the object as its own context', function() {
var bro = Bro(obj);
bro.iDontAlways('foo').butWhenIdo(fn);
assert.equal(context, obj);
});
});
describe('Bro.braceYourself', function() {
var success,
error,
obj = {
"foo": function() {
throw 'an error';
}
},
fn = function(e) {
success = true;
error = e;
};
beforeEach(function() {
success = null;
error = null;
});
it('should fire the callback when an exception is thrown', function() {
var bro = Bro(obj);
bro.braceYourself('foo').hereComeTheErrors(fn);
assert.equal(success, true);
});
it('should pass the error to the callback', function() {
var bro = Bro(obj);
bro.braceYourself('foo').hereComeTheErrors(fn);
assert.equal(error, 'an error');
});
});
describe('Bro.makeItHappen', function() {
var expected,
obj;
beforeEach(function() {
obj = { "foo": { "bar": {} } };
});
it('should add properties to object, in a nested fashion', function() {
expected = { "foo": { "bar": {} }, "stuff": { "and": { "things": {} } } };
var bro = Bro(obj);
bro.makeItHappen('stuff.and.things');
assert.deepEqual(expected, obj);
});
it('should add properties to object, extending deeper nested objects', function() {
expected = { "foo": { "bar": { "stuff": { "and": { "things": {} } } } } };
var bro = Bro(obj);
bro.makeItHappen('foo.bar.stuff.and.things');
assert.deepEqual(expected, obj);
});
it('should set existing deeply nested properties on an object', function() {
expected = { "foo": { "bar": 'awesome' } };
var bro = Bro(obj);
bro.makeItHappen('foo.bar', 'awesome');
assert.deepEqual(expected, obj);
});
it('should create new properties, then set them, as needed', function() {
expected = { "foo": { "bar": { "stuff": { "and": { "things": 'super awesome' } } } } };
var bro = Bro(obj);
bro.makeItHappen('foo.bar.stuff.and.things', 'super awesome');
assert.deepEqual(expected, obj);
});
});
describe('brototype alias', function(){
it('kind of basically works', function(){
assert.notEqual(Bro.brototype.doYouEven, undefined);
});
});