1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const chai_1 = require("chai");
|
19 | const objects_1 = require("../../common/objects");
|
20 | const mock_tree_model_1 = require("./test/mock-tree-model");
|
21 | const tree_model_1 = require("./tree-model");
|
22 | const tree_iterator_1 = require("./tree-iterator");
|
23 | const tree_test_container_1 = require("./test/tree-test-container");
|
24 | const tree_expansion_1 = require("./tree-expansion");
|
25 | describe('tree-iterator', () => {
|
26 | const model = createTreeModel();
|
27 | const findNode = (id) => model.getNode(id);
|
28 | beforeEach(() => {
|
29 | model.root = mock_tree_model_1.MockTreeModel.HIERARCHICAL_MOCK_ROOT();
|
30 | });
|
31 | it('should include root', () => {
|
32 | const expected = ['1'];
|
33 | const actual = [...new tree_iterator_1.BottomUpTreeIterator(findNode('1'))].map(node => node.id);
|
34 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
35 | });
|
36 | it('should return `undefined` after consuming the iterator', () => {
|
37 | const itr = new tree_iterator_1.BottomUpTreeIterator(findNode('1'));
|
38 | let next = itr.next();
|
39 | while (!next.done) {
|
40 | (0, chai_1.expect)(next.value).to.be.not.undefined;
|
41 | next = itr.next();
|
42 | }
|
43 | (0, chai_1.expect)(next.done).to.be.true;
|
44 | (0, chai_1.expect)(next.value).to.be.undefined;
|
45 | });
|
46 | it('depth-first (no collapsed nodes)', () => {
|
47 | const expected = ['1', '1.1', '1.1.1', '1.1.2', '1.2', '1.2.1', '1.2.1.1', '1.2.1.2', '1.2.2', '1.2.3', '1.3'];
|
48 | const actual = [...new tree_iterator_1.DepthFirstTreeIterator(model.root)].map(node => node.id);
|
49 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
50 | });
|
51 | it('depth-first (with collapsed nodes)', () => {
|
52 | collapseNode('1.1', '1.2.1');
|
53 | const expected = ['1', '1.1', '1.2', '1.2.1', '1.2.2', '1.2.3', '1.3'];
|
54 | const actual = [...new tree_iterator_1.DepthFirstTreeIterator(model.root, { pruneCollapsed: true })].map(node => node.id);
|
55 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
56 | });
|
57 | it('breadth-first (no collapsed nodes)', () => {
|
58 | const expected = ['1', '1.1', '1.2', '1.3', '1.1.1', '1.1.2', '1.2.1', '1.2.2', '1.2.3', '1.2.1.1', '1.2.1.2'];
|
59 | const actual = [...new tree_iterator_1.BreadthFirstTreeIterator(model.root)].map(node => node.id);
|
60 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
61 | });
|
62 | it('breadth-first (with collapsed nodes)', () => {
|
63 | collapseNode('1.1', '1.2.1');
|
64 | const expected = ['1', '1.1', '1.2', '1.3', '1.2.1', '1.2.2', '1.2.3'];
|
65 | const actual = [...new tree_iterator_1.BreadthFirstTreeIterator(model.root, { pruneCollapsed: true })].map(node => node.id);
|
66 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
67 | });
|
68 | it('bottom-up (no collapsed nodes)', () => {
|
69 | const expected = ['1.2.2', '1.2.1.2', '1.2.1.1', '1.2.1', '1.2', '1.1.2', '1.1.1', '1.1', '1'];
|
70 | const actual = [...new tree_iterator_1.BottomUpTreeIterator(findNode('1.2.2'))].map(node => node.id);
|
71 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
72 | });
|
73 | it('bottom-up (with collapsed nodes)', () => {
|
74 | collapseNode('1.1', '1.2.1');
|
75 | const expected = ['1.2.2', '1.2.1', '1.2', '1.1', '1'];
|
76 | const actual = [...new tree_iterator_1.BottomUpTreeIterator(findNode('1.2.2'), { pruneCollapsed: true })].map(node => node.id);
|
77 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
78 | });
|
79 | it('top-down (no collapsed nodes)', () => {
|
80 | const expected = ['1.1.2', '1.2', '1.2.1', '1.2.1.1', '1.2.1.2', '1.2.2', '1.2.3', '1.3'];
|
81 | const actual = [...new tree_iterator_1.TopDownTreeIterator(findNode('1.1.2'))].map(node => node.id);
|
82 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
83 | });
|
84 | it('top-down (with collapsed nodes)', () => {
|
85 | collapseNode('1.2.1');
|
86 | const expected = ['1.1.2', '1.2', '1.2.1', '1.2.2', '1.2.3', '1.3'];
|
87 | const actual = [...new tree_iterator_1.TopDownTreeIterator(findNode('1.1.2'), { pruneCollapsed: true })].map(node => node.id);
|
88 | (0, chai_1.expect)(expected).to.be.deep.equal(actual);
|
89 | });
|
90 | function collapseNode(...ids) {
|
91 | ids.map(findNode).filter(objects_1.notEmpty).filter(tree_expansion_1.ExpandableTreeNode.is).forEach(node => {
|
92 | model.collapseNode(node);
|
93 | (0, chai_1.expect)(node).to.have.property('expanded', false);
|
94 | });
|
95 | }
|
96 | function createTreeModel() {
|
97 | return (0, tree_test_container_1.createTreeTestContainer)().get(tree_model_1.TreeModel);
|
98 | }
|
99 | });
|
100 | describe('iterators', () => {
|
101 | it('as-iterator', () => {
|
102 | const array = [1, 2, 3, 4];
|
103 | const itr = tree_iterator_1.Iterators.asIterator(array);
|
104 | let next = itr.next();
|
105 | while (!next.done) {
|
106 | const { value } = next;
|
107 | (0, chai_1.expect)(value).to.be.not.undefined;
|
108 | const index = array.indexOf(value);
|
109 | (0, chai_1.expect)(index).to.be.not.equal(-1);
|
110 | array.splice(index, 1);
|
111 | next = itr.next();
|
112 | }
|
113 | (0, chai_1.expect)(array).to.be.empty;
|
114 | });
|
115 | it('cycle - without start', function () {
|
116 | this.timeout(1000);
|
117 | const array = [1, 2, 3, 4];
|
118 | const itr = tree_iterator_1.Iterators.cycle(array);
|
119 | const visitedItems = new Set();
|
120 | let next = itr.next();
|
121 | while (!next.done) {
|
122 | const { value } = next;
|
123 | (0, chai_1.expect)(value).to.be.not.undefined;
|
124 | if (visitedItems.has(value)) {
|
125 | (0, chai_1.expect)(Array.from(visitedItems).sort()).to.be.deep.equal(array.sort());
|
126 | break;
|
127 | }
|
128 | visitedItems.add(value);
|
129 | next = itr.next();
|
130 | }
|
131 | });
|
132 | it('cycle - with start', function () {
|
133 | this.timeout(1000);
|
134 | const array = [1, 2, 3, 4];
|
135 | const itr = tree_iterator_1.Iterators.cycle(array, 2);
|
136 | const visitedItems = new Set();
|
137 | let next = itr.next();
|
138 | (0, chai_1.expect)(next.value).to.be.equal(2);
|
139 | while (!next.done) {
|
140 | const { value } = next;
|
141 | (0, chai_1.expect)(value).to.be.not.undefined;
|
142 | if (visitedItems.has(value)) {
|
143 | (0, chai_1.expect)(Array.from(visitedItems).sort()).to.be.deep.equal(array.sort());
|
144 | break;
|
145 | }
|
146 | visitedItems.add(value);
|
147 | next = itr.next();
|
148 | }
|
149 | });
|
150 | });
|
151 |
|
\ | No newline at end of file |