mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
refactor: remove /dist from repo
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { expect } from 'chai'
|
||||
import Liquid from 'src/liquid'
|
||||
import { mock, restore } from 'test/stub/mockfs'
|
||||
|
||||
describe('LiquidOptions#cache', function () {
|
||||
let engine: Liquid
|
||||
beforeEach(function () {
|
||||
engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html'
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
})
|
||||
afterEach(restore)
|
||||
it('should be disabled by default', function () {
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(() => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(() => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('bar'))
|
||||
})
|
||||
it('should respect cache=true option', function () {
|
||||
engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(() => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(() => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,31 @@
|
||||
import { expect } from 'chai'
|
||||
import Liquid from 'src/liquid'
|
||||
|
||||
describe('LiquidOptions#*_delimiter_*', function () {
|
||||
it('should respect tag_delimiter_*', async function () {
|
||||
const engine = new Liquid({
|
||||
tag_delimiter_left: '<%=',
|
||||
tag_delimiter_right: '%>'
|
||||
})
|
||||
const html = await engine.parseAndRender('<%=if true%>foo<%=endif%> ')
|
||||
return expect(html).to.equal('foo ')
|
||||
})
|
||||
it('should respect output_delimiter_*', async function () {
|
||||
const engine = new Liquid({
|
||||
output_delimiter_left: '<<',
|
||||
output_delimiter_right: '>>'
|
||||
})
|
||||
const html = await engine.parseAndRender('<< "liquid" | capitalize >>')
|
||||
return expect(html).to.equal('Liquid')
|
||||
})
|
||||
it('should support trimming with tag_delimiter_* set', async function () {
|
||||
const engine = new Liquid({
|
||||
tag_delimiter_left: '<%=',
|
||||
tag_delimiter_right: '%>',
|
||||
trim_tag_left: true,
|
||||
trim_tag_right: true
|
||||
})
|
||||
const html = await engine.parseAndRender(' <%=if true%> \tfoo\t <%=endif%> ')
|
||||
return expect(html).to.equal('foo')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,15 @@
|
||||
import { normalize } from 'src/liquid-options'
|
||||
import { expect } from 'chai'
|
||||
|
||||
describe('LiquidOptions', function () {
|
||||
describe('#normalize ()', function () {
|
||||
it('should normalize string typed root array', function () {
|
||||
const options = normalize({ root: 'foo' })
|
||||
expect(options.root).to.eql(['foo'])
|
||||
})
|
||||
it('should normalize null typed root as empty array', function () {
|
||||
const options = normalize({ root: null } as any)
|
||||
expect(options.root).to.eql([])
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,72 @@
|
||||
import Liquid from 'src/liquid'
|
||||
import * as chai from 'chai'
|
||||
import { mock, restore } from 'test/stub/mockfs'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
describe('Liquid', function () {
|
||||
describe('#plugin()', function () {
|
||||
it('should call plugin on the instance', async function () {
|
||||
const engine = new Liquid()
|
||||
engine.plugin(function () {
|
||||
this.registerFilter('foo', x => `foo${x}foo`)
|
||||
})
|
||||
const html = await engine.parseAndRender('{{"bar"|foo}}')
|
||||
expect(html).to.equal('foobarfoo')
|
||||
})
|
||||
it('should call plugin with Liquid', async function () {
|
||||
const engine = new Liquid()
|
||||
engine.plugin(function (Liquid) {
|
||||
this.registerFilter('t', x => Liquid.isFalsy(x))
|
||||
})
|
||||
const html = await engine.parseAndRender('{{false|t}}')
|
||||
expect(html).to.equal('true')
|
||||
})
|
||||
})
|
||||
describe('#parseAndRender', function () {
|
||||
const engine = new Liquid()
|
||||
it('should parse and render variable output', async function () {
|
||||
const html = await engine.parseAndRender('{{"foo"}}')
|
||||
expect(html).to.equal('foo')
|
||||
})
|
||||
it('should parse and render complex output', async function () {
|
||||
const tpl = '{{ "Welcome|to]Liquid" | split: "|" | join: "("}}'
|
||||
const html = await engine.parseAndRender(tpl)
|
||||
expect(html).to.equal('Welcome(to]Liquid')
|
||||
})
|
||||
})
|
||||
describe('#express()', function () {
|
||||
const liquid = new Liquid({ root: '/root' })
|
||||
const render = liquid.express()
|
||||
before(function () {
|
||||
mock({
|
||||
'/root/foo': 'foo'
|
||||
})
|
||||
})
|
||||
after(restore)
|
||||
it('should render single template', function (done) {
|
||||
render.call({ root: '.' }, 'foo', null as any, (err: Error | null, result: string | undefined) => {
|
||||
if (err) return done(err)
|
||||
expect(result).to.equal('foo')
|
||||
done()
|
||||
})
|
||||
})
|
||||
it('should render single template with Array-typed root', function (done) {
|
||||
render.call({ root: ['.'] }, 'foo', null as any, (err: Error | null, result: string | undefined) => {
|
||||
if (err) return done(err)
|
||||
expect(result).to.equal('foo')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
describe('#renderFile', function () {
|
||||
it('should throw with lookup list when file not exist', function () {
|
||||
const engine = new Liquid({
|
||||
root: ['/boo', '/root/'],
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(engine.renderFile('/not/exist.html')).to
|
||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,33 @@
|
||||
import Liquid from 'src/liquid'
|
||||
import { expect } from 'chai'
|
||||
|
||||
describe('LiquidOptions#strict_*', function () {
|
||||
let engine: Liquid
|
||||
const ctx = {}
|
||||
beforeEach(function () {
|
||||
engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html'
|
||||
})
|
||||
})
|
||||
it('should not throw when strict_variables false (default)', async function () {
|
||||
const html = await engine.parseAndRender('before{{notdefined}}after', ctx)
|
||||
return expect(html).to.equal('beforeafter')
|
||||
})
|
||||
it('should throw when strict_variables true', function () {
|
||||
const tpl = engine.parse('before{{notdefined}}after')
|
||||
const opts = {
|
||||
strict_variables: true
|
||||
}
|
||||
return expect(engine.render(tpl, ctx, opts)).to
|
||||
.be.rejectedWith(/undefined variable: notdefined/)
|
||||
})
|
||||
it('should pass strict_variables to render by parseAndRender', function () {
|
||||
const html = 'before{{notdefined}}after'
|
||||
const opts = {
|
||||
strict_variables: true
|
||||
}
|
||||
return expect(engine.parseAndRender(html, ctx, opts)).to
|
||||
.be.rejectedWith(/undefined variable: notdefined/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,84 @@
|
||||
import { expect } from 'chai'
|
||||
import Liquid from 'src/liquid'
|
||||
|
||||
describe('LiquidOptions#trimming', function () {
|
||||
const ctx = { name: 'harttle' }
|
||||
|
||||
describe('tag trimming', function () {
|
||||
it('should respect trim_tag_left', async function () {
|
||||
const engine = new Liquid({ trim_tag_left: true })
|
||||
const html = await engine.parseAndRender(' \n \t{%if true%}foo{%endif%} ')
|
||||
return expect(html).to.equal('foo ')
|
||||
})
|
||||
it('should respect trim_tag_right', async function () {
|
||||
const engine = new Liquid({ trim_tag_right: true })
|
||||
const html = await engine.parseAndRender('\t{%if true%}foo{%endif%} \n')
|
||||
return expect(html).to.equal('\tfoo')
|
||||
})
|
||||
it('should not trim value', async function () {
|
||||
const engine = new Liquid({ trim_tag_left: true, trim_tag_right: true })
|
||||
const html = await engine.parseAndRender('{%if true%}a {{name}} b{%endif%}', ctx)
|
||||
return expect(html).to.equal('a harttle b')
|
||||
})
|
||||
})
|
||||
describe('value trimming', function () {
|
||||
it('should respect trim_output_left', async function () {
|
||||
const engine = new Liquid({ trim_output_left: true })
|
||||
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
|
||||
return expect(html).to.equal('harttle ')
|
||||
})
|
||||
it('should respect trim_output_right', async function () {
|
||||
const engine = new Liquid({ trim_output_right: true })
|
||||
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
|
||||
return expect(html).to.equal(' \n \tharttle')
|
||||
})
|
||||
it('should respect not trim tag', async function () {
|
||||
const engine = new Liquid({ trim_output_left: true, trim_output_right: true })
|
||||
const html = await engine.parseAndRender('\t{% if true %} aha {%endif%}\t')
|
||||
return expect(html).to.equal('\t aha \t')
|
||||
})
|
||||
})
|
||||
describe('greedy', function () {
|
||||
const src = '\n {%-if true-%}\n a \n{{-name-}}{%-endif-%}\n '
|
||||
it('should enable greedy by default', async function () {
|
||||
const engine = new Liquid()
|
||||
const html = await engine.parseAndRender(src, ctx)
|
||||
return expect(html).to.equal('aharttle')
|
||||
})
|
||||
it('should respect to greedy:false by default', async function () {
|
||||
const engine = new Liquid({ greedy: false })
|
||||
const html = await engine.parseAndRender(src, ctx)
|
||||
return expect(html).to.equal('\n a \nharttle ')
|
||||
})
|
||||
})
|
||||
describe('markup', function () {
|
||||
it('should support trim using markup', async function () {
|
||||
const engine = new Liquid()
|
||||
const src = [
|
||||
'{%- assign username = "John G. Chalmers-Smith" -%}',
|
||||
'{%- if username and username.length > 10 -%}',
|
||||
' Wow, {{ username }}, you have a long name!',
|
||||
'{%- else -%}',
|
||||
' Hello there!',
|
||||
'{%- endif -%}'
|
||||
].join('\n')
|
||||
const dst = 'Wow, John G. Chalmers-Smith, you have a long name!'
|
||||
const html = await engine.parseAndRender(src)
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
it('should not trim when not specified', async function () {
|
||||
const engine = new Liquid()
|
||||
const src = [
|
||||
'{% assign username = "John G. Chalmers-Smith" %}',
|
||||
'{% if username and username.length > 10 %}',
|
||||
' Wow, {{ username }}, you have a long name!',
|
||||
'{% else %}',
|
||||
' Hello there!',
|
||||
'{% endif %}'
|
||||
].join('\n')
|
||||
const dst = '\n\n Wow, John G. Chalmers-Smith, you have a long name!\n'
|
||||
const html = await engine.parseAndRender(src)
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user