mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
feat: implement liquid and echo tags, see #428
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { Liquid } from '../../../../src/liquid'
|
||||
import { expect, use } from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
use(chaiAsPromised)
|
||||
|
||||
describe('tags/echo', function () {
|
||||
const liquid = new Liquid()
|
||||
|
||||
it('should output literals', async function () {
|
||||
const src = '{% echo 1 %} {% echo "1" %} {% echo 1.1 %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('1 1 1.1')
|
||||
})
|
||||
|
||||
it('should output variables', async function () {
|
||||
const src = '{% echo people.users[0].name %}'
|
||||
const html = await liquid.parseAndRender(src, { people: { users: [ { name: 'Sally' } ] } })
|
||||
return expect(html).to.equal('Sally')
|
||||
})
|
||||
|
||||
it('should apply filters before output', async function () {
|
||||
const src = '{% echo user.name | upcase | prepend: "Hello, " | append: "!" %}'
|
||||
const html = await liquid.parseAndRender(src, { user: { name: 'Sally' } })
|
||||
return expect(html).to.equal('Hello, SALLY!')
|
||||
})
|
||||
|
||||
it('should handle empty tag', async function () {
|
||||
const src = '{% echo %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('')
|
||||
})
|
||||
|
||||
it('should handle extra whitespace', async function () {
|
||||
const src = `{% echo
|
||||
user.name
|
||||
| upcase | prepend:
|
||||
"Hello, " | append: "!"
|
||||
%}`
|
||||
const html = await liquid.parseAndRender(src, { user: { name: 'Sally' } })
|
||||
return expect(html).to.equal('Hello, SALLY!')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,76 @@
|
||||
import { Liquid } from '../../../../src/liquid'
|
||||
import { expect, use } from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
use(chaiAsPromised)
|
||||
|
||||
describe('tags/liquid', function () {
|
||||
const liquid = new Liquid()
|
||||
|
||||
it('should support shorthand syntax', async function () {
|
||||
const src = `
|
||||
{%- liquid
|
||||
for value in array
|
||||
echo value
|
||||
unless forloop.last
|
||||
echo '#'
|
||||
endunless
|
||||
endfor
|
||||
-%}
|
||||
`
|
||||
const html = await liquid.parseAndRender(src, { array: [1, 2, 3] })
|
||||
return expect(html).to.equal('1#2#3')
|
||||
})
|
||||
|
||||
it('should support shorthand syntax with assignments and filters', async function () {
|
||||
const src = `
|
||||
{%- liquid
|
||||
for value in array
|
||||
assign double_value = value | times: 2
|
||||
echo double_value | times: 2
|
||||
unless forloop.last
|
||||
echo '#'
|
||||
endunless
|
||||
endfor
|
||||
|
||||
echo '#'
|
||||
echo double_value
|
||||
-%}
|
||||
`
|
||||
const html = await liquid.parseAndRender(src, { array: [1, 2, 3] })
|
||||
return expect(html).to.equal('4#8#12#6')
|
||||
})
|
||||
|
||||
it('should handle empty tag', async function () {
|
||||
const src = '{% liquid %}'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('')
|
||||
})
|
||||
|
||||
it('should handle lines containing only whitespace', async function () {
|
||||
const src = `{% liquid
|
||||
echo 'hello '
|
||||
|
||||
|
||||
\t
|
||||
echo 'goodbye'
|
||||
%}`
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal('hello goodbye')
|
||||
})
|
||||
|
||||
it('should fail with carriage return terminated tags', async function () {
|
||||
const src = [
|
||||
'{%- liquid',
|
||||
' for value in array',
|
||||
' echo value',
|
||||
' unless forloop.last',
|
||||
' echo "#"',
|
||||
' endunless',
|
||||
'endfor',
|
||||
'-%}'
|
||||
].join('\r')
|
||||
return expect(liquid.parseAndRender(src))
|
||||
.to.be.rejectedWith(/not closed/)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user