fix: comparison for empty/nil, fixes #321

This commit is contained in:
Jun Yang
2021-03-13 17:14:08 +08:00
parent aff9976042
commit 99d14e76d7
6 changed files with 36 additions and 11 deletions
+8
View File
@@ -87,4 +87,12 @@ describe('Issues', function () {
const html = await engine.parseAndRender(`{{ name | default: "default name" }}`)
expect(html).to.equal('default name')
})
it('#321 comparison for empty/nil', async () => {
const engine = new Liquid()
const html = await engine.parseAndRender(
'{% if empty == nil %}true{%else%}false{%endif%}' +
'{% if nil == empty %}true{%else%}false{%endif%}'
)
expect(html).to.equal('falsefalse')
})
})
+10
View File
@@ -79,4 +79,14 @@ describe('drop/empty-drop', function () {
const html = await liquid.parseAndRender(src)
expect(html).to.equal('true')
})
it('empty != empty', async function () {
const src = '{%if empty == empty %}true{%else%}false{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('false')
})
it('empty != nil', async function () {
const src = '{%if empty == nil %}true{%else%}false{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('false')
})
})
+11 -6
View File
@@ -13,22 +13,27 @@ describe('drop/null-drop', function () {
const html = await liquid.parseAndRender('{{null}}')
expect(html).to.equal('')
})
it('undefined variable should equal to null', async function () {
it('undefined == null', async function () {
const src = '{%if foo == nil %}foo == nil{%else%}foo != nil{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('foo == nil')
})
it('nil equals blank', async function () {
const src = '{%if nil == blank %}nil == blank{%else%}nil != blank{% endif %}'
it('nil != blank', async function () {
const src = '{%if nil == blank %}eq{%else%}neq{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('nil == blank')
expect(html).to.equal('neq')
})
it('0 should not equal to null', async function () {
it('nil != empty', async function () {
const src = '{%if nil == empty %}eq{%else%}neq{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('neq')
})
it('0 != null', async function () {
const src = '{%if 0 == null %}0 == null{%else%}0 != null{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('0 != null')
})
it('nil should equal to null', async function () {
it('nil == null', async function () {
const src = '{%if nil == null %}nil == null{%else%}nil != null{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('nil == null')