namespace_test.js
2.8 KB
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
'use strict';
var getobject = require('../lib/getobject');
exports.get = {
'no create': function(test) {
var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h.i': 2}}};
test.strictEqual(getobject.get(obj, 'a'), obj.a, 'should get immediate properties.');
test.strictEqual(getobject.get(obj, 'a.b'), obj.a.b, 'should get nested properties.');
test.strictEqual(getobject.get(obj, 'a.x'), undefined, 'should return undefined for nonexistent properties.');
test.strictEqual(getobject.get(obj, 'a.b.c'), 1, 'should return values.');
test.strictEqual(getobject.get(obj, 'a.b.d'), '', 'should return values.');
test.strictEqual(getobject.get(obj, 'a.b.e'), null, 'should return values.');
test.strictEqual(getobject.get(obj, 'a.b.f'), undefined, 'should return values.');
test.strictEqual(getobject.get(obj, 'a.b.g\\.h\\.i'), 2, 'literal backslash should escape period in property name.');
test.done();
},
'create': function(test) {
var obj = {a: 1};
test.strictEqual(getobject.get(obj, 'a', true), obj.a, 'should just return existing properties.');
test.strictEqual(getobject.get(obj, 'b', true), obj.b, 'should create immediate properties.');
test.strictEqual(getobject.get(obj, 'c.d.e', true), obj.c.d.e, 'should create nested properties.');
test.done();
}
};
exports.set = function(test) {
var obj = {};
test.strictEqual(getobject.set(obj, 'a', 1), 1, 'should return immediate property value.');
test.strictEqual(obj.a, 1, 'should set property value.');
test.strictEqual(getobject.set(obj, 'b.c.d', 1), 1, 'should return nested property value.');
test.strictEqual(obj.b.c.d, 1, 'should set property value.');
test.strictEqual(getobject.set(obj, 'e\\.f\\.g', 1), 1, 'literal backslash should escape period in property name.');
test.strictEqual(obj['e.f.g'], 1, 'should set property value.');
test.done();
};
exports.exists = function(test) {
var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h.i': 2}}};
test.ok(getobject.exists(obj, 'a'), 'immediate property should exist.');
test.ok(getobject.exists(obj, 'a.b'), 'nested property should exist.');
test.ok(getobject.exists(obj, 'a.b.c'), 'nested property should exist.');
test.ok(getobject.exists(obj, 'a.b.d'), 'nested property should exist.');
test.ok(getobject.exists(obj, 'a.b.e'), 'nested property should exist.');
test.ok(getobject.exists(obj, 'a.b.f'), 'nested property should exist.');
test.ok(getobject.exists(obj, 'a.b.g\\.h\\.i'), 'literal backslash should escape period in property name.');
test.equal(getobject.exists(obj, 'x'), false, 'nonexistent property should not exist.');
test.equal(getobject.exists(obj, 'a.x'), false, 'nonexistent property should not exist.');
test.equal(getobject.exists(obj, 'a.b.x'), false, 'nonexistent property should not exist.');
test.done();
};