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