@cloudpss/template 0.6.15 → 0.6.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tests/parser.ts DELETED
@@ -1,285 +0,0 @@
1
- import { parseTemplate, parseInterpolation } from '../dist/parser.js';
2
-
3
- describe('parseInterpolation', () => {
4
- it('should parse simple interpolation', () => {
5
- expect(parseInterpolation('')).toEqual({
6
- type: 'interpolation',
7
- templates: [''],
8
- values: [],
9
- });
10
- expect(parseInterpolation('hello')).toEqual({
11
- type: 'interpolation',
12
- templates: ['hello'],
13
- values: [],
14
- });
15
- expect(parseInterpolation('hello $name!')).toEqual({
16
- type: 'interpolation',
17
- templates: ['hello ', '!'],
18
- values: ['name'],
19
- });
20
- expect(parseInterpolation('$$name')).toEqual({
21
- type: 'interpolation',
22
- templates: ['$', ''],
23
- values: ['name'],
24
- });
25
- expect(parseInterpolation('$name_is_long')).toEqual({
26
- type: 'interpolation',
27
- templates: ['', ''],
28
- values: ['name_is_long'],
29
- });
30
- expect(parseInterpolation('$_001')).toEqual({
31
- type: 'interpolation',
32
- templates: ['', ''],
33
- values: ['_001'],
34
- });
35
- expect(parseInterpolation('$001$a$')).toEqual({
36
- type: 'interpolation',
37
- templates: ['$001', '$'],
38
- values: ['a'],
39
- });
40
- expect(parseInterpolation('$$name$a')).toEqual({
41
- type: 'interpolation',
42
- templates: ['$', '', ''],
43
- values: ['name', 'a'],
44
- });
45
- expect(parseInterpolation('$ $ $ $')).toEqual({
46
- type: 'interpolation',
47
- templates: ['$ $ $ $'],
48
- values: [],
49
- });
50
- });
51
- describe('should parse range', () => {
52
- it('should throw on bad range', () => {
53
- expect(() => parseInterpolation('xxx', -1, 0)).toThrow('start out of range');
54
- expect(() => parseInterpolation('xxx', 0, 4)).toThrow('length out of range');
55
- expect(() => parseInterpolation('xxx', 1, 3)).toThrow('length out of range');
56
- expect(() => parseInterpolation('xxx', 3, -1)).toThrow('length out of range');
57
- });
58
- it('should parse range', () => {
59
- expect(parseInterpolation('abcde', 0, 3)).toEqual({
60
- type: 'interpolation',
61
- templates: ['abc'],
62
- values: [],
63
- });
64
- expect(parseInterpolation('abcde', 1, 3)).toEqual({
65
- type: 'interpolation',
66
- templates: ['bcd'],
67
- values: [],
68
- });
69
- expect(parseInterpolation('abcde', 2, 3)).toEqual({
70
- type: 'interpolation',
71
- templates: ['cde'],
72
- values: [],
73
- });
74
- expect(parseInterpolation('abcde', 3, 1)).toEqual({
75
- type: 'interpolation',
76
- templates: ['d'],
77
- values: [],
78
- });
79
- expect(parseInterpolation('abcde', 5, 0)).toEqual({
80
- type: 'interpolation',
81
- templates: [''],
82
- values: [],
83
- });
84
- });
85
- });
86
- });
87
-
88
- describe('parseTemplate', () => {
89
- describe('should parse plain', () => {
90
- it('empty string', () => {
91
- // @ts-expect-error test
92
- expect(parseTemplate(null)).toEqual('');
93
- // @ts-expect-error test
94
- expect(parseTemplate(undefined)).toEqual('');
95
- expect(parseTemplate('')).toEqual('');
96
- });
97
- it('string', () => {
98
- expect(parseTemplate('Hello, world!')).toEqual('Hello, world!');
99
- expect(parseTemplate('\u0000')).toEqual('\u0000');
100
- expect(parseTemplate(' ')).toEqual(' ');
101
- // bad surrogate pair
102
- expect(parseTemplate('\uD800')).toEqual('\uD800');
103
- expect(parseTemplate('\uDC00')).toEqual('\uDC00');
104
- });
105
- it('template like (but not)', () => {
106
- expect(parseTemplate('$')).toBe('$');
107
- expect(parseTemplate('=')).toBe('=');
108
- expect(parseTemplate(' = 1+1')).toEqual(' = 1+1');
109
- expect(parseTemplate(' =1+1')).toEqual(' =1+1');
110
- expect(parseTemplate(' =1+1 ')).toEqual(' =1+1 ');
111
- expect(parseTemplate(' =1+1 ')).toEqual(' =1+1 ');
112
- expect(parseTemplate(' $aa')).toEqual(' $aa');
113
- });
114
- });
115
-
116
- it('should parse formulas', () => {
117
- expect(parseTemplate('= 1+1')).toEqual({ type: 'formula', value: ' 1+1' });
118
- expect(parseTemplate('= 1+1 ')).toEqual({ type: 'formula', value: ' 1+1 ' });
119
- expect(parseTemplate('=arg')).toEqual({ type: 'formula', value: 'arg' });
120
- expect(parseTemplate('=arg1+arg2')).toEqual({ type: 'formula', value: 'arg1+arg2' });
121
- });
122
-
123
- describe('should parse interpolation', () => {
124
- it('optimize for no interpolation', () => {
125
- expect(parseTemplate('$not')).toBe('not');
126
- expect(parseTemplate('$not interpolation')).toBe('not interpolation');
127
- expect(parseTemplate('$$')).toBe('$');
128
- expect(parseTemplate('$$$')).toBe('$$');
129
- expect(parseTemplate('$$$$')).toBe('$$$');
130
- });
131
-
132
- it('should parse simple interpolation', () => {
133
- expect(parseTemplate('$hello $name!')).toEqual({
134
- type: 'interpolation',
135
- templates: ['hello ', '!'],
136
- values: ['name'],
137
- });
138
- expect(parseTemplate('$$name')).toEqual({
139
- type: 'interpolation',
140
- templates: ['', ''],
141
- values: ['name'],
142
- });
143
- expect(parseTemplate('$$name_is_long')).toEqual({
144
- type: 'interpolation',
145
- templates: ['', ''],
146
- values: ['name_is_long'],
147
- });
148
- expect(parseTemplate('$$_001')).toEqual({
149
- type: 'interpolation',
150
- templates: ['', ''],
151
- values: ['_001'],
152
- });
153
- expect(parseTemplate('$$001$a')).toEqual({
154
- type: 'interpolation',
155
- templates: ['$001', ''],
156
- values: ['a'],
157
- });
158
- expect(parseTemplate('$$name$a')).toEqual({
159
- type: 'interpolation',
160
- templates: ['', '', ''],
161
- values: ['name', 'a'],
162
- });
163
- });
164
-
165
- it('should parse complex interpolation', () => {
166
- expect(parseTemplate('$hello ${name}!')).toEqual({
167
- type: 'interpolation',
168
- templates: ['hello ', '!'],
169
- values: ['name'],
170
- });
171
- expect(parseTemplate('$${name}')).toEqual({
172
- type: 'interpolation',
173
- templates: ['', ''],
174
- values: ['name'],
175
- });
176
- expect(parseTemplate('$${name_is_long}')).toEqual({
177
- type: 'interpolation',
178
- templates: ['', ''],
179
- values: ['name_is_long'],
180
- });
181
- expect(parseTemplate('$${_001}')).toEqual({
182
- type: 'interpolation',
183
- templates: ['', ''],
184
- values: ['_001'],
185
- });
186
- expect(parseTemplate('$1${ }2')).toEqual({
187
- type: 'interpolation',
188
- templates: ['1', '2'],
189
- values: [''],
190
- });
191
- expect(parseTemplate('$${ name }${ a}')).toEqual({
192
- type: 'interpolation',
193
- templates: ['', '', ''],
194
- values: ['name', 'a'],
195
- });
196
- });
197
-
198
- it('should parse mixed interpolation', () => {
199
- expect(parseTemplate('$$name${a}')).toEqual({
200
- type: 'interpolation',
201
- templates: ['', '', ''],
202
- values: ['name', 'a'],
203
- });
204
- expect(parseTemplate('$${name }$a')).toEqual({
205
- type: 'interpolation',
206
- templates: ['', '', ''],
207
- values: ['name', 'a'],
208
- });
209
- expect(
210
- parseInterpolation(
211
- "hello $name! I am $age years old. I'll be ${age+1} next year. And $(2*age) after $<age> years. $100",
212
- ),
213
- ).toEqual({
214
- type: 'interpolation',
215
- templates: ['hello ', '! I am ', " years old. I'll be ", ' next year. And ', ' after ', ' years. $100'],
216
- values: ['name', 'age', 'age+1', '2*age', 'age'],
217
- });
218
- });
219
-
220
- it('should not break on normal braces', () => {
221
- expect(parseTemplate('$$aa{$b}')).toEqual({
222
- type: 'interpolation',
223
- templates: ['', '{', '}'],
224
- values: ['aa', 'b'],
225
- });
226
- expect(parseTemplate('$$aa{$}')).toEqual({
227
- type: 'interpolation',
228
- templates: ['', '{$}'],
229
- values: ['aa'],
230
- });
231
- expect(parseTemplate('$$aa{}')).toEqual({
232
- type: 'interpolation',
233
- templates: ['', '{}'],
234
- values: ['aa'],
235
- });
236
- expect(parseTemplate('$$aa<xx>')).toEqual({
237
- type: 'interpolation',
238
- templates: ['', '<xx>'],
239
- values: ['aa'],
240
- });
241
- expect(parseTemplate('$$$$aa{}')).toEqual({
242
- type: 'interpolation',
243
- templates: ['$$', '{}'],
244
- values: ['aa'],
245
- });
246
- });
247
-
248
- it('should throw on invalid interpolation', () => {
249
- expect(() => parseTemplate('$${')).toThrow('Unexpected end of input');
250
- expect(() => parseTemplate('$$<')).toThrow('Unexpected end of input');
251
- expect(() => parseTemplate('$$[')).toThrow('Unexpected end of input');
252
- expect(() => parseTemplate('$$(')).toThrow('Unexpected end of input');
253
- expect(() => parseTemplate('$${123')).toThrow('Unexpected end of input');
254
- expect(() => parseTemplate('$${{123}')).toThrow('Unexpected end of input');
255
- });
256
-
257
- it('should pair braces', () => {
258
- expect(parseTemplate('$${n{am}e}')).toEqual({
259
- type: 'interpolation',
260
- templates: ['', ''],
261
- values: ['n{am}e'],
262
- });
263
- expect(parseTemplate('$$(n{a(m)e)')).toEqual({
264
- type: 'interpolation',
265
- templates: ['', ''],
266
- values: ['n{a(m)e'],
267
- });
268
- expect(parseTemplate('$${n{{a}m}e}')).toEqual({
269
- type: 'interpolation',
270
- templates: ['', ''],
271
- values: ['n{{a}m}e'],
272
- });
273
- expect(parseTemplate('$aaa${n{{a}m}e}bbb')).toEqual({
274
- type: 'interpolation',
275
- templates: ['aaa', 'bbb'],
276
- values: ['n{{a}m}e'],
277
- });
278
- expect(parseTemplate('$aaa${n{{a}m}e}${n{{a}m}e}bbb${n{{a}m}e}ccc')).toEqual({
279
- type: 'interpolation',
280
- templates: ['aaa', '', 'bbb', 'ccc'],
281
- values: ['n{{a}m}e', 'n{{a}m}e', 'n{{a}m}e'],
282
- });
283
- });
284
- });
285
- });