-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathtemplate_spec.ts
207 lines (182 loc) · 5.74 KB
/
template_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @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
*/
/* eslint-disable import/no-extraneous-dependencies */
import { normalize } from '@angular-devkit/core';
import { UnitTestTree } from '@angular-devkit/schematics/testing';
import { of as observableOf } from 'rxjs';
import { SchematicContext } from '../engine/interface';
import { HostTree } from '../tree/host-tree';
import { FileEntry, MergeStrategy } from '../tree/interface';
import { callRule } from './call';
import {
InvalidPipeException,
OptionIsNotDefinedException,
UnknownPipeException,
applyContentTemplate,
applyPathTemplate,
applyTemplates,
} from './template';
function _entry(path?: string, content?: string): FileEntry {
if (!path) {
path = 'a/b/c';
}
if (!content) {
content = 'hello world';
}
return {
path: normalize(path),
content: Buffer.from(content),
};
}
describe('applyPathTemplate', () => {
function _applyPathTemplate(path: string, options: {}): string | null {
const newEntry = applyPathTemplate(options)(_entry(path));
if (newEntry) {
return newEntry.path;
} else {
return null;
}
}
it('works', () => {
expect(_applyPathTemplate('/a/b/c/d', {})).toBe('/a/b/c/d');
expect(_applyPathTemplate('/a/b/__c__/d', { c: 1 })).toBe('/a/b/1/d');
expect(_applyPathTemplate('/a/b/__c__/d', { c: 'hello/world' })).toBe('/a/b/hello/world/d');
expect(_applyPathTemplate('/a__c__b', { c: 'hello/world' })).toBe('/ahello/worldb');
expect(_applyPathTemplate('/a__c__b__d__c', { c: '1', d: '2' })).toBe('/a1b2c');
});
it('works with single _', () => {
expect(_applyPathTemplate('/a_b_c/d__e_f__g', { e_f: 1 })).toBe('/a_b_c/d1g');
});
it('works with complex _________...', () => {
expect(
_applyPathTemplate('/_____' + 'a' + '___a__' + '_/' + '__a___/__b___', {
_a: 0,
a: 1,
b: 2,
_: '.',
}),
).toBe('/.a0_/1_/2_');
expect(_applyPathTemplate('_____' + '_____' + '_____' + '___', { _: '.' })).toBe('...___');
});
it('works with functions', () => {
let arg = '';
expect(
_applyPathTemplate('/a__c__b', {
c: (x: string) => {
arg = x;
return 'hello';
},
}),
).toBe('/ahellob');
expect(arg).toBe('/a__c__b');
});
it('works with pipes', () => {
let called = '';
let called2 = '';
expect(
_applyPathTemplate('/a__c@d__b', {
c: 1,
d: (x: string) => ((called = x), 2),
}),
).toBe('/a2b');
expect(called).toBe('1');
expect(
_applyPathTemplate('/a__c@d@e__b', {
c: 10,
d: (x: string) => ((called = x), 20),
e: (x: string) => ((called2 = x), 30),
}),
).toBe('/a30b');
expect(called).toBe('10');
expect(called2).toBe('20');
});
it('errors out on undefined values', () => {
expect(() => _applyPathTemplate('/a__b__c', {})).toThrow(new OptionIsNotDefinedException('b'));
});
it('errors out on undefined or invalid pipes', () => {
expect(() => _applyPathTemplate('/a__b@d__c', { b: 1 })).toThrow(new UnknownPipeException('d'));
expect(() => _applyPathTemplate('/a__b@d__c', { b: 1, d: 1 })).toThrow(
new InvalidPipeException('d'),
);
});
});
describe('contentTemplate', () => {
function _applyContentTemplate(content: string, options: {}) {
const newEntry = applyContentTemplate(options)(_entry('', content));
if (newEntry) {
return newEntry.content.toString('utf-8');
} else {
return null;
}
}
it('works with echo token <%= ... %>', () => {
expect(_applyContentTemplate('a<%= value %>b', { value: 123 })).toBe('a123b');
});
it('works with if', () => {
expect(
_applyContentTemplate('a<% if (a) { %>b<% } %>c', {
value: 123,
a: true,
}),
).toBe('abc');
expect(
_applyContentTemplate('a<% if (a) { %>b<% } %>c', {
value: 123,
a: false,
}),
).toBe('ac');
});
it('works with for', () => {
expect(
_applyContentTemplate('a<% for (let i = 0; i < value; i++) { %>1<% } %>b', {
value: 5,
}),
).toBe('a11111b');
});
it('escapes HTML', () => {
expect(
_applyContentTemplate('a<%- html %>b', {
html: '<script>',
}),
).toBe('a<script>b');
});
it('escapes strings properly', () => {
expect(_applyContentTemplate('a<%= value %>b', { value: `'abc'` })).toBe("a'abc'b");
expect(_applyContentTemplate('a<%= \'a\' + "b" %>b', {})).toBe('aabb');
expect(_applyContentTemplate('a<%= "\\n" + "b" %>b', {})).toBe('a\nbb');
});
});
describe('applyTemplateFiles', () => {
it('works with template files exclusively', (done) => {
const tree = new UnitTestTree(new HostTree());
tree.create('a/b/file1', 'hello world');
tree.create('a/b/file2', 'hello world');
tree.create('a/b/file3.template', 'hello <%= 1 %> world');
tree.create('a/b/file__a__.template', 'hello <%= 1 %> world');
tree.create('a/b/file__norename__', 'hello <%= 1 %> world');
tree.create('a/c/file4', 'hello world');
const context: SchematicContext = {
strategy: MergeStrategy.Default,
} as SchematicContext;
// Rename all files that contain 'b' to 'hello'.
callRule(applyTemplates({ a: 'foo' }), observableOf(tree), context)
.toPromise()
.then(() => {
expect([...tree.files].sort()).toEqual([
'/a/b/file1',
'/a/b/file2',
'/a/b/file3',
'/a/b/file__norename__',
'/a/b/filefoo',
'/a/c/file4',
]);
expect(tree.readContent('/a/b/file3')).toBe('hello 1 world');
})
.then(done, done.fail);
});
});