cover all

This commit is contained in:
harttle
2017-08-14 22:07:08 +08:00
parent 7904f823a8
commit 72597c52e5
14 changed files with 268 additions and 205 deletions
+6
View File
@@ -34,6 +34,12 @@ describe('liquid', function () {
afterEach(function () {
mock.restore()
})
describe('Liquid', function () {
it('should ignore invalid root option', function () {
var liquid = Liquid({ root: /regex/ })
expect(liquid.options.root).to.deep.equal([])
})
})
describe('{{output}}', function () {
it('should output object', function () {
return expect(engine.parseAndRender('{{obj}}', ctx)).to.eventually.equal('{"foo":"bar"}')
+20 -3
View File
@@ -98,7 +98,13 @@ describe('scope', function () {
expect(scope.get('bar[foo]')).to.equal('coo')
})
it('should support nested case', function () {
it('should return undefined when not exist', function () {
expect(scope.get('foo.foo.foo')).to.be.undefined
})
})
describe('#set', function () {
it('should set nested value', function () {
scope.set('posts', {
'first': {
'name': 'A Nice Day'
@@ -109,8 +115,12 @@ describe('scope', function () {
})
expect(scope.get('posts[category.diary[0]].name'), 'A Nice Day')
})
})
it('should create parents if needed', function () {
scope.set('foo.bar.coo', 'COO')
expect(scope.get('foo.bar.coo'), 'COO')
})
})
describe('strict_variables', function () {
var scope
beforeEach(function () {
@@ -118,12 +128,19 @@ describe('scope', function () {
strict_variables: true
})
})
it('should throw undefined in strict mode', function () {
it('should throw when variable not defined', function () {
function fn () {
scope.get('notdefined')
}
expect(fn).to.throw(/undefined variable: notdefined/)
})
it('should throw when deep variable not exist', function () {
scope.set('foo', 'bar')
function fn () {
scope.get('foo.bar.not.defined')
}
expect(fn).to.throw(/undefined variable: not/)
})
it('should find variable in parent scope', function () {
scope.set('foo', 'foo')
scope.push({
+51 -33
View File
@@ -7,6 +7,9 @@ describe('tags/for', function () {
var liquid, ctx
before(function () {
liquid = Liquid()
liquid.registerTag('throwingTag', {
render: function () { throw new Error('intended render error') }
})
ctx = {
one: 1,
// eslint-disable-next-line
@@ -30,10 +33,18 @@ describe('tags/for', function () {
.to.eventually.equal('foo-coo-')
})
it('should throw when for not closed', function () {
var src = '{%for c in alpha%}{{c}}'
return expect(liquid.parseAndRender(src, ctx))
.to.be.rejectedWith(/tag .* not closed/)
describe('illegal', function () {
it('should reject when for not closed', function () {
var src = '{%for c in alpha%}{{c}}'
return expect(liquid.parseAndRender(src, ctx))
.to.be.rejectedWith(/tag .* not closed/)
})
it('should reject when inner templates rejected', function () {
var src = '{%for c in alpha%}{%throwingTag%}{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.be.rejectedWith(/intended render error/)
})
})
describe('else', function () {
@@ -43,6 +54,12 @@ describe('tags/for', function () {
.to.eventually.equal('b')
})
it('should treat non-empty string as one single element', function () {
var src = '{%for c in "abc"%}x{{c}}{%else%}y{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('xabc')
})
it('should goto else for empty string', function () {
var src = '{%for c in ""%}a{%else%}b{%endfor%}'
return expect(liquid.parseAndRender(src, ctx))
@@ -92,42 +109,43 @@ describe('tags/for', function () {
})
it('should support for with break', function () {
var src = '{% for i in (one..5) %}' +
'{% if i == 4 %}{% break %}{% endif %}' +
'{{ i }}' +
'{% endfor %}'
// return liquid.parseAndRender(src, ctx).catch(e => {
// console.log(e.stack);
// });
'{% if i == 4 %}{% break %}{% endif %}' +
'{{ i }}' +
'{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('123')
})
it('should support for with limit', function () {
var src = '{% for i in (1..5) limit:2 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('12')
})
it('should support for with limit and offset', function () {
var src = '{% for i in (1..10) limit:2 offset:5%}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('67')
describe('limit', function () {
it('should support for with limit', function () {
var src = '{% for i in (1..5) limit:2 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('12')
})
it('should support for with limit and offset', function () {
var src = '{% for i in (1..10) limit:2 offset:5%}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('67')
})
})
it('should support for reversed in the last position', function () {
var src = '{% for i in (1..5) limit:2 reversed %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21')
})
describe('reverse', function () {
it('should support for reversed in the last position', function () {
var src = '{% for i in (1..5) limit:2 reversed %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21')
})
it('should support for reversed in the first position', function () {
var src = '{% for i in (1..5) reversed limit:2 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21')
})
it('should support for reversed in the first position', function () {
var src = '{% for i in (1..5) reversed limit:2 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21')
})
it('should support for reversed in the middle position', function () {
var src = '{% for i in (1..5) offset:2 reversed limit:4 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('543')
it('should support for reversed in the middle position', function () {
var src = '{% for i in (1..5) offset:2 reversed limit:4 %}{{ i }}{% endfor %}'
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('543')
})
})
})
+18 -12
View File
@@ -12,9 +12,9 @@ describe('tags/tablerow', function () {
alpha: ['a', 'b', 'c']
}
var dst = '<table>' +
'<tr class="row1"><td class="col1">a</td><td class="col2">b</td></tr>' +
'<tr class="row2"><td class="col1">c</td></tr>' +
'</table>'
'<tr class="row1"><td class="col1">a</td><td class="col2">b</td></tr>' +
'<tr class="row2"><td class="col1">c</td></tr>' +
'</table>'
return expect(liquid.parseAndRender(src, ctx)).to.eventually.equal(dst)
})
@@ -24,6 +24,12 @@ describe('tags/tablerow', function () {
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support empty array', function () {
var src = '{% tablerow i in alpha.z cols:2 %}{{ i }}{% endtablerow %}'
var dst = '<table></table>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should throw when tablerow not closed', function () {
var src = '{% tablerow i in (1..0) cols:2 %}{{ i }}'
return expect(liquid.parseAndRender(src))
@@ -33,10 +39,10 @@ describe('tags/tablerow', function () {
it('should support tablerow with range', function () {
var src = '{% tablerow i in (1..5) cols:2 %}{{ i }}{% endtablerow %}'
var dst = '<table>' +
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td><td class="col2">4</td></tr>' +
'<tr class="row3"><td class="col1">5</td></tr>' +
'</table>'
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td><td class="col2">4</td></tr>' +
'<tr class="row3"><td class="col1">5</td></tr>' +
'</table>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
@@ -54,17 +60,17 @@ describe('tags/tablerow', function () {
it('should support tablerow with limit', function () {
var src = '{% tablerow i in (1..5) cols:2 limit:3 %}{{ i }}{% endtablerow %}'
var dst = '<table>' +
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td></tr>' +
'</table>'
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td></tr>' +
'</table>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support tablerow with offset', function () {
var src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}'
var dst = '<table>' +
'<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>' +
'</table>'
'<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>' +
'</table>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
})
+18
View File
@@ -0,0 +1,18 @@
const chai = require('chai')
const expect = chai.expect
const assert = require('../../src/util/assert.js')
describe('assert', function () {
it('should not throw if predicate is truthy', function () {
var fn = () => assert('foo', 'bar')
expect(fn).to.not.throw()
})
it('should not throw if predicate is truthy', function () {
var fn = () => assert('', 'bar')
expect(fn).to.throw(/bar/)
})
it('should populate default message', function () {
var fn = () => assert(false)
expect(fn).to.throw(/expect false to be true/)
})
})
+8
View File
@@ -72,6 +72,14 @@ describe('util/underscore', function () {
expect(log).to.have.been.calledWith('[foo]', 'bar')
})
})
describe('.range()', function () {
it('should return a range of integers', function () {
expect(_.range(3, 5)).to.deep.equal([3, 4])
})
it('should treat start as 0 if omitted', function () {
expect(_.range(3)).to.deep.equal([0, 1, 2])
})
})
describe('.assign()', function () {
it('should handle null dst', function () {
expect(_.assign(null, {