feat: passing liquid to FilterImpl, closes #277

This commit is contained in:
harttle
2020-12-08 00:17:27 +08:00
parent 2bbf50171b
commit f9f595f0f4
11 changed files with 52 additions and 25 deletions
+4 -2
View File
@@ -14,8 +14,9 @@ const expect = chai.expect
describe('filter', function () {
let ctx: Context
let filters: FilterMap
const liquid = {} as any
beforeEach(function () {
filters = new FilterMap(false)
filters = new FilterMap(false, liquid)
ctx = new Context()
})
it('should create default filter if not registered', async function () {
@@ -34,12 +35,13 @@ describe('filter', function () {
await toThenable(filters.create('foo', [thirty]).render('foo', ctx))
expect(spy).to.have.been.calledWith('foo', 30)
})
it('should call filter impl with correct this arg', async function () {
it('should call filter impl with correct this', async function () {
const spy = sinon.spy()
filters.set('foo', spy)
const thirty = new NumberToken(new IdentifierToken('33', 0, 2), undefined)
await toThenable(filters.create('foo', [thirty]).render('foo', ctx))
expect(spy).to.have.been.calledOn(sinon.match.has('context', ctx))
expect(spy).to.have.been.calledOn(sinon.match.has('liquid', liquid))
})
it('should render a simple filter', async function () {
filters.set('upcase', x => x.toUpperCase())
+6 -5
View File
@@ -9,9 +9,10 @@ const expect = chai.expect
describe('Output', function () {
const emitter: any = { write: (html: string) => (emitter.html += html), html: '' }
const liquid = {} as any
let filters: FilterMap
beforeEach(function () {
filters = new FilterMap(false)
filters = new FilterMap(false, liquid)
emitter.html = ''
})
@@ -19,25 +20,25 @@ describe('Output', function () {
const scope = new Context({
foo: { obj: { arr: ['a', 2] } }
})
const output = new Output({ content: 'foo' } as OutputToken, filters)
const output = new Output({ content: 'foo' } as OutputToken, filters, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('[object Object]')
})
it('should skip function property', async function () {
const scope = new Context({ obj: { foo: 'foo', bar: (x: any) => x } })
const output = new Output({ content: 'obj' } as OutputToken, filters)
const output = new Output({ content: 'obj' } as OutputToken, filters, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('[object Object]')
})
it('should respect to .toString()', async () => {
const scope = new Context({ obj: { toString: () => 'FOO' } })
const output = new Output({ content: 'obj' } as OutputToken, filters)
const output = new Output({ content: 'obj' } as OutputToken, filters, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('FOO')
})
it('should respect to .toString()', async () => {
const scope = new Context({ obj: { toString: () => 'FOO' } })
const output = new Output({ content: 'obj' } as OutputToken, filters)
const output = new Output({ content: 'obj' } as OutputToken, filters, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('FOO')
})
+7 -5
View File
@@ -12,15 +12,17 @@ chai.use(sinonChai)
const expect = chai.expect
describe('Value', function () {
const liquid = {} as any
describe('#constructor()', function () {
const filterMap = new FilterMap(false)
const filterMap = new FilterMap(false, liquid)
it('should parse "foo', function () {
const tpl = new Value('foo', filterMap)
const tpl = new Value('foo', filterMap, liquid)
expect(tpl.initial!.getText()).to.equal('foo')
expect(tpl.filters).to.deep.equal([])
})
it('should parse filters in value content', function () {
const f = new Value('o | foo: a: "a"', filterMap)
const f = new Value('o | foo: a: "a"', filterMap, liquid)
expect(f.filters[0].name).to.equal('foo')
expect(f.filters[0].args).to.have.lengthOf(1)
const [k, v] = f.filters[0].args[0] as any
@@ -34,10 +36,10 @@ describe('Value', function () {
it('should call chained filters correctly', async function () {
const date = sinon.stub().returns('y')
const time = sinon.spy()
const filterMap = new FilterMap(false)
const filterMap = new FilterMap(false, liquid)
filterMap.set('date', date)
filterMap.set('time', time)
const tpl = new Value('foo.bar | date: "b" | time:2', filterMap)
const tpl = new Value('foo.bar | date: "b" | time:2', filterMap, liquid)
const scope = new Context({
foo: { bar: 'bar' }
})