test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var assert = require("assert");
  2. var jsonpointer = require("./jsonpointer");
  3. var obj = {
  4. a: 1,
  5. b: {
  6. c: 2
  7. },
  8. d: {
  9. e: [{a:3}, {b:4}, {c:5}]
  10. }
  11. };
  12. assert.equal(jsonpointer.get(obj, "/a"), 1);
  13. assert.equal(jsonpointer.get(obj, "/b/c"), 2);
  14. assert.equal(jsonpointer.get(obj, "/d/e/0/a"), 3);
  15. assert.equal(jsonpointer.get(obj, "/d/e/1/b"), 4);
  16. assert.equal(jsonpointer.get(obj, "/d/e/2/c"), 5);
  17. // set returns old value
  18. assert.equal(jsonpointer.set(obj, "/a", 2), 1);
  19. assert.equal(jsonpointer.set(obj, "/b/c", 3), 2);
  20. assert.equal(jsonpointer.set(obj, "/d/e/0/a", 4), 3);
  21. assert.equal(jsonpointer.set(obj, "/d/e/1/b", 5), 4);
  22. assert.equal(jsonpointer.set(obj, "/d/e/2/c", 6), 5);
  23. assert.equal(jsonpointer.get(obj, "/a"), 2);
  24. assert.equal(jsonpointer.get(obj, "/b/c"), 3);
  25. assert.equal(jsonpointer.get(obj, "/d/e/0/a"), 4);
  26. assert.equal(jsonpointer.get(obj, "/d/e/1/b"), 5);
  27. assert.equal(jsonpointer.get(obj, "/d/e/2/c"), 6);
  28. assert.equal(jsonpointer.get(obj, ""), obj);
  29. assert.throws(function(){ jsonpointer.get(obj, "a"); }, validateError);
  30. assert.throws(function(){ jsonpointer.get(obj, "a/"); }, validateError);
  31. function validateError(err) {
  32. if ( (err instanceof Error) && /Invalid JSON pointer/.test(err.message) ) {
  33. return true;
  34. }
  35. }
  36. var complexKeys = {
  37. "a/b": {
  38. c: 1
  39. },
  40. d: {
  41. "e/f": 2
  42. },
  43. "~1": 3,
  44. "01": 4
  45. }
  46. assert.equal(jsonpointer.get(complexKeys, "/a~1b/c"), 1);
  47. assert.equal(jsonpointer.get(complexKeys, "/d/e~1f"), 2);
  48. assert.equal(jsonpointer.get(complexKeys, "/~01"), 3);
  49. assert.equal(jsonpointer.get(complexKeys, "/01"), 4);
  50. assert.equal(jsonpointer.get(complexKeys, "/a/b/c"), null);
  51. assert.equal(jsonpointer.get(complexKeys, "/~1"), null);
  52. // draft-ietf-appsawg-json-pointer-08 has special array rules
  53. var ary = [ "zero", "one", "two" ];
  54. assert.equal(jsonpointer.get(ary, "/01"), null);
  55. //assert.equal(jsonpointer.set(ary, "/-", "three"), null);
  56. //assert.equal(ary[3], "three");
  57. // Examples from the draft:
  58. var example = {
  59. "foo": ["bar", "baz"],
  60. "": 0,
  61. "a/b": 1,
  62. "c%d": 2,
  63. "e^f": 3,
  64. "g|h": 4,
  65. "i\\j": 5,
  66. "k\"l": 6,
  67. " ": 7,
  68. "m~n": 8
  69. };
  70. assert.equal(jsonpointer.get(example, ""), example);
  71. var ans = jsonpointer.get(example, "/foo");
  72. assert.equal(ans.length, 2);
  73. assert.equal(ans[0], "bar");
  74. assert.equal(ans[1], "baz");
  75. assert.equal(jsonpointer.get(example, "/foo/0"), "bar");
  76. assert.equal(jsonpointer.get(example, "/"), 0);
  77. assert.equal(jsonpointer.get(example, "/a~1b"), 1);
  78. assert.equal(jsonpointer.get(example, "/c%d"), 2);
  79. assert.equal(jsonpointer.get(example, "/e^f"), 3);
  80. assert.equal(jsonpointer.get(example, "/g|h"), 4);
  81. assert.equal(jsonpointer.get(example, "/i\\j"), 5);
  82. assert.equal(jsonpointer.get(example, "/k\"l"), 6);
  83. assert.equal(jsonpointer.get(example, "/ "), 7);
  84. assert.equal(jsonpointer.get(example, "/m~0n"), 8);
  85. console.log("All tests pass.");