-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathscoped_spec.ts
162 lines (128 loc) · 4.99 KB
/
scoped_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { UnitTestTree } from '../../testing';
import { HostTree } from './host-tree';
import { ScopedTree } from './scoped';
describe('ScopedTree', () => {
let base: HostTree;
let scoped: ScopedTree;
beforeEach(() => {
base = new HostTree();
base.create('/file-0-1', '0-1');
base.create('/file-0-2', '0-2');
base.create('/file-0-3', '0-3');
base.create('/level-1/file-1-1', '1-1');
base.create('/level-1/file-1-2', '1-2');
base.create('/level-1/file-1-3', '1-3');
base.create('/level-1/level-2/file-2-1', '2-1');
base.create('/level-1/level-2/file-2-2', '2-2');
base.create('/level-1/level-2/file-2-3', '2-3');
scoped = new ScopedTree(base, 'level-1');
});
it('supports exists', () => {
expect(scoped.exists('/file-1-1')).toBeTruthy();
expect(scoped.exists('file-1-1')).toBeTruthy();
expect(scoped.exists('/level-2/file-2-1')).toBeTruthy();
expect(scoped.exists('level-2/file-2-1')).toBeTruthy();
expect(scoped.exists('/file-1-4')).toBeFalsy();
expect(scoped.exists('file-1-4')).toBeFalsy();
expect(scoped.exists('/file-0-1')).toBeFalsy();
expect(scoped.exists('file-0-1')).toBeFalsy();
expect(scoped.exists('/level-1/file-1-1')).toBeFalsy();
expect(scoped.exists('level-1/file-1-1')).toBeFalsy();
});
it('supports read', () => {
expect(scoped.read('/file-1-2')).not.toBeNull();
expect(scoped.read('file-1-2')).not.toBeNull();
const test = new UnitTestTree(scoped);
expect(test.readContent('/file-1-2')).toBe('1-2');
expect(test.readContent('file-1-2')).toBe('1-2');
expect(scoped.read('/file-0-2')).toBeNull();
expect(scoped.read('file-0-2')).toBeNull();
});
it('supports create', () => {
expect(() => scoped.create('/file-1-4', '1-4')).not.toThrow();
const test = new UnitTestTree(scoped);
expect(test.readContent('/file-1-4')).toBe('1-4');
expect(test.readContent('file-1-4')).toBe('1-4');
expect(base.exists('/level-1/file-1-4')).toBeTruthy();
});
it('supports delete', () => {
expect(() => scoped.delete('/file-0-3')).toThrow();
expect(() => scoped.delete('/file-1-3')).not.toThrow();
expect(scoped.exists('/file-1-3')).toBeFalsy();
expect(base.exists('/level-1/file-1-3')).toBeFalsy();
});
it('supports overwrite', () => {
expect(() => scoped.overwrite('/file-1-1', '1-1*')).not.toThrow();
expect(() => scoped.overwrite('/file-1-4', '1-4*')).toThrow();
const test = new UnitTestTree(scoped);
expect(test.readContent('/file-1-1')).toBe('1-1*');
expect(test.readContent('file-1-1')).toBe('1-1*');
});
it('supports rename', () => {
expect(() => scoped.rename('/file-1-1', '/file-1-1-new')).not.toThrow();
expect(() => scoped.rename('/file-1-4', '/file-1-4-new')).toThrow();
const test = new UnitTestTree(scoped);
expect(test.readContent('/file-1-1-new')).toBe('1-1');
expect(test.readContent('file-1-1-new')).toBe('1-1');
});
it('supports get', () => {
expect(scoped.get('/file-1-1')).not.toBeNull();
const file = scoped.get('file-1-1');
expect(file && (file.path as string)).toBe('/file-1-1');
expect(scoped.get('/file-0-1')).toBeNull();
expect(scoped.get('file-0-1')).toBeNull();
});
it('supports getDir', () => {
expect(scoped.getDir('/level-2')).not.toBeNull();
const dir = scoped.getDir('level-2');
expect(dir.path as string).toBe('/level-2');
expect(dir.parent).not.toBeNull();
const files: string[] = [];
dir.visit((path) => files.push(path));
files.sort();
expect(files).toEqual(['/level-2/file-2-1', '/level-2/file-2-2', '/level-2/file-2-3']);
});
it('supports visit', () => {
const files: string[] = [];
scoped.visit((path) => files.push(path));
files.sort();
expect(files).toEqual([
'/file-1-1',
'/file-1-2',
'/file-1-3',
'/level-2/file-2-1',
'/level-2/file-2-2',
'/level-2/file-2-3',
]);
});
it('supports merge into a scoped tree', () => {
const other = new HostTree();
other.create('other-file', 'other');
scoped.merge(other);
expect(base.exists('/level-1/other-file')).toBeTruthy();
expect(base.exists('/other-file')).toBeFalsy();
});
it('supports merge from a scoped tree', () => {
const other = new HostTree();
other.merge(scoped);
expect(other.exists('/file-1-1')).toBeTruthy();
expect(other.exists('file-1-1')).toBeTruthy();
expect(other.exists('/file-1-2')).toBeTruthy();
expect(other.exists('/file-0-1')).toBeFalsy();
expect(other.exists('file-0-1')).toBeFalsy();
expect(other.exists('/level-1/file-1-1')).toBeFalsy();
expect(other.exists('level-1/file-1-1')).toBeFalsy();
});
it('supports root', () => {
expect(scoped.root).not.toBeNull();
expect(scoped.root.path as string).toBe('/');
expect(scoped.root.parent).toBeNull();
});
});