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.

51 lines
2.8 KiB

  1. 'use strict';
  2. var namespace = require('../../lib/util/namespace.js');
  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(namespace.get(obj, 'a'), obj.a, 'should get immediate properties.');
  7. test.strictEqual(namespace.get(obj, 'a.b'), obj.a.b, 'should get nested properties.');
  8. test.strictEqual(namespace.get(obj, 'a.x'), undefined, 'should return undefined for nonexistent properties.');
  9. test.strictEqual(namespace.get(obj, 'a.b.c'), 1, 'should return values.');
  10. test.strictEqual(namespace.get(obj, 'a.b.d'), '', 'should return values.');
  11. test.strictEqual(namespace.get(obj, 'a.b.e'), null, 'should return values.');
  12. test.strictEqual(namespace.get(obj, 'a.b.f'), undefined, 'should return values.');
  13. test.strictEqual(namespace.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(namespace.get(obj, 'a', true), obj.a, 'should just return existing properties.');
  19. test.strictEqual(namespace.get(obj, 'b', true), obj.b, 'should create immediate properties.');
  20. test.strictEqual(namespace.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(namespace.set(obj, 'a', 1), 1, 'should return immediate property value.');
  27. test.strictEqual(obj.a, 1, 'should set property value.');
  28. test.strictEqual(namespace.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(namespace.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(namespace.exists(obj, 'a'), 'immediate property should exist.');
  37. test.ok(namespace.exists(obj, 'a.b'), 'nested property should exist.');
  38. test.ok(namespace.exists(obj, 'a.b.c'), 'nested property should exist.');
  39. test.ok(namespace.exists(obj, 'a.b.d'), 'nested property should exist.');
  40. test.ok(namespace.exists(obj, 'a.b.e'), 'nested property should exist.');
  41. test.ok(namespace.exists(obj, 'a.b.f'), 'nested property should exist.');
  42. test.ok(namespace.exists(obj, 'a.b.g\\.h\\.i'), 'literal backslash should escape period in property name.');
  43. test.equal(namespace.exists(obj, 'x'), false, 'nonexistent property should not exist.');
  44. test.equal(namespace.exists(obj, 'a.x'), false, 'nonexistent property should not exist.');
  45. test.equal(namespace.exists(obj, 'a.b.x'), false, 'nonexistent property should not exist.');
  46. test.done();
  47. };