mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
feat: with & for in render tag, closes #195
This commit is contained in:
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
import { expect } from 'chai'
|
||||
import { LRU } from '../../../src/cache/lru'
|
||||
|
||||
describe('LRU', () => {
|
||||
it('should perform read()/write()', () => {
|
||||
const lru = new LRU(2)
|
||||
expect(lru.limit).to.equal(2)
|
||||
|
||||
lru.write('foo', 'FOO')
|
||||
lru.write('bar', 'BAR')
|
||||
expect(lru.read('foo')).to.equal('FOO')
|
||||
expect(lru.read('bar')).to.equal('BAR')
|
||||
})
|
||||
it('should perform clear()', () => {
|
||||
const lru = new LRU(2)
|
||||
lru.write('foo', 'FOO')
|
||||
lru.write('bar', 'BAR')
|
||||
expect(lru.size).to.equal(2)
|
||||
lru.clear()
|
||||
expect(lru.size).to.equal(0)
|
||||
expect(lru.read('foo')).to.be.undefined
|
||||
})
|
||||
it('should remove lrc item when full(2)', () => {
|
||||
const lru = new LRU(2)
|
||||
expect(lru.size).to.equal(0)
|
||||
lru.write('foo', 'FOO')
|
||||
expect(lru.size).to.equal(1)
|
||||
lru.write('bar', 'BAR')
|
||||
expect(lru.size).to.equal(2)
|
||||
lru.write('coo', 'COO')
|
||||
expect(lru.size).to.equal(2)
|
||||
expect(lru.read('foo')).to.be.undefined
|
||||
expect(lru.read('bar')).to.equal('BAR')
|
||||
expect(lru.read('coo')).to.equal('COO')
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import fs from '../../../src/fs/browser'
|
||||
import * as fs from '../../../src/fs/browser'
|
||||
import * as sinon from 'sinon'
|
||||
import { expect, use } from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import fs from '../../../src/fs/node'
|
||||
import * as fs from '../../../src/fs/node'
|
||||
import * as path from 'path'
|
||||
import { expect, use } from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import { tokenize } from '../../../src/parser/expression-tokenizer'
|
||||
import { expect } from 'chai'
|
||||
|
||||
describe('expression tokenizer', () => {
|
||||
describe('spaces', () => {
|
||||
it('should tokenize a + b', () => {
|
||||
expect([...tokenize('a + b')]).to.deep.equal(['a', '+', 'b'])
|
||||
})
|
||||
it('should tokenize a==1', () => {
|
||||
expect([...tokenize('a==1')]).to.deep.equal(['a', '==', '1'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('range', () => {
|
||||
it('should tokenize (1..3) contains 3', () => {
|
||||
expect([...tokenize('(1..3)')]).to.deep.equal(['(1..3)'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('bracket', () => {
|
||||
it('should tokenize a[b] = c', () => {
|
||||
expect([...tokenize('a[b] = c')]).to.deep.equal(['a[b]', '=', 'c'])
|
||||
})
|
||||
it('should tokenize c[a["b"]] < c', () => {
|
||||
expect([...tokenize('c[a["b"]] < c')]).to.deep.equal(['c[a["b"]]', '<', 'c'])
|
||||
})
|
||||
it('should tokenize "][" == var', () => {
|
||||
expect([...tokenize('"][" == var')]).to.deep.equal(['"]["', '==', 'var'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('quotes', () => {
|
||||
it('should tokenize " " == var', () => {
|
||||
expect([...tokenize('" " == var')]).to.deep.equal(['" "', '==', 'var'])
|
||||
})
|
||||
it('should tokenize "\\\'" == var', () => {
|
||||
expect([...tokenize('"\\\'" == var')]).to.deep.equal(['"\\\'"', '==', 'var'])
|
||||
})
|
||||
it('should tokenize "\\"" == var', () => {
|
||||
expect([...tokenize('"\\"" == var')]).to.deep.equal(['"\\""', '==', 'var'])
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,12 +0,0 @@
|
||||
import * as chai from 'chai'
|
||||
import { isRange } from '../../../src/parser/lexical'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
describe('lexical', function () {
|
||||
it('should test range literal', function () {
|
||||
expect(isRange('(12..32)')).to.equal(true)
|
||||
expect(isRange('(12..foo)')).to.equal(true)
|
||||
expect(isRange('(foo.bar..foo)')).to.equal(true)
|
||||
})
|
||||
})
|
||||
+197
-74
@@ -4,84 +4,207 @@ import { TagToken } from '../../../src/parser/tag-token'
|
||||
import { OutputToken } from '../../../src/parser/output-token'
|
||||
import { HTMLToken } from '../../../src/parser/html-token'
|
||||
|
||||
describe('tokenizer', function () {
|
||||
const tokenizer = new Tokenizer()
|
||||
describe('#tokenize()', function () {
|
||||
it('should handle plain HTML', function () {
|
||||
const html = '<html><body><p>Lorem Ipsum</p></body></html>'
|
||||
const tokens = tokenizer.tokenize(html)
|
||||
describe('Tokenize', function () {
|
||||
it('should read quoted', () => {
|
||||
expect(new Tokenizer('"foo" ff').readQuoted()).to.equal('"foo"')
|
||||
expect(new Tokenizer(' "foo"ff').readQuoted()).to.equal('"foo"')
|
||||
})
|
||||
it('should read property access', () => {
|
||||
expect(new Tokenizer('a[ b][ "c d" ]').readPropertyAccess()).to.equal('a[b]["c d"]')
|
||||
expect(new Tokenizer('a.b[c[d.e]]').readPropertyAccess()).to.equal('a.b[c[d.e]]')
|
||||
})
|
||||
it('should read value', () => {
|
||||
expect(new Tokenizer('2.33.2').readValue()).to.equal('2.33.2')
|
||||
expect(new Tokenizer('"foo"a').readValue()).to.equal('"foo"')
|
||||
expect(new Tokenizer('a[b]["c d"]').readValue()).to.equal('a[b]["c d"]')
|
||||
})
|
||||
it('should read hash', () => {
|
||||
expect(new Tokenizer('foo: 3').readHash()).to.deep.equal(['foo', '3'])
|
||||
expect(new Tokenizer(', foo: a[ "bar"]').readHash()).to.deep.equal(['foo', 'a["bar"]'])
|
||||
})
|
||||
it('should read hashs', () => {
|
||||
expect(new Tokenizer(', limit: 3 reverse offset:off').readHashes())
|
||||
.to.deep.equal([['limit', '3'], ['reverse', ''], ['offset', 'off']])
|
||||
expect(new Tokenizer('cols: 2, rows: data["rows"]').readHashes())
|
||||
.to.deep.equal([['cols', '2'], ['rows', 'data["rows"]']])
|
||||
})
|
||||
it('should read HTML token', function () {
|
||||
const html = '<html><body><p>Lorem Ipsum</p></body></html>'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
const tokens = tokenizer.readTokens()
|
||||
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0].value).to.equal(html)
|
||||
expect(tokens[0]).instanceOf(HTMLToken)
|
||||
})
|
||||
it('should handle tag syntax', function () {
|
||||
const html = '<p>{% for p in a[1]%}</p>'
|
||||
const tokens = tokenizer.tokenize(html)
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0].content).to.equal(html)
|
||||
expect(tokens[0]).instanceOf(HTMLToken)
|
||||
})
|
||||
it('should read tag token', function () {
|
||||
const html = '<p>{% for p in a[1]%}</p>'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
const tokens = tokenizer.readTokens()
|
||||
|
||||
expect(tokens.length).to.equal(3)
|
||||
expect(tokens[1]).instanceOf(TagToken)
|
||||
expect(tokens[1].value).to.equal('for p in a[1]')
|
||||
})
|
||||
it('should handle value syntax', function () {
|
||||
const html = '<p>{{foo | date: "%Y-%m-%d"}}</p>'
|
||||
const tokens = tokenizer.tokenize(html)
|
||||
expect(tokens.length).to.equal(3)
|
||||
expect(tokens[1]).instanceOf(TagToken)
|
||||
expect(tokens[1].content).to.equal('for p in a[1]')
|
||||
})
|
||||
it('should read value token', function () {
|
||||
const html = '<p>{{foo | date: "%Y-%m-%d"}}</p>'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
const tokens = tokenizer.readTokens()
|
||||
|
||||
expect(tokens.length).to.equal(3)
|
||||
expect(tokens[1]).instanceOf(OutputToken)
|
||||
expect(tokens[1].value).to.equal('foo | date: "%Y-%m-%d"')
|
||||
})
|
||||
it('should handle consecutive value and tags', function () {
|
||||
const html = '{{foo}}{{bar}}{%foo%}{%bar%}'
|
||||
const tokens = tokenizer.tokenize(html)
|
||||
expect(tokens.length).to.equal(3)
|
||||
expect(tokens[1]).instanceOf(OutputToken)
|
||||
expect(tokens[1].content).to.equal('foo | date: "%Y-%m-%d"')
|
||||
})
|
||||
it('should handle consecutive value and tags', function () {
|
||||
const html = '{{foo}}{{bar}}{%foo%}{%bar%}'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
const tokens = tokenizer.readTokens()
|
||||
|
||||
expect(tokens.length).to.equal(4)
|
||||
expect(tokens[0]).instanceOf(OutputToken)
|
||||
expect(tokens[2]).instanceOf(TagToken)
|
||||
expect(tokens.length).to.equal(4)
|
||||
expect(tokens[0]).instanceOf(OutputToken)
|
||||
expect(tokens[2]).instanceOf(TagToken)
|
||||
|
||||
expect(tokens[1].value).to.equal('bar')
|
||||
expect(tokens[2].value).to.equal('foo')
|
||||
})
|
||||
it('should keep white spaces and newlines', function () {
|
||||
const html = '{%foo%}\n{%bar %} \n {%alice%}'
|
||||
const tokens = tokenizer.tokenize(html)
|
||||
expect(tokens.length).to.equal(5)
|
||||
expect(tokens[1]).instanceOf(HTMLToken)
|
||||
expect(tokens[1].raw).to.equal('\n')
|
||||
expect(tokens[3]).instanceOf(HTMLToken)
|
||||
expect(tokens[3].raw).to.equal(' \n ')
|
||||
})
|
||||
it('should handle multiple lines tag', function () {
|
||||
const html = '{%foo\na:a\nb:1.23\n%}'
|
||||
const tokens = tokenizer.tokenize(html)
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0]).instanceOf(TagToken)
|
||||
expect((tokens[0] as TagToken).args).to.equal('a:a\nb:1.23')
|
||||
expect(tokens[0].raw).to.equal('{%foo\na:a\nb:1.23\n%}')
|
||||
})
|
||||
it('should handle multiple lines value', function () {
|
||||
const html = '{{foo\n|date:\n"%Y-%m-%d"\n}}'
|
||||
const tokens = tokenizer.tokenize(html)
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0]).instanceOf(OutputToken)
|
||||
expect(tokens[0].raw).to.equal('{{foo\n|date:\n"%Y-%m-%d"\n}}')
|
||||
})
|
||||
it('should handle complex object property access', function () {
|
||||
const html = '{{ obj["my:property with anything"] }}'
|
||||
const tokens = tokenizer.tokenize(html)
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0]).instanceOf(OutputToken)
|
||||
expect(tokens[0].value).to.equal('obj["my:property with anything"]')
|
||||
})
|
||||
it('should throw if tag not closed', function () {
|
||||
expect(() => {
|
||||
tokenizer.tokenize('{% assign foo = bar {{foo}}')
|
||||
}).to.throw(/tag "{% assign foo..." not closed/)
|
||||
})
|
||||
it('should throw if output not closed', function () {
|
||||
expect(() => {
|
||||
tokenizer.tokenize('{{name}')
|
||||
}).to.throw(/output "{{name}" not closed/)
|
||||
})
|
||||
expect(tokens[1].content).to.equal('bar')
|
||||
expect(tokens[2].content).to.equal('foo')
|
||||
})
|
||||
it('should keep white spaces and newlines', function () {
|
||||
const html = '{%foo%}\n{%bar %} \n {%alice%}'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
const tokens = tokenizer.readTokens()
|
||||
expect(tokens.length).to.equal(5)
|
||||
expect(tokens[1]).instanceOf(HTMLToken)
|
||||
expect(tokens[1].raw).to.equal('\n')
|
||||
expect(tokens[3]).instanceOf(HTMLToken)
|
||||
expect(tokens[3].raw).to.equal(' \n ')
|
||||
})
|
||||
it('should handle multiple lines tag', function () {
|
||||
const html = '{%foo\na:a\nb:1.23\n%}'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
const tokens = tokenizer.readTokens()
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0]).instanceOf(TagToken)
|
||||
expect((tokens[0] as TagToken).args).to.equal('a:a\nb:1.23')
|
||||
expect(tokens[0].raw).to.equal('{%foo\na:a\nb:1.23\n%}')
|
||||
})
|
||||
it('should handle multiple lines value', function () {
|
||||
const html = '{{foo\n|date:\n"%Y-%m-%d"\n}}'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
const tokens = tokenizer.readTokens()
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0]).instanceOf(OutputToken)
|
||||
expect(tokens[0].raw).to.equal('{{foo\n|date:\n"%Y-%m-%d"\n}}')
|
||||
})
|
||||
it('should handle complex object property access', function () {
|
||||
const html = '{{ obj["my:property with anything"] }}'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
const tokens = tokenizer.readTokens()
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0]).instanceOf(OutputToken)
|
||||
expect(tokens[0].content).to.equal('obj["my:property with anything"]')
|
||||
})
|
||||
it('should throw if tag not closed', function () {
|
||||
const html = '{% assign foo = bar {{foo}}'
|
||||
const tokenizer = new Tokenizer(html)
|
||||
expect(() => tokenizer.readTokens()).to.throw(/tag "{% assign foo..." not closed/)
|
||||
})
|
||||
it('should throw if output not closed', function () {
|
||||
const tokenizer = new Tokenizer('{{name}')
|
||||
expect(() => tokenizer.readTokens()).to.throw(/output "{{name}" not closed/)
|
||||
})
|
||||
it('should read a simple filter', function () {
|
||||
const tokenizer = new Tokenizer('| plus')
|
||||
const token = tokenizer.readFilterToken()
|
||||
expect(token).to.have.property('name', 'plus')
|
||||
expect(token).to.have.property('args').to.deep.equal([])
|
||||
})
|
||||
it('should read a filter with argument', function () {
|
||||
const tokenizer = new Tokenizer(' | plus: 1')
|
||||
const token = tokenizer.readFilterToken()
|
||||
expect(token).to.have.property('name', 'plus')
|
||||
expect(token).to.have.property('args').to.deep.equal(['1'])
|
||||
})
|
||||
it('should read a filter with colon but no argument', function () {
|
||||
const tokenizer = new Tokenizer('| plus:')
|
||||
const token = tokenizer.readFilterToken()
|
||||
expect(token).to.have.property('name', 'plus')
|
||||
expect(token).to.have.property('args').to.deep.equal([])
|
||||
})
|
||||
it('should read a filter with k/v argument', function () {
|
||||
const tokenizer = new Tokenizer(' | plus: a:1')
|
||||
const token = tokenizer.readFilterToken()
|
||||
expect(token).to.have.property('name', 'plus')
|
||||
expect(token).to.have.property('args').to.deep.equal([['a', '1']])
|
||||
})
|
||||
it('should read a filter with "arr[0]" argument', function () {
|
||||
const tokenizer = new Tokenizer('| plus: arr[0]')
|
||||
const token = tokenizer.readFilterToken()
|
||||
expect(token).to.have.property('name', 'plus')
|
||||
expect(token).to.have.property('args').to.deep.equal(['arr[0]'])
|
||||
})
|
||||
it('should read a filter with obj.foo argument', function () {
|
||||
const tokenizer = new Tokenizer('| plus: obj.foo')
|
||||
const token = tokenizer.readFilterToken()
|
||||
expect(token).to.have.property('name', 'plus')
|
||||
expect(token).to.have.property('args').to.deep.equal(['obj.foo'])
|
||||
})
|
||||
it('should read a filter with obj["foo"] argument', function () {
|
||||
const tokenizer = new Tokenizer('| plus: obj["good luck"]')
|
||||
const token = tokenizer.readFilterToken()
|
||||
expect(token).to.have.property('name', 'plus')
|
||||
expect(token).to.have.property('args').to.deep.equal(['obj["good luck"]'])
|
||||
})
|
||||
it('should read simple filters', function () {
|
||||
const tokenizer = new Tokenizer('| plus: 3 | capitalize')
|
||||
const tokens = tokenizer.readFilterTokens()
|
||||
|
||||
expect(tokens).to.have.lengthOf(2)
|
||||
expect(tokens[0]).to.have.property('name', 'plus')
|
||||
expect(tokens[0]).to.have.property('args').to.deep.equal(['3'])
|
||||
expect(tokens[1]).to.have.property('name', 'capitalize')
|
||||
expect(tokens[1]).to.have.property('args').to.deep.equal([])
|
||||
})
|
||||
it('should read filters', function () {
|
||||
const tokenizer = new Tokenizer('| plus: 3 | capitalize | append: foo[a.b["c d"]]')
|
||||
const tokens = tokenizer.readFilterTokens()
|
||||
|
||||
expect(tokens).to.have.lengthOf(3)
|
||||
expect(tokens[0]).to.have.property('name', 'plus')
|
||||
expect(tokens[0]).to.have.property('args').to.deep.equal(['3'])
|
||||
expect(tokens[1]).to.have.property('name', 'capitalize')
|
||||
expect(tokens[1]).to.have.property('args').to.deep.equal([])
|
||||
expect(tokens[2]).to.have.property('name', 'append')
|
||||
expect(tokens[2]).to.have.property('args').to.deep.equal(['foo[a.b["c d"]]'])
|
||||
})
|
||||
it('should read expression `a==b`', () => {
|
||||
const exp = new Tokenizer('a==b').readExpression()
|
||||
expect([...exp]).to.deep.equal(['a', '==', 'b'])
|
||||
})
|
||||
it('should read expression `^`', () => {
|
||||
const exp = new Tokenizer('^').readExpression()
|
||||
expect([...exp]).to.deep.equal([])
|
||||
})
|
||||
it('should read expression `a == b`', () => {
|
||||
const exp = new Tokenizer('a == b').readExpression()
|
||||
expect([...exp]).to.deep.equal(['a', '==', 'b'])
|
||||
})
|
||||
it('should read expression `(1..3) contains 3`', () => {
|
||||
const exp = new Tokenizer('(1..3) contains 3').readExpression()
|
||||
expect([...exp]).to.deep.equal(['(1..3)', 'contains', '3'])
|
||||
})
|
||||
it('should read expression `a[b] = c`', () => {
|
||||
const exp = new Tokenizer('a[b] = c').readExpression()
|
||||
expect([...exp]).to.deep.equal(['a[b]', '=', 'c'])
|
||||
})
|
||||
it('should read expression `c[a["b"]] >= c`', () => {
|
||||
const exp = new Tokenizer('c[a["b"]] >= c').readExpression()
|
||||
expect([...exp]).to.deep.equal(['c[a["b"]]', '>=', 'c'])
|
||||
})
|
||||
it('should read expression `"][" == var`', () => {
|
||||
const exp = new Tokenizer('"][" == var').readExpression()
|
||||
expect([...exp]).to.deep.equal(['"]["', '==', 'var'])
|
||||
})
|
||||
it('should read expression `"\\\'" == "\\""`', () => {
|
||||
const exp = new Tokenizer('"\\\'" == "\\""').readExpression()
|
||||
expect([...exp]).to.deep.equal(['"\\\'"', '==', '"\\""'])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,7 +14,7 @@ describe('render', function () {
|
||||
describe('.renderTemplates()', function () {
|
||||
it('should render html', async function () {
|
||||
const scope = new Context()
|
||||
const token = { type: 'html', value: '<p>' } as Token
|
||||
const token = { type: 'html', content: '<p>' } as Token
|
||||
const html = await toThenable(render.renderTemplates([new HTML(token)], scope))
|
||||
return expect(html).to.equal('<p>')
|
||||
})
|
||||
|
||||
@@ -6,12 +6,39 @@ import { Context } from '../../../src/context/context'
|
||||
const expect = chai.expect
|
||||
|
||||
describe('Hash', function () {
|
||||
it('should parse variable', async function () {
|
||||
const hash = await toThenable(Hash.create('num:foo', new Context({ foo: 3 })))
|
||||
it('should parse "reverse"', async function () {
|
||||
const hash = await toThenable(new Hash('reverse').render(new Context({ foo: 3 })))
|
||||
expect(hash).to.haveOwnProperty('reverse')
|
||||
expect(hash.reverse).to.be.undefined
|
||||
})
|
||||
it('should parse "num:foo"', async function () {
|
||||
const hash = await toThenable(new Hash('num:foo').render(new Context({ foo: 3 })))
|
||||
expect(hash.num).to.equal(3)
|
||||
})
|
||||
it('should parse literals', async function () {
|
||||
const hash = await toThenable(Hash.create('num:3', new Context()))
|
||||
it('should parse "num:3"', async function () {
|
||||
const hash = await toThenable(new Hash('num:3').render(new Context()))
|
||||
expect(hash.num).to.equal(3)
|
||||
})
|
||||
it('should parse "num: arr[0]"', async function () {
|
||||
const hash = await toThenable(new Hash('num:3').render(new Context({ arr: [3] })))
|
||||
expect(hash.num).to.equal(3)
|
||||
})
|
||||
it('should parse "num: 2.3"', async function () {
|
||||
const hash = await toThenable(new Hash('num:2.3').render(new Context()))
|
||||
expect(hash.num).to.equal(2.3)
|
||||
})
|
||||
it('should parse "num:bar.coo"', async function () {
|
||||
const hash = await toThenable(new Hash('num:bar.coo').render(new Context({ bar: { coo: 3 } })))
|
||||
expect(hash.num).to.equal(3)
|
||||
})
|
||||
it('should parse "num1:2.3 reverse,num2:bar.coo\n num3: arr[0]"', async function () {
|
||||
const ctx = new Context({ bar: { coo: 3 }, arr: [4] })
|
||||
const hash = await toThenable(new Hash('num1:2.3 reverse,num2:bar.coo\n num3: arr[0]').render(ctx))
|
||||
expect(hash).to.deep.equal({
|
||||
num1: 2.3,
|
||||
reverse: undefined,
|
||||
num2: 3,
|
||||
num3: 4
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -19,25 +19,25 @@ describe('Output', function () {
|
||||
const scope = new Context({
|
||||
foo: { obj: { arr: ['a', 2] } }
|
||||
})
|
||||
const output = new Output({ value: 'foo' } as OutputToken, filters)
|
||||
const output = new Output({ content: 'foo' } as OutputToken, filters)
|
||||
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({ value: 'obj' } as OutputToken, filters)
|
||||
const output = new Output({ content: 'obj' } as OutputToken, filters)
|
||||
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({ value: 'obj' } as OutputToken, filters)
|
||||
const output = new Output({ content: 'obj' } as OutputToken, filters)
|
||||
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({ value: 'obj' } as OutputToken, filters)
|
||||
const output = new Output({ content: 'obj' } as OutputToken, filters)
|
||||
await toThenable(output.render(scope, emitter))
|
||||
return expect(emitter.html).to.equal('FOO')
|
||||
})
|
||||
|
||||
@@ -32,7 +32,8 @@ describe('Tag', function () {
|
||||
expect(function () {
|
||||
new Tag({ // eslint-disable-line
|
||||
type: 'tag',
|
||||
value: 'foo',
|
||||
content: 'foo',
|
||||
args: '',
|
||||
name: 'not-exist'
|
||||
} as TagToken, [], liquid)
|
||||
}).to.throw(/tag "not-exist" not found/)
|
||||
@@ -49,52 +50,11 @@ describe('Tag', function () {
|
||||
liquid.registerTag('foo', { render: spy })
|
||||
const token = {
|
||||
type: 'tag',
|
||||
value: 'foo',
|
||||
content: 'foo',
|
||||
args: '',
|
||||
name: 'foo'
|
||||
} as TagToken
|
||||
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
|
||||
expect(spy).to.have.been.called
|
||||
})
|
||||
|
||||
describe('hash', function () {
|
||||
let spy: sinon.SinonSpy, token: TagToken
|
||||
beforeEach(function () {
|
||||
spy = sinon.spy()
|
||||
liquid.registerTag('foo', { render: spy })
|
||||
token = {
|
||||
type: 'tag',
|
||||
value: 'foo aa:foo bb: arr[0] cc: 2.3\ndd:bar.coo',
|
||||
name: 'foo',
|
||||
args: 'aa:foo bb: arr[0] cc: 2.3\ndd:bar.coo'
|
||||
} as TagToken
|
||||
})
|
||||
it('should call tag.render with scope', async function () {
|
||||
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
|
||||
expect(spy).to.have.been.calledWithMatch(ctx)
|
||||
})
|
||||
it('should resolve identifier hash', async function () {
|
||||
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
|
||||
expect(spy).to.have.been.calledWithMatch({}, {
|
||||
aa: 'bar'
|
||||
})
|
||||
})
|
||||
it('should accept space between key/value', async function () {
|
||||
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
|
||||
expect(spy).to.have.been.calledWithMatch({}, {
|
||||
bb: 2
|
||||
})
|
||||
})
|
||||
it('should resolve number value hash', async function () {
|
||||
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
|
||||
expect(spy).to.have.been.calledWithMatch(ctx, {
|
||||
cc: 2.3
|
||||
})
|
||||
})
|
||||
it('should resolve property access hash', async function () {
|
||||
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
|
||||
expect(spy).to.have.been.calledWithMatch(ctx, {
|
||||
dd: 'uoo'
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -83,33 +83,6 @@ describe('Value', function () {
|
||||
})
|
||||
})
|
||||
|
||||
describe('#tokenize()', function () {
|
||||
it('should tokenize a simple value', function () {
|
||||
expect(Value.tokenize('foo')).to.eql(['foo'])
|
||||
})
|
||||
it('should tokenize a value with spaces', function () {
|
||||
expect(Value.tokenize(' foo \t')).to.eql(['foo'])
|
||||
})
|
||||
it('should tokenize a simple filter', function () {
|
||||
expect(Value.tokenize('foo | add')).to.eql(['foo', '|', 'add'])
|
||||
})
|
||||
it('should tokenize a filter with a single argument', function () {
|
||||
expect(Value.tokenize('foo | add: 1')).to.eql(['foo', '|', 'add', ':', '1'])
|
||||
})
|
||||
it('should tokenize array indexing', function () {
|
||||
expect(Value.tokenize('arr[0]')).to.eql(['arr[0]'])
|
||||
})
|
||||
it('should tokenize simple object access', function () {
|
||||
expect(Value.tokenize('obj["foo"]')).to.eql(['obj["foo"]'])
|
||||
})
|
||||
it('should tokenize simple dot syntax object access', function () {
|
||||
expect(Value.tokenize('obj.foo')).to.eql(['obj.foo'])
|
||||
})
|
||||
it('should tokenize complex object property access', function () {
|
||||
expect(Value.tokenize('obj["complex:string here"]')).to.eql(['obj["complex:string here"]'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('#value()', function () {
|
||||
it('should call chained filters correctly', async function () {
|
||||
const date = sinon.stub().returns('y')
|
||||
|
||||
Reference in New Issue
Block a user