-
-
Notifications
You must be signed in to change notification settings - Fork 236
/
case.spec.ts
135 lines (133 loc) · 5 KB
/
case.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
import { Liquid } from '../../../src/liquid'
describe('tags/case', function () {
const liquid = new Liquid()
it('should reject if not closed', function () {
const src = '{% case "foo"%}'
return expect(liquid.parseAndRender(src))
.rejects.toThrow(/{% case "foo"%} not closed/)
})
it('should hit the specified case', async function () {
const src = '{% case "foo"%}' +
'{% when "foo" %}foo{% when "bar"%}bar' +
'{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('foo')
})
it('should resolve blank as empty string', async function () {
const src = '{% case blank %}{% when ""%}bar{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('bar')
})
it('should resolve empty as empty string', async function () {
const src = '{% case empty %}{% when ""%}bar{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('bar')
})
it('should accept empty string as branch name', async function () {
const src = '{% case "" %}{% when ""%}bar{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('bar')
})
it('should support boolean case', async function () {
const src = '{% case false %}' +
'{% when "foo" %}foo{% when false%}bar' +
'{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('bar')
})
it('should support else branch', async function () {
const src = '{% case "a" %}' +
'{% when "b" %}b{% when "c"%}c{%else %}d' +
'{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('d')
})
describe('sync support', function () {
it('should hit the specified case', function () {
const src = '{% case "foo"%}' +
'{% when "foo" %}foo{% when "bar"%}bar' +
'{%endcase%}'
const html = liquid.parseAndRenderSync(src)
return expect(html).toBe('foo')
})
it('should support else branch', function () {
const src = '{% case "a" %}' +
'{% when "b" %}b{% when "c"%}c{%else %}d' +
'{%endcase%}'
const html = liquid.parseAndRenderSync(src)
return expect(html).toBe('d')
})
})
it('should support case with multiple values', async function () {
const src = '{% case "b" %}' +
'{% when "a", "b" %}foo' +
'{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('foo')
})
it('should render multiple matching branches', async function () {
const src = '{% case "b" %}' +
'{% when "a", "b" %}first' +
'{% when "b" %}second' +
'{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('firstsecond')
})
it('should support case with multiple values separated by or', async function () {
const src = '{% case 3 %}' +
'{% when 1 or 2 or 3 %}1 or 2 or 3' +
'{% else %}not 1 or 2 or 3' +
'{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('1 or 2 or 3')
})
it('should support case with multiple strings separated by or', async function () {
const src = '{% case "or" %}' +
'{% when "and" or "or" %}and or or' +
'{% else %}not and or or' +
'{%endcase%}'
const html = await liquid.parseAndRender(src)
return expect(html).toBe('and or or')
})
it('should not render anything after an else branch', async function () {
const html = await liquid.parseAndRenderSync('{% assign value = "this" %}' +
'{% case true %}' +
'{% when false %}don\'t show' +
'{% else %}show {{ value }}' +
'{% else %}don\'t show' +
'{% endcase %}', {})
expect(html).toEqual('show this')
})
it('should not render anything after an else branch even when first else branch is empty', async function () {
const html = await liquid.parseAndRenderSync('{% case true %}' +
'{% when false %}don\'t show' +
'{% else %}' +
'{% else %}don\'t show' +
'{% endcase %}', {})
expect(html).toEqual('')
})
it('should not render anything after an else branch even when there are \'when\' conditions', () => {
const engine = new Liquid()
const result = engine.parseAndRenderSync('{% assign value = "this" %}' +
'{% case true -%}' +
'{% when false -%}don\'t show' +
'{% else %}show {{ value }}' +
'{% else %}don\'t show' +
'{%- when true -%}don\'t show' +
'{%- endcase %}', {})
expect(result).toEqual('show this')
})
it('should apply value equal for arrays', () => {
const engine = new Liquid()
const result = engine.parseAndRenderSync(`
{%- assign x = "a,b,c" | split: "," %}
{%- assign y = "a,b,c" | split: "," %}
{% case x %}{% when y %}TRUE{% else %}FALSE{% endcase %}
{% if x == y %}TRUE{% else %}FALSE{% endif %}
`)
expect(result).toEqual(`
TRUE
TRUE
`)
})
})