You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
2.8 KiB

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