fix: filters break when argument contains [()|, fixes #89

This commit is contained in:
harttle
2019-02-24 04:48:42 +08:00
parent 279458c670
commit e977669118
10 changed files with 2100 additions and 2092 deletions
+23 -30
View File
@@ -8,57 +8,50 @@ import Value from 'src/template/value'
chai.use(sinonChai)
const expect = chai.expect
const add = (l: number, r: number) => l + r
describe('Value', function () {
beforeEach(() => Filter.clear())
it('should throw when value string illegal', function () {
expect(function () {
new Value('/')
}).to.throw(/illegal value string/)
})
it('should parse value string', function () {
const tpl: any = new Value('foo')
it('should parse "foo', function () {
const tpl: any = new Value('foo', false)
expect(tpl.initial).to.equal('foo')
expect(tpl.filters).to.deep.equal([])
})
it('should parse value string with a simple filter', function () {
Filter.register('add', add)
const tpl: any = new Value('foo | add: 3, "foo"')
it('should parse "foo | add"', function () {
const tpl: any = new Value('foo | add', false)
expect(tpl.initial).to.equal('foo')
expect(tpl.filters.length).to.equal(1)
expect(tpl.filters[0].impl).to.equal(add)
expect(tpl.filters[0].args).to.eql([])
})
it('should parse value string with filters', function () {
const tpl: any = new Value('foo | add: "|" | add')
it('should parse "foo | add: 3, false"', function () {
const tpl: any = new Value('foo | add: 3, "foo"', false)
expect(tpl.initial).to.equal('foo')
expect(tpl.filters.length).to.equal(2)
expect(tpl.filters.length).to.equal(1)
expect(tpl.filters[0].args).to.eql(['3', '"foo"'])
})
it('should eval value', function () {
Filter.register('date', (l, r) => l + r)
Filter.register('time', (l, r) => l + 3 * r)
const tpl = new Value('foo.bar[0] | date: "b" | time:2')
const scope = new Scope({
foo: { bar: ['a'] }
})
expect(tpl.value(scope)).to.equal('ab6')
it('should parse "foo | add: "|", 3', function () {
const tpl: any = new Value('foo | add: "|", 3', false)
expect(tpl.initial).to.equal('foo')
expect(tpl.filters.length).to.equal(1)
expect(tpl.filters[0].args).to.eql(['"|"', '3'])
})
it('should reserve type', function () {
Filter.register('arr', () => [1])
const tpl = new Value('"x" | arr')
expect(tpl.value(new Scope())).to.deep.equal([1])
it('should parse "foo | add: "|", 3', function () {
const tpl: any = new Value('foo | add: "|", 3', false)
expect(tpl.initial).to.equal('foo')
expect(tpl.filters.length).to.equal(1)
expect(tpl.filters[0].args).to.eql(['"|"', '3'])
})
it('should eval filter with correct arguments', function () {
it('should call chained filters correctly', function () {
const date = sinon.stub().returns('y')
const time = sinon.spy()
Filter.register('date', date)
Filter.register('time', time)
const tpl = new Value('foo.bar | date: "b" | time:2')
const tpl = new Value('foo.bar | date: "b" | time:2', false)
const scope = new Scope({
foo: { bar: 'bar' }
})