fix: break/continue omitting output before them, #123

BREAKING CHANGE: remove default export, now should be used like import
{Liquid} from 'liquidjs'
This commit is contained in:
harttle
2019-08-26 10:15:43 -05:00
committed by Jun Yang
parent 854e12ba53
commit ae45c4622e
99 changed files with 373 additions and 407 deletions
+39 -40
View File
@@ -1,10 +1,10 @@
import * as chai from 'chai'
import Context from '../../../src/context/context'
import { Context } from '../../../src/context/context'
import { Scope } from '../../../src/context/scope'
const expect = chai.expect
describe('scope', function () {
describe('Context', function () {
let ctx: any, scope: Scope
beforeEach(function () {
scope = {
@@ -22,108 +22,107 @@ describe('scope', function () {
describe('#propertyAccessSeq()', function () {
it('should handle dot syntax', async function () {
expect(await ctx.parseProp('foo.bar'))
expect(ctx.parseProp('foo.bar'))
.to.deep.equal(['foo', 'bar'])
})
it('should handle [<String>] syntax', async function () {
expect(await ctx.parseProp('foo["bar"]'))
expect(ctx.parseProp('foo["bar"]'))
.to.deep.equal(['foo', 'bar'])
})
it('should handle [<Identifier>] syntax', async function () {
expect(await ctx.parseProp('foo[foo]'))
expect(ctx.parseProp('foo[foo]'))
.to.deep.equal(['foo', 'zoo'])
})
it('should handle nested access 1', async function () {
expect(await ctx.parseProp('foo[bar.zoo]'))
expect(ctx.parseProp('foo[bar.zoo]'))
.to.deep.equal(['foo', 'coo'])
})
it('should handle nested access 2', async function () {
expect(await ctx.parseProp('foo[bar["zoo"]]'))
expect(ctx.parseProp('foo[bar["zoo"]]'))
.to.deep.equal(['foo', 'coo'])
})
it('should handle nested access 3', async function () {
expect(await ctx.parseProp('bar["foo"].zoo'))
expect(ctx.parseProp('bar["foo"].zoo'))
.to.deep.equal(['bar', 'foo', 'zoo'])
})
it('should handle nested access 4', async function () {
expect(await ctx.parseProp('foo[0].bar'))
expect(ctx.parseProp('foo[0].bar'))
.to.deep.equal(['foo', '0', 'bar'])
})
it('should handle nested access 5', async function () {
expect(await ctx.parseProp('foo[one].bar'))
expect(ctx.parseProp('foo[one].bar'))
.to.deep.equal(['foo', '1', 'bar'])
})
it('should handle nested access 6', async function () {
expect(await ctx.parseProp('foo[two].bar'))
expect(ctx.parseProp('foo[two].bar'))
.to.deep.equal(['foo', 'undefined', 'bar'])
})
})
describe('#get()', function () {
it('should get direct property', async function () {
expect(await await ctx.get('foo')).equal('zoo')
expect(ctx.get('foo')).equal('zoo')
})
it('undefined property should yield undefined', async function () {
expect(ctx.get('notdefined')).to.be.rejected
expect(await ctx.get('notdefined')).to.equal(undefined)
expect(await ctx.get(false as any)).to.equal(undefined)
expect(ctx.get('notdefined')).to.equal(undefined)
expect(ctx.get(false as any)).to.equal(undefined)
})
it('should throw for invalid path', async function () {
expect(ctx.get('')).to.be.rejectedWith('invalid path:""')
expect(() => ctx.get('')).to.throw('invalid path:""')
})
it('should throw when [] unbalanced', async function () {
expect(ctx.get('foo[bar')).to.be.rejectedWith(/unbalanced \[\]/)
expect(() => ctx.get('foo[bar')).to.throw(/unbalanced \[\]/)
})
it('should throw when "" unbalanced', async function () {
expect(ctx.get('foo["bar]')).to.be.rejectedWith(/unbalanced "/)
expect(() => ctx.get('foo["bar]')).to.throw(/unbalanced "/)
})
it("should throw when '' unbalanced", async function () {
expect(ctx.get("foo['bar]")).to.be.rejectedWith(/unbalanced '/)
expect(() => ctx.get("foo['bar]")).to.throw(/unbalanced '/)
})
it('should respect to toLiquid', async function () {
const scope = new Context({ foo: {
toLiquid: () => ({ bar: 'BAR' }),
bar: 'bar'
} })
expect(await scope.get('foo.bar')).to.equal('BAR')
expect(scope.get('foo.bar')).to.equal('BAR')
})
it('should access child property via dot syntax', async function () {
expect(await ctx.get('bar.zoo')).to.equal('coo')
expect(await ctx.get('bar.arr')).to.deep.equal(['a', 'b'])
expect(ctx.get('bar.zoo')).to.equal('coo')
expect(ctx.get('bar.arr')).to.deep.equal(['a', 'b'])
})
it('should access child property via [<String>] syntax', async function () {
expect(await ctx.get('bar["zoo"]')).to.equal('coo')
expect(ctx.get('bar["zoo"]')).to.equal('coo')
})
it('should access child property via [<Number>] syntax', async function () {
expect(await ctx.get('bar.arr[0]')).to.equal('a')
expect(ctx.get('bar.arr[0]')).to.equal('a')
})
it('should access child property via [<Identifier>] syntax', async function () {
expect(await ctx.get('bar[foo]')).to.equal('coo')
expect(ctx.get('bar[foo]')).to.equal('coo')
})
it('should return undefined when not exist', async function () {
expect(await ctx.get('foo.foo.foo')).to.be.undefined
expect(ctx.get('foo.foo.foo')).to.be.undefined
})
it('should return string length as size', async function () {
expect(await ctx.get('foo.size')).to.equal(3)
expect(ctx.get('foo.size')).to.equal(3)
})
it('should return array length as size', async function () {
expect(await ctx.get('bar.arr.size')).to.equal(2)
expect(ctx.get('bar.arr.size')).to.equal(2)
})
it('should return size property if exists', async function () {
expect(await ctx.get('zoo.size')).to.equal(4)
expect(ctx.get('zoo.size')).to.equal(4)
})
it('should return undefined if do not have size and length', async function () {
expect(await ctx.get('one.size')).to.equal(undefined)
expect(ctx.get('one.size')).to.equal(undefined)
})
})
describe('strictVariables', async function () {
@@ -134,28 +133,28 @@ describe('scope', function () {
} as any)
})
it('should throw when variable not defined', function () {
return expect(ctx.get('notdefined')).to.be.rejectedWith(/undefined variable: notdefined/)
return expect(() => ctx.get('notdefined')).to.throw(/undefined variable: notdefined/)
})
it('should throw when deep variable not exist', async function () {
ctx.push({ foo: 'FOO' })
return expect(ctx.get('foo.bar.not.defined')).to.be.rejectedWith(/undefined variable: bar/)
return expect(() => ctx.get('foo.bar.not.defined')).to.throw(/undefined variable: bar/)
})
it('should throw when itself not defined', async function () {
ctx.push({ foo: 'FOO' })
return expect(ctx.get('foo.BAR')).to.be.rejectedWith(/undefined variable: BAR/)
return expect(() => ctx.get('foo.BAR')).to.throw(/undefined variable: BAR/)
})
it('should find variable in parent scope', async function () {
ctx.push({ 'foo': 'foo' })
ctx.push({
'bar': 'bar'
})
expect(await ctx.get('foo')).to.equal('foo')
expect(ctx.get('foo')).to.equal('foo')
})
})
describe('.getAll()', function () {
it('should get all properties when arguments empty', async function () {
expect(await ctx.getAll()).deep.equal(scope)
expect(ctx.getAll()).deep.equal(scope)
})
})
@@ -165,14 +164,14 @@ describe('scope', function () {
ctx.push({
foo: 'foo'
})
expect(await ctx.get('foo')).to.equal('foo')
expect(await ctx.get('bar')).to.equal('bar')
expect(ctx.get('foo')).to.equal('foo')
expect(ctx.get('bar')).to.equal('bar')
})
it('should hide deep properties by push', async function () {
ctx.push({ bar: { bar: 'bar' } })
ctx.push({ bar: { foo: 'foo' } })
expect(await ctx.get('bar.foo')).to.equal('foo')
expect(await ctx.get('bar.bar')).to.equal(undefined)
expect(ctx.get('bar.foo')).to.equal('foo')
expect(ctx.get('bar.bar')).to.equal(undefined)
})
})
describe('.pop()', function () {
@@ -181,7 +180,7 @@ describe('scope', function () {
foo: 'foo'
})
ctx.pop()
expect(await ctx.get('foo')).to.equal('zoo')
expect(ctx.get('foo')).to.equal('zoo')
})
})
})
+4 -4
View File
@@ -1,8 +1,8 @@
import { expect } from 'chai'
import Tokenizer from '../../../src/parser/tokenizer'
import TagToken from '../../../src/parser/tag-token'
import OutputToken from '../../../src/parser/output-token'
import HTMLToken from '../../../src/parser/html-token'
import { Tokenizer } from '../../../src/parser/tokenizer'
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()
+5 -5
View File
@@ -1,10 +1,10 @@
import { expect } from 'chai'
import Context from '../../../src/context/context'
import Token from '../../../src/parser/token'
import Tag from '../../../src/template/tag/tag'
import { Context } from '../../../src/context/context'
import { Token } from '../../../src/parser/token'
import { Tag } from '../../../src/template/tag/tag'
import { Filter } from '../../../src/template/filter/filter'
import Render from '../../../src/render/render'
import HTML from '../../../src/template/html'
import { Render } from '../../../src/render/render'
import { HTML } from '../../../src/template/html'
describe('render', function () {
let render: Render
+2 -2
View File
@@ -1,4 +1,4 @@
import Context from '../../../src/context/context'
import { Context } from '../../../src/context/context'
import { expect } from 'chai'
import { evalExp, evalValue, isTruthy } from '../../../src/render/syntax'
@@ -62,7 +62,7 @@ describe('render/syntax', function () {
describe('.evalExp()', function () {
it('should throw when scope undefined', async function () {
return expect((evalExp as any)('')).to.be.rejectedWith(/scope undefined/)
return expect(() => (evalExp as any)('')).to.throw(/scope undefined/)
})
it('should eval simple expression', async function () {
+2 -2
View File
@@ -2,7 +2,7 @@ import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import { Filter } from '../../../../src/template/filter/filter'
import Context from '../../../../src/context/context'
import { Context } from '../../../../src/context/context'
chai.use(sinonChai)
const expect = chai.expect
@@ -14,7 +14,7 @@ describe('filter', function () {
ctx = new Context()
})
it('should create default filter if not registered', async function () {
const result = new Filter('foo', [], false)
const result = new Filter('foo', [], false) as any
expect(result.name).to.equal('foo')
})
+13 -11
View File
@@ -1,14 +1,16 @@
import * as chai from 'chai'
import Context from '../../../src/context/context'
import Output from '../../../src/template/output'
import OutputToken from '../../../src/parser/output-token'
import { Context } from '../../../src/context/context'
import { Output } from '../../../src/template/output'
import { OutputToken } from '../../../src/parser/output-token'
import { Filter } from '../../../src/template/filter/filter'
const expect = chai.expect
describe('Output', function () {
const emitter = { write: (html: string) => (emitter.html += html), html: '' }
beforeEach(function () {
Filter.clear()
emitter.html = ''
})
it('should stringify objects', async function () {
@@ -16,25 +18,25 @@ describe('Output', function () {
foo: { obj: { arr: ['a', 2] } }
})
const output = new Output({ value: 'foo' } as OutputToken, false)
const html = await output.render(scope)
return expect(html).to.equal('[object Object]')
await 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, false)
const html = await output.render(scope)
return expect(html).to.equal('[object Object]')
await 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, false)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
await 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, false)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
await output.render(scope, emitter)
return expect(emitter.html).to.equal('FOO')
})
})
+14 -10
View File
@@ -1,10 +1,10 @@
import * as chai from 'chai'
import Tag from '../../../src/template/tag/tag'
import Context from '../../../src/context/context'
import { Tag } from '../../../src/template/tag/tag'
import { Context } from '../../../src/context/context'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import Liquid from '../../../src/liquid'
import TagToken from '../../../src/parser/tag-token'
import { Liquid } from '../../../src/liquid'
import { TagToken } from '../../../src/parser/tag-token'
chai.use(sinonChai)
const expect = chai.expect
@@ -12,6 +12,7 @@ const liquid = new Liquid()
describe('tag', function () {
let ctx: Context
const emitter = { write: (html: string) => (emitter.html += html), html: '' }
before(function () {
ctx = new Context({
foo: 'bar',
@@ -22,6 +23,9 @@ describe('tag', function () {
})
Tag.clear()
})
beforeEach(function () {
emitter.html = ''
})
it('should throw when not registered', function () {
expect(function () {
@@ -51,7 +55,7 @@ describe('tag', function () {
value: 'foo',
name: 'foo'
} as TagToken
await new Tag(token, [], liquid).render(ctx)
await new Tag(token, [], liquid).render(ctx, emitter)
expect(spy).to.have.been.called
})
@@ -70,29 +74,29 @@ describe('tag', function () {
} as TagToken
})
it('should call tag.render with scope', async function () {
await new Tag(token, [], liquid).render(ctx)
await new Tag(token, [], liquid).render(ctx, emitter)
expect(spy).to.have.been.calledWithMatch(ctx)
})
it('should resolve identifier hash', async function () {
await new Tag(token, [], liquid).render(ctx)
await 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 new Tag(token, [], liquid).render(ctx)
await new Tag(token, [], liquid).render(ctx, emitter)
expect(spy).to.have.been.calledWithMatch({}, {
bb: 2
})
})
it('should resolve number value hash', async function () {
await new Tag(token, [], liquid).render(ctx)
await 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 new Tag(token, [], liquid).render(ctx)
await new Tag(token, [], liquid).render(ctx, emitter)
expect(spy).to.have.been.calledWithMatch(ctx, {
dd: 'uoo'
})
+2 -2
View File
@@ -1,9 +1,9 @@
import * as chai from 'chai'
import * as sinonChai from 'sinon-chai'
import * as sinon from 'sinon'
import Context from '../../../src/context/context'
import { Context } from '../../../src/context/context'
import { Filter } from '../../../src/template/filter/filter'
import Value from '../../../src/template/value'
import { Value } from '../../../src/template/value'
chai.use(sinonChai)
+1 -1
View File
@@ -1,5 +1,5 @@
import * as chai from 'chai'
import assert from '../../../src/util/assert'
import { assert } from '../../../src/util/assert'
const expect = chai.expect