mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
fix existing tags
new tags: for,cycle
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
const chai = require("chai");
|
||||
const sinonChai = require("sinon-chai");
|
||||
const sinon = require("sinon");
|
||||
const expect = chai.expect;
|
||||
|
||||
chai.use(sinonChai);
|
||||
|
||||
var expression = require('../expression.js');
|
||||
var Scope = require('../scope.js');
|
||||
var evalExp = expression.evalExp;
|
||||
var evalValue = expression.evalValue;
|
||||
|
||||
describe('expression', function() {
|
||||
var scope;
|
||||
|
||||
beforeEach(function() {
|
||||
scope = Scope.factory({
|
||||
one: 1,
|
||||
two: 2,
|
||||
x: 'XXX'
|
||||
});
|
||||
});
|
||||
|
||||
it('should eval literals', function() {
|
||||
expect(evalValue('2.3')).to.equal(2.3);
|
||||
expect(evalValue('"foo"')).to.equal("foo");
|
||||
});
|
||||
|
||||
it('should throw on illegal expression', function() {
|
||||
expect(function() {
|
||||
evalExp('1 contains "x"', scope);
|
||||
}).to.throw();
|
||||
});
|
||||
|
||||
it('should eval variables', function() {
|
||||
expect(evalValue('23', scope)).to.equal(23);
|
||||
expect(evalValue('one', scope)).to.equal(1);
|
||||
expect(evalValue('x', scope)).to.equal('XXX');
|
||||
});
|
||||
|
||||
it('should eval simple expression', function() {
|
||||
expect(evalExp('1<2', scope)).to.equal(true);
|
||||
expect(evalExp('2<=2', scope)).to.equal(true);
|
||||
expect(evalExp('one<=two', scope)).to.equal(true);
|
||||
expect(evalExp('x contains "x"', scope)).to.equal(false);
|
||||
expect(evalExp('x contains "X"', scope)).to.equal(true);
|
||||
expect(evalExp('x contains z', scope)).to.equal(true);
|
||||
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);
|
||||
});
|
||||
|
||||
it("should eval range expression", function() {
|
||||
expect(evalExp('(2..4)', scope)).to.deep.equal([2,3,4]);
|
||||
expect(evalExp('(two..4)', scope)).to.deep.equal([2,3,4]);
|
||||
});
|
||||
});
|
||||
+6
-11
@@ -17,6 +17,12 @@ describe('identifier', function() {
|
||||
identifier.isLiteral('23').should.equal(true);
|
||||
});
|
||||
|
||||
it("should test range literal", function() {
|
||||
identifier.isRange("(12..32)").should.equal(true);
|
||||
identifier.isRange("(12..foo)").should.equal(true);
|
||||
identifier.isRange("(foo.bar..foo)").should.equal(true);
|
||||
});
|
||||
|
||||
it('should test string literal', function() {
|
||||
identifier.isLiteral('""').should.equal(true);
|
||||
identifier.isLiteral('"a\'b"').should.equal(true);
|
||||
@@ -24,10 +30,6 @@ describe('identifier', function() {
|
||||
identifier.isLiteral("'a bcd'").should.equal(true);
|
||||
});
|
||||
|
||||
it("should test range literal", function() {
|
||||
identifier.isLiteral("(12..32)").should.equal(true);
|
||||
});
|
||||
|
||||
it("should test variable", function() {
|
||||
identifier.isVariable("foo").should.equal(true);
|
||||
identifier.isVariable("foo.bar.foo").should.equal(true);
|
||||
@@ -65,11 +67,4 @@ describe('identifier', function() {
|
||||
identifier.parseLiteral('"ab\'c"').should.equal("ab\'c");
|
||||
});
|
||||
|
||||
it("should parse range literal", function() {
|
||||
var arr = identifier.parseLiteral("(12..32)");
|
||||
arr.length.should.equal(20);
|
||||
arr[0].should.equal(12);
|
||||
arr[1].should.equal(13);
|
||||
arr[19].should.equal(31);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,7 +10,6 @@ var Scope = require('../scope.js');
|
||||
var filter = require('../filter')();
|
||||
var Render = require('../render.js')(filter, tag);
|
||||
var render = Render.renderTemplates;
|
||||
var evalExp = Render.evalExp;
|
||||
var evalFilter = Render.evalFilter;
|
||||
|
||||
describe('render', function() {
|
||||
@@ -18,9 +17,6 @@ describe('render', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
scope = Scope.factory({
|
||||
one: 1,
|
||||
two: 2,
|
||||
x: 'XXX',
|
||||
foo: {
|
||||
bar: ['a', 2]
|
||||
}
|
||||
@@ -51,19 +47,4 @@ describe('render', function() {
|
||||
filter.register('time', (l, r) => l + 3 * r);
|
||||
expect(evalFilter('foo.bar[0] | date: "b" | time:2', scope)).to.equal('ab6');
|
||||
});
|
||||
|
||||
it('should eval expression', function() {
|
||||
expect(evalExp('1<2', scope)).to.equal(true);
|
||||
expect(evalExp('2<=2', scope)).to.equal(true);
|
||||
expect(evalExp('one<=two', scope)).to.equal(true);
|
||||
expect(function() {
|
||||
evalExp('1 contains "x"', scope);
|
||||
}).to.throw();
|
||||
expect(evalExp('x contains "x"', scope)).to.equal(false);
|
||||
expect(evalExp('x contains "X"', scope)).to.equal(true);
|
||||
expect(evalExp('x contains z', scope)).to.equal(true);
|
||||
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('"<=" == "<="', scope)).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
+5
-6
@@ -13,12 +13,6 @@ describe('scope', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse literal', function() {
|
||||
scope.get('2.3').should.equal(2.3);
|
||||
scope.get('(2..4)').should.deep.equal([2,3]);
|
||||
scope.get('"foo"').should.equal("foo");
|
||||
});
|
||||
|
||||
it('should get property', function() {
|
||||
scope.get('foo').should.equal('bar');
|
||||
});
|
||||
@@ -28,6 +22,11 @@ describe('scope', function() {
|
||||
scope.get('foo').should.equal('FOO');
|
||||
});
|
||||
|
||||
it('should set child property', function() {
|
||||
scope.set('oo.bar', 'FOO');
|
||||
scope.get('oo.bar').should.equal('FOO');
|
||||
});
|
||||
|
||||
it('should get desendent property', function() {
|
||||
scope.get('bar[0]').should.equal('a');
|
||||
scope.get('bar[1].b').should.deep.equal([1, 2]);
|
||||
|
||||
+2
-2
@@ -52,7 +52,7 @@ describe('tag', function() {
|
||||
type: 'tag',
|
||||
value: 'foo',
|
||||
name: 'foo'
|
||||
}).render(scope);
|
||||
}).render(scope, {});
|
||||
expect(spy).to.have.been.called;
|
||||
});
|
||||
|
||||
@@ -68,7 +68,7 @@ describe('tag', function() {
|
||||
name: 'foo',
|
||||
args: 'aa:foo bb: arr[0] cc: 2.3'
|
||||
};
|
||||
tag.construct(token).render(scope);
|
||||
tag.construct(token).render(scope, {});
|
||||
expect(spy).to.have.been.calledWithMatch(scope, {
|
||||
aa: 'bar',
|
||||
bb: 2,
|
||||
|
||||
+88
-33
@@ -2,7 +2,7 @@ const chai = require("chai");
|
||||
const expect = chai.expect;
|
||||
|
||||
var liquid = require('..')(),
|
||||
ctx;
|
||||
ctx, src, dst;
|
||||
|
||||
function test(src, dst) {
|
||||
expect(liquid.render(src, ctx)).to.equal(dst);
|
||||
@@ -22,30 +22,29 @@ describe('tags', function() {
|
||||
leq: '<=',
|
||||
empty: '',
|
||||
foo: 'bar',
|
||||
arr: [-2, 'a', {
|
||||
foo: 'bar'
|
||||
}],
|
||||
arr: [-2, 'a'],
|
||||
alpha: ['a', 'b', 'c'],
|
||||
emptyArray: []
|
||||
};
|
||||
});
|
||||
//it('should support assign', function() {
|
||||
//test('{% assign foo="bar"%}{{foo}}', 'bar');
|
||||
//});
|
||||
//it('should support case', function() {
|
||||
//testThrow('{% case "foo"%}', /case "foo" not closed/);
|
||||
//test('{% case "foo"%}' +
|
||||
//'{% when "foo" %}foo{% when "bar"%}bar' +
|
||||
//'{%endcase%}', 'foo');
|
||||
//test('{% case empty %}' +
|
||||
//'{% when "foo" %}foo{% when ""%}bar' +
|
||||
//'{%endcase%}', 'bar');
|
||||
//test('{% case false %}' +
|
||||
//'{% when "foo" %}foo{% when ""%}bar' +
|
||||
//'{%endcase%}', '');
|
||||
//test('{% case "a" %}' +
|
||||
//'{% when "b" %}b{% when "c"%}c{%else %}d' +
|
||||
//'{%endcase%}', 'd');
|
||||
//});
|
||||
it('should support assign', function() {
|
||||
test('{% assign foo="bar"%}{{foo}}', 'bar');
|
||||
});
|
||||
it('should support case', function() {
|
||||
testThrow('{% case "foo"%}', /{% case "foo"%} not closed/);
|
||||
test('{% case "foo"%}' +
|
||||
'{% when "foo" %}foo{% when "bar"%}bar' +
|
||||
'{%endcase%}', 'foo');
|
||||
test('{% case empty %}' +
|
||||
'{% when "foo" %}foo{% when ""%}bar' +
|
||||
'{%endcase%}', 'bar');
|
||||
test('{% case false %}' +
|
||||
'{% when "foo" %}foo{% when ""%}bar' +
|
||||
'{%endcase%}', '');
|
||||
test('{% case "a" %}' +
|
||||
'{% when "b" %}b{% when "c"%}c{%else %}d' +
|
||||
'{%endcase%}', 'd');
|
||||
});
|
||||
|
||||
it('should support if', function() {
|
||||
testThrow('{% if false%}yes', /tag {% if false%} not closed/);
|
||||
@@ -68,17 +67,73 @@ describe('tags', function() {
|
||||
testThrow('{% capture = %}{%endcapture%}', /= not valid identifier/);
|
||||
});
|
||||
|
||||
//it('should support for', function() {
|
||||
//test('{%for i in arr%}{{"a" | capitalize}}{%endcapture%}{{f}}', 'A');
|
||||
//});
|
||||
it('should support for', function() {
|
||||
test('{%for c in alpha%}{{c}}{%endfor%}', 'abc');
|
||||
});
|
||||
|
||||
//it('should support increment', function() {
|
||||
//test('{% increment foo %}{%increment foo%}{{foo}}', '2');
|
||||
//test('{% increment one %}{{one}}', '2');
|
||||
//});
|
||||
it('should support for with forloop', function() {
|
||||
src = '{%for c in alpha%}' +
|
||||
'{{forloop.first}}.{{forloop.index}}.{{forloop.index0}}.' +
|
||||
'{{forloop.last}}.{{forloop.length}}.' +
|
||||
'{{forloop.rindex}}.{{forloop.rindex0}}' +
|
||||
'{{c}}\n' +
|
||||
'{%endfor%}';
|
||||
dst = 'true.1.0.false.3.3.2a\n' +
|
||||
'false.2.1.false.3.2.1b\n' +
|
||||
'false.3.2.true.3.1.0c\n';
|
||||
test(src, dst);
|
||||
});
|
||||
|
||||
//it('should support decrement', function() {
|
||||
//test('{% decrement foo %}{%decrement foo%}{{foo}}', '-2');
|
||||
//test('{% decrement one %}{{one}}', '0');
|
||||
//});
|
||||
it('should support for with continue and break', function() {
|
||||
src = '{% for i in (1..5) %}' +
|
||||
'{% if i == 4 %}{% continue %}' +
|
||||
'{% else %}{{ i }}' +
|
||||
'{% endif %}' +
|
||||
'{% endfor %}';
|
||||
test(src, '1235');
|
||||
src = '{% for i in (one..5) %}' +
|
||||
'{% if i == 4 %}{% break %}{% endif %}' +
|
||||
'{{ i }}' +
|
||||
'{% endfor %}';
|
||||
test(src, '123');
|
||||
});
|
||||
|
||||
it('should support for with limit and offset', function() {
|
||||
src = '{% for i in (1..5) limit:2 %}{{ i }}{% endfor %}';
|
||||
test(src, '12');
|
||||
src = '{% for i in (1..10) limit:2 offset:5%}{{ i }}{% endfor %}';
|
||||
test(src, '67');
|
||||
});
|
||||
|
||||
it('should support for reversed', function() {
|
||||
src = '{% for i in (1..5) limit:2 reversed %}{{ i }}{% endfor %}';
|
||||
test(src, '21');
|
||||
});
|
||||
|
||||
it('should support cycle', function() {
|
||||
src = "{% cycle '1', '2', '3' %}";
|
||||
test(src + src + src + src, '1231');
|
||||
});
|
||||
|
||||
it('should support cycle in for block', function() {
|
||||
src = '{% for i in (1..5) %}{% cycle one, "e"%}{% endfor %}';
|
||||
test(src, '1e1e1');
|
||||
});
|
||||
|
||||
it('should support cycle group', function() {
|
||||
src = "{% cycle one: '1', '2', '3'%}" +
|
||||
"{% cycle 1: '1', '2', '3'%}" +
|
||||
"{% cycle 2: '1', '2', '3'%}";
|
||||
test(src, '121');
|
||||
});
|
||||
|
||||
it('should support increment', function() {
|
||||
test('{% increment foo %}{%increment foo%}{{foo}}', '2');
|
||||
test('{% increment one %}{{one}}', '2');
|
||||
});
|
||||
|
||||
it('should support decrement', function() {
|
||||
test('{% decrement foo %}{%decrement foo%}{{foo}}', '-2');
|
||||
test('{% decrement one %}{{one}}', '0');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user