11import { describe , expect , it } from 'vitest'
22import { 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-
384it ( '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
8654it ( '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+ } )
0 commit comments