Version 1
[yaffs-website] / node_modules / getobject / test / namespace_test.js
1 'use strict';
2
3 var getobject = require('../lib/getobject');
4
5 exports.get = {
6   'no create': function(test) {
7     var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h.i': 2}}};
8     test.strictEqual(getobject.get(obj, 'a'), obj.a, 'should get immediate properties.');
9     test.strictEqual(getobject.get(obj, 'a.b'), obj.a.b, 'should get nested properties.');
10     test.strictEqual(getobject.get(obj, 'a.x'), undefined, 'should return undefined for nonexistent properties.');
11     test.strictEqual(getobject.get(obj, 'a.b.c'), 1, 'should return values.');
12     test.strictEqual(getobject.get(obj, 'a.b.d'), '', 'should return values.');
13     test.strictEqual(getobject.get(obj, 'a.b.e'), null, 'should return values.');
14     test.strictEqual(getobject.get(obj, 'a.b.f'), undefined, 'should return values.');
15     test.strictEqual(getobject.get(obj, 'a.b.g\\.h\\.i'), 2, 'literal backslash should escape period in property name.');
16     test.done();
17   },
18   'create': function(test) {
19     var obj = {a: 1};
20     test.strictEqual(getobject.get(obj, 'a', true), obj.a, 'should just return existing properties.');
21     test.strictEqual(getobject.get(obj, 'b', true), obj.b, 'should create immediate properties.');
22     test.strictEqual(getobject.get(obj, 'c.d.e', true), obj.c.d.e, 'should create nested properties.');
23     test.done();
24   }
25 };
26
27 exports.set = function(test) {
28   var obj = {};
29   test.strictEqual(getobject.set(obj, 'a', 1), 1, 'should return immediate property value.');
30   test.strictEqual(obj.a, 1, 'should set property value.');
31   test.strictEqual(getobject.set(obj, 'b.c.d', 1), 1, 'should return nested property value.');
32   test.strictEqual(obj.b.c.d, 1, 'should set property value.');
33   test.strictEqual(getobject.set(obj, 'e\\.f\\.g', 1), 1, 'literal backslash should escape period in property name.');
34   test.strictEqual(obj['e.f.g'], 1, 'should set property value.');
35   test.done();
36 };
37
38 exports.exists = function(test) {
39   var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h.i': 2}}};
40   test.ok(getobject.exists(obj, 'a'), 'immediate property should exist.');
41   test.ok(getobject.exists(obj, 'a.b'), 'nested property should exist.');
42   test.ok(getobject.exists(obj, 'a.b.c'), 'nested property should exist.');
43   test.ok(getobject.exists(obj, 'a.b.d'), 'nested property should exist.');
44   test.ok(getobject.exists(obj, 'a.b.e'), 'nested property should exist.');
45   test.ok(getobject.exists(obj, 'a.b.f'), 'nested property should exist.');
46   test.ok(getobject.exists(obj, 'a.b.g\\.h\\.i'), 'literal backslash should escape period in property name.');
47   test.equal(getobject.exists(obj, 'x'), false, 'nonexistent property should not exist.');
48   test.equal(getobject.exists(obj, 'a.x'), false, 'nonexistent property should not exist.');
49   test.equal(getobject.exists(obj, 'a.b.x'), false, 'nonexistent property should not exist.');
50   test.done();
51 };