-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathcommon_spec.ts
62 lines (51 loc) · 1.62 KB
/
common_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
/**
* @license
* Copyright Google Inc. 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
*/
// tslint:disable:no-non-null-assertion
import { normalize } from '@angular-devkit/core';
import { Tree } from './interface';
export interface VisitTestVisitSpec {
root: string;
expected?: string[];
exception?: (spec: {path: string}) => Error;
focus?: boolean;
}
export interface VisitTestSet {
name: string;
files: string[];
visits: VisitTestVisitSpec[];
focus?: boolean;
}
export interface VisitTestSpec {
createTree: (paths: string[]) => Tree;
sets: VisitTestSet[];
}
export function testTreeVisit({createTree, sets}: VisitTestSpec) {
sets.forEach(({name, files: paths, visits, focus: focusSet}) => {
visits.forEach(({root, expected, exception, focus}) => {
if (expected == null) { expected = paths; }
const that = focusSet || focus ? fit : it;
that(`can visit: ${name} from ${root}`, () => {
const tree = createTree(paths);
const normalizedRoot = normalize(root);
if (exception != null) {
expect(() => tree.getDir(normalizedRoot).visit(() => {}))
.toThrow(exception({path: normalizedRoot}));
return;
}
const allPaths: string[] = [];
tree.getDir(normalizedRoot)
.visit((path, entry) => {
expect(entry).not.toBeNull();
expect(entry!.content.toString()).toEqual(path);
allPaths.push(path);
});
expect(allPaths).toEqual(expected!);
});
});
});
}