Skip to content

Commit 772f914

Browse files
committed
test(template): reorganize and improve tests for block and if tags
- Remove deprecated block tag tests file and integrate tests into main compiler test - Add comprehensive tests for block tags including basic usage, whitespace control, and invalid cases - Add filtering tests for
1 parent a02ff1f commit 772f914

7 files changed

Lines changed: 200 additions & 224 deletions

File tree

Lines changed: 126 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22
import { compile } from '../test/__helper'
33

4-
describe('stripComments', async () => {
5-
it('on', async () => {
6-
expect(await compile('{{! this is a comment }}')).toMatchInlineSnapshot(
7-
`""use strict";return(async()=>{let s="";s+="<!--this is a comment-->";return s;})();"`,
8-
)
9-
})
10-
11-
it('off', async () => {
12-
expect(
13-
await compile('{{! this is a comment }}', {
14-
stripComments: false,
15-
}),
16-
).toMatchInlineSnapshot(
17-
`""use strict";return(async()=>{let s="";s+="<!--this is a comment-->";return s;})();"`,
18-
)
19-
})
20-
})
21-
22-
describe('strictMode', async () => {
23-
it('on', async () => {
24-
expect(await compile('')).toMatchInlineSnapshot(
25-
`""use strict";return(async()=>{let s="";return s;})();"`,
26-
)
27-
})
28-
29-
it('off', async () => {
30-
expect(
31-
await compile('', {
32-
strictMode: false,
33-
}),
34-
).toMatchInlineSnapshot(`"return(async()=>{let s="";return s;})();"`)
35-
})
36-
})
37-
384
it('empty', async () => {
395
expect(await compile('')).toMatchInlineSnapshot(
406
`""use strict";return(async()=>{let s="";return s;})();"`,
@@ -67,20 +33,22 @@ it('translate', async () => {
6733
)
6834
})
6935

70-
it('if/elif/else', async () => {
71-
expect((await compile(
72-
'{{ #if x }}x{{ elif y }}y{{ else }}z{{ /if }}',
73-
))).toMatchInlineSnapshot(
74-
`""use strict";return(async()=>{let s="";if(c.x){s+="x";}else if(c.y){s+="y";}else{s+="z";}return s;})();"`,
36+
it('stripComments off', async () => {
37+
expect(
38+
await compile('{{! this is a comment }}', {
39+
stripComments: false,
40+
}),
41+
).toMatchInlineSnapshot(
42+
`""use strict";return(async()=>{let s="";s+="<!--this is a comment-->";return s;})();"`,
7543
)
7644
})
7745

78-
it('if/elif/else nested', async () => {
79-
expect((await compile(
80-
'{{ #if x }}{{ #if x }}x{{ elif y }}y{{ else }}z{{ /if }}{{ elif y }}{{ #if x }}x{{ elif y }}y{{ else }}z{{ /if }}{{ else }}{{ #if x }}x{{ elif y }}y{{ else }}z{{ /if }}{{ /if }}',
81-
))).toMatchInlineSnapshot(
82-
`""use strict";return(async()=>{let s="";if(c.x){if(c.x){s+="x";}else if(c.y){s+="y";}else{s+="z";}}else if(c.y){if(c.x){s+="x";}else if(c.y){s+="y";}else{s+="z";}}else{if(c.x){s+="x";}else if(c.y){s+="y";}else{s+="z";}}return s;})();"`,
83-
)
46+
it('strictMode off', async () => {
47+
expect(
48+
await compile('', {
49+
strictMode: false,
50+
}),
51+
).toMatchInlineSnapshot(`"return(async()=>{let s="";return s;})();"`)
8452
})
8553

8654
it('invalid', async () => {
@@ -106,3 +74,116 @@ it('invalid', async () => {
10674
`,
10775
)
10876
})
77+
78+
describe('block', () => {
79+
it('basic', async () => {
80+
expect(
81+
await compile(
82+
'{{ #block title }}1{{ /block }}',
83+
),
84+
).toMatchInlineSnapshot(
85+
`""use strict";return(async()=>{let s="";s+="1";return s;})();"`,
86+
)
87+
expect(
88+
await compile(
89+
'{{ #block title }}{{ super }}{{ /block }}',
90+
),
91+
).toMatchInlineSnapshot(
92+
`""use strict";return(async()=>{let s="";return s;})();"`,
93+
)
94+
expect(
95+
await compile(
96+
'{{ #block title }}{{ super }}{{ /block }}{{ #block title }}{{ super }}{{ /block }}',
97+
),
98+
).toMatchInlineSnapshot(
99+
`""use strict";return(async()=>{let s="";return s;})();"`,
100+
)
101+
expect(
102+
await compile(
103+
'{{ #block title }}1{{ /block }}{{ #block title }}2{{ /block }}{{ #block title }}{{ super }}3{{ /block }}',
104+
),
105+
).toMatchInlineSnapshot(
106+
`""use strict";return(async()=>{let s="";s+="2";s+="3";return s;})();"`,
107+
)
108+
expect(
109+
await compile(
110+
'{{ #block title }}1{{ /block }}{{ #block title }}2{{ super }}{{ /block }}{{ #block title }}{{ super }}3{{ /block }}',
111+
),
112+
).toMatchInlineSnapshot(
113+
`""use strict";return(async()=>{let s="";s+="2";s+="1";s+="3";return s;})();"`,
114+
)
115+
expect(
116+
await compile(
117+
' {{- #block title }} 1 {{- /block }} {{ #block title -}} 2 {{ super -}} {{ /block -}} {{- #block title -}} {{- super -}} 3 {{- /block -}} ',
118+
),
119+
).toMatchInlineSnapshot(
120+
`""use strict";return(async()=>{let s="";s+="2";s+=" 1";s+=" ";s+="3";s+=" ";s+=" ";return s;})();"`,
121+
)
122+
})
123+
124+
it('whitespace control', async () => {
125+
expect(
126+
await compile(' {{ #block name }} x {{ /block }} '),
127+
).toMatchInlineSnapshot(
128+
`""use strict";return(async()=>{let s="";s+=" ";s+=" x ";s+=" ";return s;})();"`,
129+
)
130+
expect(
131+
await compile(' {{ #block name -}} x {{- /block }} '),
132+
).toMatchInlineSnapshot(
133+
`""use strict";return(async()=>{let s="";s+=" ";s+="x";s+=" ";return s;})();"`,
134+
)
135+
expect(
136+
await compile(' {{- #block name -}} x {{- /block -}} '),
137+
).toMatchInlineSnapshot(
138+
`""use strict";return(async()=>{let s="";s+="x";return s;})();"`,
139+
)
140+
expect(
141+
await compile(' {{- #block name -}} x {{ super }} y {{- /block -}} '),
142+
).toMatchInlineSnapshot(
143+
`""use strict";return(async()=>{let s="";s+="x ";s+=" y";return s;})();"`,
144+
)
145+
expect(
146+
await compile(' {{- #block name -}} x {{- super -}} y {{- /block -}} '),
147+
).toMatchInlineSnapshot(
148+
`""use strict";return(async()=>{let s="";s+="x";s+="y";return s;})();"`,
149+
)
150+
expect(
151+
await compile(' {{- #block name }} x {{- super -}} y {{ /block -}} '),
152+
).toMatchInlineSnapshot(
153+
`""use strict";return(async()=>{let s="";s+=" x";s+="y ";return s;})();"`,
154+
)
155+
})
156+
157+
it('invalid', async () => {
158+
expect(
159+
await compile('{{ #block }}{{ /block }}', { debug: true }),
160+
).toMatchInlineSnapshot(
161+
`"block tag must have a value"`,
162+
)
163+
expect(
164+
await compile('{{ #block }}{{ /block }}', {
165+
debug: true,
166+
}),
167+
).toMatchInlineSnapshot(`"block tag must have a value"`)
168+
expect(
169+
await compile('{{ #if x }}{{ #block title }}{{ /block }}{{ /if }}', { debug: true }),
170+
).toMatchInlineSnapshot(
171+
`""use strict";return(async()=>{let s="";if(c.x){}return s;})();"`,
172+
)
173+
expect(
174+
await compile('{{ #if x }}{{ #block title }}{{ /block }}{{ /if }}', {
175+
debug: true,
176+
}),
177+
).toMatchInlineSnapshot(`""use strict";return(async()=>{let s="";if(c.x){}return s;})();"`)
178+
expect(
179+
await compile('{{ #if x }}{{ super }}{{ /if }}', { debug: true }),
180+
).toMatchInlineSnapshot(
181+
`""use strict";return(async()=>{let s="";if(c.x){}return s;})();"`,
182+
)
183+
expect(
184+
await compile('{{ #if x }}{{ super }}{{ /if }}', { debug: true }),
185+
).toMatchInlineSnapshot(
186+
`""use strict";return(async()=>{let s="";if(c.x){}return s;})();"`,
187+
)
188+
})
189+
})

packages/template/src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export const FILTERS = 'f' // Filters can be async
88
export const ESCAPE = 'e'
99
export const HELPERS = 'h'
1010

11+
export const BLOCK = ['block', '#block']
12+
export const SUPER = ['super']
13+
export const END_BLOCK = ['end_block', 'endblock', '/block']
14+
1115
export const config: Required<Config> = {
1216
debug: false,
1317
globals: {

packages/template/src/tags/block.test.ts

Lines changed: 0 additions & 113 deletions
This file was deleted.

packages/template/src/tags/block.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/template/src/tags/if.test.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,30 +115,12 @@ it('elif', async () => {
115115
)
116116
})
117117

118-
describe('filter', async () => {
119-
it('basic', async () => {
120-
expect(
121-
await compile('{{ #if name | length }}{{= name }}{{ /if }}'),
122-
).toMatchInlineSnapshot(
123-
`""use strict";return(async()=>{let s="";if(await f.length.call(c,c.name,c)){s+=e(c.name);}return s;})();"`,
124-
)
125-
})
126-
127-
it('multiple', async () => {
128-
expect(
129-
await compile('{{ #if name | length | odd }}{{= name }}{{ /if }}'),
130-
).toMatchInlineSnapshot(
131-
`""use strict";return(async()=>{let s="";if(await f.odd.call(c,await f.length.call(c,c.name,c),c)){s+=e(c.name);}return s;})();"`,
132-
)
133-
})
134-
135-
it('w/ args', async () => {
136-
expect(
137-
await compile('{{ #if names | join: "" == "foobar" }}yes{{ /if }}'),
138-
).toMatchInlineSnapshot(
139-
`""use strict";return(async()=>{let s="";if(await f.join.call(c,c.names,"")==="foobar"){s+="yes";}return s;})();"`,
140-
)
141-
})
118+
it('nested', async () => {
119+
expect((await compile(
120+
'{{ #if x }}{{ #if x }}x{{ elif y }}y{{ else }}z{{ /if }}{{ elif y }}{{ #if x }}x{{ elif y }}y{{ else }}z{{ /if }}{{ else }}{{ #if x }}x{{ elif y }}y{{ else }}z{{ /if }}{{ /if }}',
121+
))).toMatchInlineSnapshot(
122+
`""use strict";return(async()=>{let s="";if(c.x){if(c.x){s+="x";}else if(c.y){s+="y";}else{s+="z";}}else if(c.y){if(c.x){s+="x";}else if(c.y){s+="y";}else{s+="z";}}else{if(c.x){s+="x";}else if(c.y){s+="y";}else{s+="z";}}return s;})();"`,
123+
)
142124
})
143125

144126
it('whitespace control', async () => {
@@ -219,3 +201,29 @@ it('invalid', async () => {
219201
`,
220202
)
221203
})
204+
205+
describe('filter', async () => {
206+
it('basic', async () => {
207+
expect(
208+
await compile('{{ #if name | length }}{{= name }}{{ /if }}'),
209+
).toMatchInlineSnapshot(
210+
`""use strict";return(async()=>{let s="";if(await f.length.call(c,c.name,c)){s+=e(c.name);}return s;})();"`,
211+
)
212+
})
213+
214+
it('multiple', async () => {
215+
expect(
216+
await compile('{{ #if name | length | odd }}{{= name }}{{ /if }}'),
217+
).toMatchInlineSnapshot(
218+
`""use strict";return(async()=>{let s="";if(await f.odd.call(c,await f.length.call(c,c.name,c),c)){s+=e(c.name);}return s;})();"`,
219+
)
220+
})
221+
222+
it('w/ args', async () => {
223+
expect(
224+
await compile('{{ #if names | join: "" == "foobar" }}yes{{ /if }}'),
225+
).toMatchInlineSnapshot(
226+
`""use strict";return(async()=>{let s="";if(await f.join.call(c,c.names,"")==="foobar"){s+="yes";}return s;})();"`,
227+
)
228+
})
229+
})

0 commit comments

Comments
 (0)