namespace_test.js 2.8 KB

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