feat: support filters in if/unless/case, see #287

This commit is contained in:
harttle
2021-02-12 22:22:41 +08:00
parent 4e82da689e
commit 2f059f6a25
18 changed files with 164 additions and 176 deletions
+10 -18
View File
@@ -3,7 +3,6 @@ import { toThenable } from '../../../src/util/async'
import { Context } from '../../../src/context/context'
import { Output } from '../../../src/template/output'
import { OutputToken } from '../../../src/tokens/output-token'
import { FilterMap } from '../../../src/template/filter/filter-map'
import { defaultOptions } from '../../../src/liquid-options'
import { createTrie } from '../../../src/util/operator-trie'
import { defaultOperators } from '../../../src/types'
@@ -15,35 +14,31 @@ describe('Output', function () {
const liquid = {
options: { operatorsTrie: createTrie(defaultOperators) }
} as any
let filters: FilterMap
beforeEach(function () {
filters = new FilterMap(false, liquid)
emitter.html = ''
})
beforeEach(() => { emitter.html = '' })
it('should stringify objects', async function () {
const scope = new Context({
foo: { obj: { arr: ['a', 2] } }
})
const output = new Output({ content: 'foo' } as OutputToken, filters, liquid)
const output = new Output({ content: 'foo' } as OutputToken, 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, liquid)
const output = new Output({ content: 'obj' } as OutputToken, 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, liquid)
const output = new Output({ content: 'obj' } as OutputToken, 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, liquid)
const output = new Output({ content: 'obj' } as OutputToken, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('FOO')
})
@@ -60,16 +55,13 @@ describe('Output', function () {
keepOutputType: true
}
beforeEach(function () {
filters = new FilterMap(false, liquid)
emitter.html = ''
})
beforeEach(() => { emitter.html = '' })
it('should respect output variable number type', async () => {
const scope = new Context({
foo: 42
}, { ...defaultOptions, keepOutputType: true })
const output = new Output({ content: 'foo' } as OutputToken, filters, liquid)
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal(42)
})
@@ -77,7 +69,7 @@ describe('Output', function () {
const scope = new Context({
foo: true
}, { ...defaultOptions, keepOutputType: true })
const output = new Output({ content: 'foo' } as OutputToken, filters, liquid)
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal(true)
})
@@ -85,7 +77,7 @@ describe('Output', function () {
const scope = new Context({
foo: 'test'
}, { ...defaultOptions, keepOutputType: true })
const output = new Output({ content: 'foo' } as OutputToken, filters, liquid)
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.equal('test')
})
@@ -93,7 +85,7 @@ describe('Output', function () {
const scope = new Context({
foo: { a: { b: 42 } }
}, { ...defaultOptions, keepOutputType: true })
const output = new Output({ content: 'foo' } as OutputToken, filters, liquid)
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toThenable(output.render(scope, emitter))
return expect(emitter.html).to.deep.equal({ a: { b: 42 } })
})
+7 -18
View File
@@ -1,32 +1,22 @@
import * as chai from 'chai'
import { Liquid } from '../../../src/liquid'
import { QuotedToken } from '../../../src/tokens/quoted-token'
import { toThenable } from '../../../src/util/async'
import { FilterMap } from '../../../src/template/filter/filter-map'
import * as sinonChai from 'sinon-chai'
import * as sinon from 'sinon'
import { Context } from '../../../src/context/context'
import { Value } from '../../../src/template/value'
import { createTrie } from '../../../src/util/operator-trie'
import { defaultOperators } from '../../../src/types'
chai.use(sinonChai)
const expect = chai.expect
describe('Value', function () {
const liquid = {
options: { operatorsTrie: createTrie(defaultOperators) }
} as any
const liquid = new Liquid()
describe('#constructor()', function () {
const filterMap = new FilterMap(false, liquid)
it('should parse "foo', function () {
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, liquid)
const f = new Value('o | foo: a: "a"', 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
@@ -40,14 +30,13 @@ 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, liquid)
filterMap.set('date', date)
filterMap.set('time', time)
const tpl = new Value('foo.bar | date: "b" | time:2', filterMap, liquid)
liquid.registerFilter('date', date)
liquid.registerFilter('time', time)
const tpl = new Value('foo.bar | date: "b" | time:2', liquid)
const scope = new Context({
foo: { bar: 'bar' }
})
await toThenable(tpl.value(scope))
await toThenable(tpl.value(scope, false))
expect(date).to.have.been.calledWith('bar', 'b')
expect(time).to.have.been.calledWith('y', 2)
})