feat: tablerowloop object

See: https://www.rubydoc.info/gems/liquid/Liquid/TablerowloopDrop
This commit is contained in:
Jun Yang
2019-03-02 06:00:30 +08:00
parent cca0306f5b
commit 3647305dcf
8 changed files with 121 additions and 129 deletions
+5 -1
View File
@@ -34,7 +34,11 @@ describe('tags/for', function () {
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('foo,bar-coo,haa-')
})
it('should output forloop', async function () {
const src = '{%for i in (1..1)%}{{forloop}}{%endfor%}'
const html = await liquid.parseAndRender(src, ctx)
return expect(html).to.equal('{"i":0,"length":1}')
})
describe('scope', function () {
it('should read super scope', async function () {
const src = '{%for a in (1..2)%}{{num}}{%endfor%}'
+35 -3
View File
@@ -69,10 +69,42 @@ describe('tags/tablerow', function () {
return expect(html).to.equal(dst)
})
it('should support tablerow with offset', async function () {
const src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>'
it('should support index0, index, rindex0, rindex', async function () {
const src = '{% tablerow i in (1..3)%}{{tablerowloop.index0}}{{tablerowloop.index}}{{tablerowloop.rindex0}}{{tablerowloop.rindex}}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">0123</td><td class="col2">1212</td><td class="col3">2301</td></tr>'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should support first, last, length', async function () {
const src = '{% tablerow i in (1..3)%}{{tablerowloop.first}} {{tablerowloop.last}} {{tablerowloop.length}}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">true false 3</td><td class="col2">false false 3</td><td class="col3">false true 3</td></tr>'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('should support col, row, col0, col_first, col_last', async function () {
const src = '{% tablerow i in (1..3)%}{{tablerowloop.col}} {{tablerowloop.col0}} {{tablerowloop.col_first}} {{tablerowloop.col_last}}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">1 0 true false</td><td class="col2">2 1 false false</td><td class="col3">3 2 false true</td></tr>'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
describe('offset', function () {
it('should support tablerow with offset', async function () {
const src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('index should also start at 1', async function () {
const src = '{% tablerow i in (1..4) cols:2 offset:2 %}{{tablerowloop.index}}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
it('col should also start at 1', async function () {
const src = '{% tablerow i in (1..4) cols:2 offset:3 %}{{tablerowloop.col}}{% endtablerow %}'
const dst = '<tr class="row1"><td class="col1">1</td></tr>'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal(dst)
})
})
})
-57
View File
@@ -1,57 +0,0 @@
import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(sinonChai)
chai.use(chaiAsPromised)
const P = require('../../../src/util/promise')
describe('util/promise', function () {
describe('.mapSeries()', async function () {
it('should resolve when all resolved', async function () {
const result = await P.mapSeries(
['first', 'second', 'third'],
(item: string) => Promise.resolve(item)
)
return expect(result).to.deep.equal(['first', 'second', 'third'])
})
it('should reject with the error that first callback rejected', () => {
const p = P.mapSeries(['first', 'second'],
(item: string) => Promise.reject(item))
return expect(p).to.rejectedWith('first')
})
it('should resolve in series', function () {
const spy1 = sinon.spy()
const spy2 = sinon.spy()
return P
.mapSeries(
['first', 'second'],
(item: string, idx: number) => new Promise(function (resolve) {
if (idx === 0) {
setTimeout(function () {
spy1()
resolve('first cb')
}, 10)
} else {
spy2()
resolve('foo')
}
}))
.then(() => expect(spy2).to.have.been.calledAfter(spy1))
})
it('should not call rest of callbacks once rejected', () => {
const spy = sinon.spy()
return P
.mapSeries(['first', 'second'], (item: string, idx: number) => {
if (idx > 0) {
spy()
}
return Promise.reject(new Error(item))
})
.catch(() => expect(spy).to.not.have.been.called)
})
})
})