refactor: rewrite expression evaluation, fix #130

This commit is contained in:
harttle
2019-08-26 10:15:43 -05:00
committed by Jun Yang
parent 538610e169
commit 76019e9e18
31 changed files with 407 additions and 246 deletions
+8 -3
View File
@@ -40,17 +40,17 @@ describe('tags/if', function () {
})
describe('expression as condition', function () {
it('should support ==', async function () {
const src = '{% if 2==3 %}yes{%else%}no{%endif%}'
const src = '{% if 2 == 3 %}yes{%else%}no{%endif%}'
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('no')
})
it('should support >=', async function () {
const src = '{% if 1>=2 and one<two %}a{%endif%}'
const src = '{% if 1 >= 2 and one<two %}a{%endif%}'
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('')
})
it('should support !=', async function () {
const src = '{% if one!=two %}yes{%else%}no{%endif%}'
const src = '{% if one != two %}yes{%else%}no{%endif%}'
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('yes')
})
@@ -60,6 +60,11 @@ describe('tags/if', function () {
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('XY')
})
it('should evaluate right to left', async function () {
const src = `{% if false and false or true %}true{%endif%}`
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('')
})
})
describe('comparasion to null', function () {
it('should evaluate false for null < 10', async function () {
+4 -4
View File
@@ -20,17 +20,17 @@ describe('tags/unless', function () {
return expect(html).to.equal('yes')
})
it('should reject when tag not closed', function () {
const src = '{% unless 1>2 %}yes'
const src = '{% unless 1 > 2 %}yes'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/tag {% unless 1>2 %} not closed/)
.to.be.rejectedWith(/tag {% unless 1 > 2 %} not closed/)
})
it('should render unless when predicate yields false and else undefined', async function () {
const src = '{% unless 1>2 %}yes{%endunless%}'
const src = '{% unless 1 > 2 %}yes{%endunless%}'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('yes')
})
it('should render "" when predicate yields false and else undefined', async function () {
const src = '{% unless 1<2 %}yes{%endunless%}'
const src = '{% unless 1 < 2 %}yes{%endunless%}'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('')
})