From 8efb41a63183dd2abefb55e030c452f1a0d614f1 Mon Sep 17 00:00:00 2001 From: harttle Date: Sun, 26 Nov 2017 23:59:02 +0800 Subject: [PATCH] use isTruthy for and/or operators, fixes #47 --- src/operators.js | 32 +++++----- src/syntax.js | 2 +- test/liquid.js | 10 +--- test/syntax.js | 18 ++++-- test/tags/if.js | 148 +++++++++++++++++++++++++---------------------- 5 files changed, 114 insertions(+), 96 deletions(-) diff --git a/src/operators.js b/src/operators.js index 67190daba..646710f32 100644 --- a/src/operators.js +++ b/src/operators.js @@ -1,17 +1,17 @@ -var operators = { - '==': (l, r) => l === r, - '!=': (l, r) => l !== r, - '>': (l, r) => l !== null && r !== null && l > r, - '<': (l, r) => l !== null && r !== null && l < r, - '>=': (l, r) => l !== null && r !== null && l >= r, - '<=': (l, r) => l !== null && r !== null && l <= r, - 'contains': (l, r) => { - if (!l) return false - if (typeof l.indexOf !== 'function') return false - return l.indexOf(r) > -1 - }, - 'and': (l, r) => l && r, - 'or': (l, r) => l || r +module.exports = function (isTruthy) { + return { + '==': (l, r) => l === r, + '!=': (l, r) => l !== r, + '>': (l, r) => l !== null && r !== null && l > r, + '<': (l, r) => l !== null && r !== null && l < r, + '>=': (l, r) => l !== null && r !== null && l >= r, + '<=': (l, r) => l !== null && r !== null && l <= r, + 'contains': (l, r) => { + if (!l) return false + if (typeof l.indexOf !== 'function') return false + return l.indexOf(r) > -1 + }, + 'and': (l, r) => isTruthy(l) && isTruthy(r), + 'or': (l, r) => isTruthy(l) || isTruthy(r) + } } - -module.exports = operators diff --git a/src/syntax.js b/src/syntax.js index aa019bfbf..9d6e98fac 100644 --- a/src/syntax.js +++ b/src/syntax.js @@ -1,4 +1,4 @@ -const operators = require('./operators.js') +const operators = require('./operators.js')(isTruthy) const lexical = require('./lexical.js') const assert = require('../src/util/assert.js') diff --git a/test/liquid.js b/test/liquid.js index a535cb673..b75dc500a 100644 --- a/test/liquid.js +++ b/test/liquid.js @@ -70,13 +70,9 @@ describe('liquid', function () { it('should render template multiple times', function () { var template = engine.parse('{{obj}}') return engine.render(template, ctx) - .then((result) => { - expect(result).to.equal('{"foo":"bar"}') - return engine.render(template, ctx) - }) - .then((result) => { - return expect(result).to.equal('{"foo":"bar"}') - }) + .then(result => expect(result).to.equal('{"foo":"bar"}')) + .then(() => engine.render(template, ctx)) + .then((result) => expect(result).to.equal('{"foo":"bar"}')) }) it('should render filters', function () { var template = engine.parse('

{{arr | join: "_"}}

') diff --git a/test/syntax.js b/test/syntax.js index 2efd36599..bb2bbc765 100644 --- a/test/syntax.js +++ b/test/syntax.js @@ -14,6 +14,7 @@ describe('expression', function () { scope = Scope.factory({ one: 1, two: 2, + empty: '', x: 'XXX', y: undefined, z: null @@ -73,10 +74,19 @@ describe('expression', function () { expect(evalExp('"<=" == "<="', scope)).to.equal(true) }) - it('should eval complex expression', function () { - expect(evalExp('1<2 and x contains "x"', scope)).to.equal(false) - expect(evalExp('1<2 or x contains "x"', scope)).to.equal(true) - expect(evalExp('false or true', scope)).to.equal(true) + describe('complex expression', function () { + it('should support value or value', function () { + expect(evalExp('false or true', scope)).to.equal(true) + }) + it('should support < and contains', function () { + expect(evalExp('1<2 and x contains "x"', scope)).to.equal(false) + }) + it('should support < or contains', function () { + expect(evalExp('1<2 or x contains "x"', scope)).to.equal(true) + }) + it('should support value and !=', function () { + expect(evalExp('empty and empty != ""', scope)).to.equal(false) + }) }) it('should eval range expression', function () { diff --git a/test/tags/if.js b/test/tags/if.js index 66091fee8..b38886688 100644 --- a/test/tags/if.js +++ b/test/tags/if.js @@ -17,87 +17,99 @@ describe('tags/if', function () { return expect(liquid.parseAndRender(src, ctx)) .to.be.rejectedWith(/tag {% if false%} not closed/) }) - it('should treat Array truthy', function () { - var src = '{%if emptyArray%}a{%endif%}' - return expect(liquid.parseAndRender(src, ctx)) - .to.eventually.equal('a') - }) - it('should support ==', function () { - var src = '{% if 2==3 %}yes{%else%}no{%endif%}' - return expect(liquid.parseAndRender(src, ctx)) - .to.eventually.equal('no') - }) - it('should support >=', function () { - var src = '{% if 1>=2 and one=', function () { + var src = '{% if 1>=2 and one 10', function () { + var src = '{% if null > 10 %}yes{% else %}no{% endif %}' + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('no') + }) - it('should return else when comparison on null 4', function () { - var src = '{% if null > 10 %}yes{% else %}no{% endif %}' - return expect(liquid.parseAndRender(src, ctx)) - .to.eventually.equal('no') - }) + it('should evaluate false for null <= 10', function () { + var src = '{% if null <= 10 %}yes{% else %}no{% endif %}' + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('no') + }) - it('should return else when comparison on null 5', function () { - var src = '{% if 10 < null %}yes{% else %}no{% endif %}' - return expect(liquid.parseAndRender(src, ctx)) - .to.eventually.equal('no') - }) + it('should evaluate false for null >= 10', function () { + var src = '{% if null >= 10 %}yes{% else %}no{% endif %}' + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('no') + }) - it('should return else when comparison on null 6', function () { - var src = '{% if 10 <= null %}yes{% else %}no{% endif %}' - return expect(liquid.parseAndRender(src, ctx)) - .to.eventually.equal('no') - }) + it('should evaluate false for 10 < null', function () { + var src = '{% if 10 < null %}yes{% else %}no{% endif %}' + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('no') + }) - it('should return else when comparison on null 7', function () { - var src = '{% if 10 >= null %}yes{% else %}no{% endif %}' - return expect(liquid.parseAndRender(src, ctx)) - .to.eventually.equal('no') - }) + it('should evaluate false for 10 > null', function () { + var src = '{% if 10 > null %}yes{% else %}no{% endif %}' + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('no') + }) - it('should return else when comparison on null 8', function () { - var src = '{% if 10 > null %}yes{% else %}no{% endif %}' - return expect(liquid.parseAndRender(src, ctx)) - .to.eventually.equal('no') + it('should evaluate false for 10 <= null', function () { + var src = '{% if 10 <= null %}yes{% else %}no{% endif %}' + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('no') + }) + + it('should evaluate false for 10 >= null', function () { + var src = '{% if 10 >= null %}yes{% else %}no{% endif %}' + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('no') + }) }) })