-
Notifications
You must be signed in to change notification settings - Fork 41
/
test.js
77 lines (67 loc) · 1.83 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
var assert = require('assert');
var delve = require('.');
var obj = {
undef: undefined,
zero: 0,
one: 1,
n: null,
f: false,
a: {
two: 2,
b: {
three: 3,
c: {
four: 4
}
}
}
};
// assert equality of a given path, as dot notation and array.
//optional third argument is for default when object is not found
function check(path, value, def) {
var out = delve(obj, path, def);
assert.strictEqual(out, value, 'delve(obj, "'+path+'") should be '+value+', got '+out);
console.log(' ✓ delve(obj, "'+path+'"'+ (def ? ', "'+def+'"' : '') + ')');
if (path) {
var arr = path.split('.');
assert.strictEqual(delve(obj, arr, def), value);
console.log(' ✓ delve(obj, ' + JSON.stringify(arr) + (def ? ', "'+def+'"' : '') + ')');
console.log(' ✓ delve(obj, '+JSON.stringify(arr)+')');
}
}
console.log("> No Defaults");
check('', undefined);
check('one', obj.one);
check('one.two', undefined);
check('a', obj.a);
check('a.two', obj.a.two);
check('a.b', obj.a.b);
check('a.b.three', obj.a.b.three);
check('a.b.c', obj.a.b.c);
check('a.b.c.four', obj.a.b.c.four);
check('n', obj.n);
check('n.badkey', undefined);
check('f', false);
check('f.badkey', undefined);
//test defaults
console.log("\n> With Defaults");
check('', 'foo', 'foo');
check('undef', 'foo', 'foo');
check('n', null, 'foo');
check('n.badkey', 'foo', 'foo');
check('zero', 0, 'foo');
check('a.badkey', 'foo', 'foo');
check('a.badkey.anotherbadkey', 'foo', 'foo');
check('f', false, 'foo');
check('f.badkey', 'foo', 'foo');
//check undefined key throws an error
assert.throws(delve.bind(this, obj, undefined));
assert.throws(delve.bind(this, obj, undefined, 'foo'));
//check undefined obj doesn't throw errors and uses default
var backupObj = obj;
obj = undefined;
check('one', undefined);
check('one', 'foo', 'foo');
obj = backupObj;
console.log('✅ Success!');
process.exit(0);