circular.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. var test = require('tape');
  3. var traverse = require('../');
  4. var deepEqual = require('./lib/deep_equal');
  5. var util = require('util');
  6. test('circular', function (t) {
  7. t.plan(1);
  8. var obj = { x: 3 };
  9. obj.y = obj;
  10. traverse(obj).forEach(function () {
  11. if (this.path.join('') === 'y') {
  12. t.equal(
  13. util.inspect(this.circular.node),
  14. util.inspect(obj)
  15. );
  16. }
  17. });
  18. });
  19. test('deepCirc', function (t) {
  20. t.plan(2);
  21. var obj = { x: [1, 2, 3], y: [4, 5] };
  22. obj.y[2] = obj;
  23. traverse(obj).forEach(function () {
  24. if (this.circular) {
  25. t.deepEqual(this.circular.path, []);
  26. t.deepEqual(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.deepEqual(circs[0].self.path, ['x', '3', '2']);
  41. t.deepEqual(circs[0].circ.path, []);
  42. t.deepEqual(circs[1].self.path, ['y', '2']);
  43. t.deepEqual(circs[1].circ.path, []);
  44. t.deepEqual(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 () {
  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 () {
  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 () {
  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. });