circular.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var test = require('tape');
  2. var traverse = require('../');
  3. var deepEqual = require('./lib/deep_equal');
  4. var util = require('util');
  5. test('circular', function (t) {
  6. t.plan(1);
  7. var obj = { x : 3 };
  8. obj.y = obj;
  9. traverse(obj).forEach(function (x) {
  10. if (this.path.join('') == 'y') {
  11. t.equal(
  12. util.inspect(this.circular.node),
  13. util.inspect(obj)
  14. );
  15. }
  16. });
  17. });
  18. test('deepCirc', function (t) {
  19. t.plan(2);
  20. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  21. obj.y[2] = obj;
  22. var times = 0;
  23. traverse(obj).forEach(function (x) {
  24. if (this.circular) {
  25. t.same(this.circular.path, []);
  26. t.same(this.path, [ 'y', 2 ]);
  27. }
  28. });
  29. });
  30. test('doubleCirc', function (t) {
  31. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  32. obj.y[2] = obj;
  33. obj.x.push(obj.y);
  34. var circs = [];
  35. traverse(obj).forEach(function (x) {
  36. if (this.circular) {
  37. circs.push({ circ : this.circular, self : this, node : x });
  38. }
  39. });
  40. t.same(circs[0].self.path, [ 'x', 3, 2 ]);
  41. t.same(circs[0].circ.path, []);
  42. t.same(circs[1].self.path, [ 'y', 2 ]);
  43. t.same(circs[1].circ.path, []);
  44. t.same(circs.length, 2);
  45. t.end();
  46. });
  47. test('circDubForEach', function (t) {
  48. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  49. obj.y[2] = obj;
  50. obj.x.push(obj.y);
  51. traverse(obj).forEach(function (x) {
  52. if (this.circular) this.update('...');
  53. });
  54. t.same(obj, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] });
  55. t.end();
  56. });
  57. test('circDubMap', function (t) {
  58. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  59. obj.y[2] = obj;
  60. obj.x.push(obj.y);
  61. var c = traverse(obj).map(function (x) {
  62. if (this.circular) {
  63. this.update('...');
  64. }
  65. });
  66. t.same(c, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] });
  67. t.end();
  68. });
  69. test('circClone', function (t) {
  70. var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
  71. obj.y[2] = obj;
  72. obj.x.push(obj.y);
  73. var clone = traverse.clone(obj);
  74. t.ok(obj !== clone);
  75. t.ok(clone.y[2] === clone);
  76. t.ok(clone.y[2] !== obj);
  77. t.ok(clone.x[3][2] === clone);
  78. t.ok(clone.x[3][2] !== obj);
  79. t.same(clone.x.slice(0,3), [1,2,3]);
  80. t.same(clone.y.slice(0,2), [4,5]);
  81. t.end();
  82. });
  83. test('circMapScrub', function (t) {
  84. var obj = { a : 1, b : 2 };
  85. obj.c = obj;
  86. var scrubbed = traverse(obj).map(function (node) {
  87. if (this.circular) this.remove();
  88. });
  89. t.same(
  90. Object.keys(scrubbed).sort(),
  91. [ 'a', 'b' ]
  92. );
  93. t.ok(deepEqual(scrubbed, { a : 1, b : 2 }));
  94. t.equal(obj.c, obj);
  95. t.end();
  96. });