feat: exported Drop interface for #107

Deprecate snake_cased options and APIs, sed #109
This commit is contained in:
harttle
2019-02-28 00:05:08 +08:00
parent b69c3a3203
commit 7bee9fc92d
26 changed files with 227 additions and 179 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ describe('filters/array', function () {
return test(src, 'tiger')
})
it('should support map', function () {
return test('{{posts | map: "category"}}', '["foo","bar"]')
return test('{{posts | map: "category"}}', 'foo,bar')
})
it('should support reverse', function () {
return test('{{ "Ground control to Major Tom." | split: "" | reverse | join: "" }}',
+1 -1
View File
@@ -15,6 +15,6 @@ describe('filters/date', function () {
return test('{{ "foo" | date: "%Y"}}', 'foo')
})
it('should render object as string if not valid', function () {
return test('{{ obj | date: "%Y"}}', '{"foo":"bar"}')
return test('{{ obj | date: "%Y"}}', '[object Object]')
})
})
+1 -1
View File
@@ -31,7 +31,7 @@ describe('tags/assign', function () {
it('should assign as array', async function () {
const src = '{% assign foo=(1..3) %}{{foo}}'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('[1,2,3]')
return expect(html).to.equal('1,2,3')
})
it('should assign as filter result', async function () {
const src = '{% assign foo="a b" | capitalize | split: " " | first %}{{foo}}'
+35
View File
@@ -0,0 +1,35 @@
import { expect } from 'chai'
import Liquid from '../../../src/liquid'
describe('drop/drop', function () {
let liquid: Liquid
before(() => (liquid = new Liquid()))
class CustomDrop extends Liquid.Types.Drop {
name: string = 'NAME'
getName() {
return 'GETNAME'
}
}
class CustomDropWithMethodMissing extends CustomDrop {
liquidMethodMissing(key: string) {
return key.toUpperCase()
}
}
it('should call corresponding method', async function () {
const html = await liquid.parseAndRender(`{{obj.getName}}`, {obj: new CustomDrop()})
expect(html).to.equal('GETNAME')
})
it('should read corresponding property', async function () {
const html = await liquid.parseAndRender(`{{obj.name}}`, {obj: new CustomDrop()})
expect(html).to.equal('NAME')
})
it('should output empty string if not exist', async function () {
const html = await liquid.parseAndRender(`{{obj.foo}}`, {obj: new CustomDrop()})
expect(html).to.equal('')
})
it('should respect liquidMethodMissing', async function () {
const html = await liquid.parseAndRender(`{{obj.foo}}`, {obj: new CustomDropWithMethodMissing()})
expect(html).to.equal('FOO')
})
})
+8 -8
View File
@@ -4,26 +4,26 @@ 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: '%>'
tagDelimiterLeft: '<%=',
tagDelimiterRight: '%>'
})
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: '>>'
outputDelimiterLeft: '<<',
outputDelimiterRight: '>>'
})
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
tagDelimiterLeft: '<%=',
tagDelimiterRight: '%>',
trimTagLeft: true,
trimTagRight: true
})
const html = await engine.parseAndRender(' <%=if true%> \tfoo\t <%=endif%> ')
return expect(html).to.equal('foo')
+5 -5
View File
@@ -10,22 +10,22 @@ describe('LiquidOptions#strict_*', function () {
extname: '.html'
})
})
it('should not throw when strict_variables false (default)', async function () {
it('should not throw when strictVariables 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 () {
it('should throw when strictVariables true', function () {
const tpl = engine.parse('before{{notdefined}}after')
const opts = {
strict_variables: true
strictVariables: true
}
return expect(engine.render(tpl, ctx, opts)).to
.be.rejectedWith(/undefined variable: notdefined/)
})
it('should pass strict_variables to render by parseAndRender', function () {
it('should pass strictVariables to render by parseAndRender', function () {
const html = 'before{{notdefined}}after'
const opts = {
strict_variables: true
strictVariables: true
}
return expect(engine.parseAndRender(html, ctx, opts)).to
.be.rejectedWith(/undefined variable: notdefined/)
+7 -7
View File
@@ -6,34 +6,34 @@ describe('LiquidOptions#trimming', function () {
describe('tag trimming', function () {
it('should respect trim_tag_left', async function () {
const engine = new Liquid({ trim_tag_left: true })
const engine = new Liquid({ trim_tag_left: true } as any)
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 engine = new Liquid({ trim_tag_right: true } as any)
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 engine = new Liquid({ trim_tag_left: true, trim_tag_right: true } as any)
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 engine = new Liquid({ trim_output_left: true } as any)
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 engine = new Liquid({ trim_output_right: true } as any)
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 engine = new Liquid({ trim_output_left: true, trim_output_right: true } as any)
const html = await engine.parseAndRender('\t{% if true %} aha {%endif%}\t')
return expect(html).to.equal('\t aha \t')
})
@@ -46,7 +46,7 @@ describe('LiquidOptions#trimming', function () {
return expect(html).to.equal('aharttle')
})
it('should respect to greedy:false by default', async function () {
const engine = new Liquid({ greedy: false })
const engine = new Liquid({ greedy: false } as any)
const html = await engine.parseAndRender(src, ctx)
return expect(html).to.equal('\n a \nharttle ')
})
+2 -2
View File
@@ -5,8 +5,8 @@ import { mock, restore } from '../../stub/mockfs'
let engine = new Liquid()
const strictEngine = new Liquid({
strict_variables: true,
strict_filters: true
strictVariables: true,
strictFilters: true
})
describe('error', function () {