case/when

This commit is contained in:
harttle
2016-06-15 14:55:00 +08:00
parent 611882ae10
commit d5ecce9c8b
12 changed files with 98 additions and 41 deletions
+3 -3
View File
@@ -16,19 +16,19 @@ describe('filter', function() {
});
it('should throw when not registered', function() {
expect(function() {
filter.parse('foo');
filter.construct('foo');
}).to.throw(/filter foo not found/);
});
it('should register a simple filter', function(){
filter.register('foo', x => x.toUpperCase());
expect(filter.parse('foo').render('foo', ctx)).to.equal('FOO');
expect(filter.construct('foo').render('foo', ctx)).to.equal('FOO');
});
it('should call filter with corrct arguments', function(){
var spy = sinon.spy();
filter.register('foo', spy);
filter.parse('foo: 33').render('foo', ctx);
filter.construct('foo: 33').render('foo', ctx);
expect(spy).to.have.been.calledWith('foo', 33);
});
});
+2 -1
View File
@@ -22,7 +22,8 @@ describe('render', function() {
});
tagToken = {
type: 'tag',
value: 'foo bar:x foo:"FOO" num:2.3'
value: 'foo bar:x foo:"FOO" num:2.3',
name: 'foo'
};
htmlToken = {
type: 'html',
+16 -4
View File
@@ -9,7 +9,7 @@ var context = require('../context.js');
describe('tag', function() {
var ctx;
before(function(){
before(function() {
ctx = context.factory({
foo: 'bar',
arr: [2, 1]
@@ -19,7 +19,11 @@ describe('tag', function() {
it('should throw when not registered', function() {
expect(function() {
tag.parse('foo');
tag.construct({
type: 'tag',
value: 'foo',
name: 'foo'
});
}).to.throw(/tag foo not found/);
});
@@ -44,7 +48,11 @@ describe('tag', function() {
tag.register('foo', {
render: spy
});
tag.parse('foo').render(tokens, ctx);
tag.construct({
type: 'tag',
value: 'foo',
name: 'foo'
}).render(tokens, ctx);
expect(spy).to.have.been.called;
});
@@ -54,7 +62,11 @@ describe('tag', function() {
tag.register('foo', {
render: spy
});
var t = tag.parse('foo aa:foo bb: arr[0] cc: 2.3');
var t = tag.construct({
type: 'tag',
value: 'foo aa:foo bb: arr[0] cc: 2.3',
name: 'foo'
});
t.render(tokens, ctx);
expect(spy).to.have.been.calledWithMatch(tokens, ctx, 'foo', {
aa: 'bar',
+19 -4
View File
@@ -4,16 +4,31 @@ const expect = chai.expect;
var liquid = require('..')();
var ctx = {
empty: '',
foo: 'bar',
arr: [-2, 'a']
};
describe('tags', function() {
it('should support assign', function() {
test('{% assign foo="bar"%}{{foo}}', 'bar');
expect(liquid.render('{% assign foo="bar"%}{{foo}}', ctx)).to.equal('bar');
});
it('should support case', function() {
expect(function() {
liquid.render('{% case "foo"%}');
}).to.throw(/case "foo" not closed/);
expect(liquid.render('{% case "foo"%}' +
'{% when "foo" %}foo{% when "bar"%}bar' +
'{%endcase%}', ctx)).to.equal('foo');
expect(liquid.render('{% case empty %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}')).to.equal('bar');
expect(liquid.render('{% case false %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}')).to.equal('');
expect(liquid.render('{% case "a" %}' +
'{% when "b" %}b{% when "c"%}c{%else %}d' +
'{%endcase%}')).to.equal('d');
});
});
function test(src, dst){
expect(liquid.render(src, ctx)).to.equal(dst);
}