refactor: remove mock-fs from dev dependency

This commit is contained in:
harttle
2019-02-19 22:28:27 +08:00
parent f432aade6a
commit 852c6ad453
70 changed files with 711 additions and 795 deletions
+95
View File
@@ -0,0 +1,95 @@
import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import Filter from 'src/template/filter'
import Scope from 'src/scope/scope'
chai.use(sinonChai)
const expect = chai.expect
describe('filter', function () {
let scope
beforeEach(function () {
Filter.clear()
scope = new Scope()
})
it('should return default filter when not registered', function () {
const result = new Filter('foo')
expect(result.name).to.equal('foo')
})
it('should throw when filter name illegal', function () {
expect(function () {
new Filter('/')
}).to.throw(/illegal filter/)
})
it('should parse argument syntax', function () {
Filter.register('foo', x => x)
const f = new Filter('foo: a, "b"')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal(['a', '"b"'])
})
it('should register a simple filter', function () {
Filter.register('upcase', x => x.toUpperCase())
expect(new Filter('upcase').render('foo', scope)).to.equal('FOO')
})
it('should register a argumented filter', function () {
Filter.register('add', (a, b) => a + b)
expect(new Filter('add: 2').render(3, scope)).to.equal(5)
})
it('should register a multi-argumented filter', function () {
Filter.register('add', (a, b, c) => a + b + c)
expect(new Filter('add: 2, "c"').render(3, scope)).to.equal('5c')
})
it('should call filter with corrct arguments', function () {
const spy = sinon.spy()
Filter.register('foo', spy)
new Filter('foo: 33').render('foo', scope)
expect(spy).to.have.been.calledWith('foo', 33)
})
it('should support arguments as named key/values', function () {
Filter.register('foo', x => x)
const f = new Filter('foo: key1: "literal1", key2: value2')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal([ '\'key1\'', '"literal1"', '\'key2\'', 'value2' ])
})
it('should support arguments as named key/values with inline literals', function () {
Filter.register('foo', x => x)
const f = new Filter('foo: "test0", key1: "literal1", key2: value2')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal([ '"test0"', '\'key1\'', '"literal1"', '\'key2\'', 'value2' ])
})
it('should support arguments as named key/values with inline values', function () {
Filter.register('foo', x => x)
const f = new Filter('foo: test0, key1: "literal1", key2: value2')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal([ 'test0', '\'key1\'', '"literal1"', '\'key2\'', 'value2' ])
})
it('should support argument values named same as keys', function () {
Filter.register('foo', x => x)
const f = new Filter('foo: a: a')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal(['\'a\'', 'a'])
})
it('should support argument literals named same as keys', function () {
Filter.register('foo', x => x)
const f = new Filter('foo: a: "a"')
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal(['\'a\'', '"a"'])
})
it('should not throw undefined filter by default', function () {
expect(new Filter('undefined').render('foo', scope)).to.equal('foo')
})
})
+61
View File
@@ -0,0 +1,61 @@
import * as chai from 'chai'
import Scope from 'src/scope/scope'
import Output from 'src/template/output'
import OutputToken from 'src/parser/output-token'
import Filter from 'src/template/filter'
const expect = chai.expect
describe('Output', function () {
beforeEach(function () {
Filter.clear()
})
it('should respect to .to_liquid() method', async function () {
const scope = new Scope({
bar: { to_liquid: () => 'custom' }
})
const output = new Output({ value: 'bar' } as OutputToken)
const html = await output.render(scope)
return expect(html).to.equal('custom')
})
it('should stringify objects', async function () {
const scope = new Scope({
foo: { obj: { arr: ['a', 2] } }
})
const output = new Output({ value: 'foo' } as OutputToken)
const html = await output.render(scope)
return expect(html).to.equal('{"obj":{"arr":["a",2]}}')
})
it('should skip circular property', async function () {
const ctx = { foo: { num: 2 }, bar: 'bar' } as any
ctx.foo.circular = ctx
const output = new Output({ value: 'foo' } as OutputToken)
const html = await output.render(new Scope(ctx))
return expect(html).equal('{"num":2,"circular":{"bar":"bar"}}')
})
it('should skip function property', async function () {
const scope = new Scope({ obj: { foo: 'foo', bar: x => x } })
const output = new Output({ value: 'obj' } as OutputToken)
const html = await output.render(scope)
return expect(html).to.equal('{"foo":"foo"}')
})
it('should respect to .toString()', async () => {
const scope = new Scope({ obj: { toString: () => 'FOO' } })
const output = new Output({ value: 'obj' } as OutputToken)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
it('should respect to .to_s()', async () => {
const scope = new Scope({ obj: { to_s: () => 'FOO' } })
const output = new Output({ value: 'obj' } as OutputToken)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
it('should respect to .liquid_method_missing()', async () => {
const scope = new Scope({ obj: { liquid_method_missing: x => x.toUpperCase() } })
const output = new Output({ value: 'obj.foo' } as OutputToken)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
})
+101
View File
@@ -0,0 +1,101 @@
import * as chai from 'chai'
import Tag from 'src/template/tag/tag'
import Scope from 'src/scope/scope'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import Liquid from 'src/liquid'
import TagToken from 'src/parser/tag-token'
chai.use(sinonChai)
const expect = chai.expect
const liquid = new Liquid()
describe('tag', function () {
let scope
before(function () {
scope = new Scope({
foo: 'bar',
arr: [2, 1],
bar: {
coo: 'uoo'
}
})
Tag.clear()
})
it('should throw when not registered', function () {
expect(function () {
new Tag({ // eslint-disable-line
type: 'tag',
value: 'foo',
name: 'foo'
} as TagToken, [], liquid)
}).to.throw(/tag foo not found/)
})
it('should register simple tag', function () {
expect(function () {
Tag.register('foo', {
render: () => 'bar'
})
}).not.throw()
})
it('should call tag.render', async function () {
const spy = sinon.spy()
Tag.register('foo', {
render: spy
})
const token = {
type: 'tag',
value: 'foo',
name: 'foo'
} as TagToken
await new Tag(token, [], liquid).render(scope)
expect(spy).to.have.been.called
})
describe('hash', function () {
let spy, token
beforeEach(function () {
spy = sinon.spy()
Tag.register('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'
}
})
it('should call tag.render with scope', async function () {
await new Tag(token, [], liquid).render(scope)
expect(spy).to.have.been.calledWithMatch(scope)
})
it('should resolve identifier hash', async function () {
await new Tag(token, [], liquid).render(scope)
expect(spy).to.have.been.calledWithMatch({}, {
aa: 'bar'
})
})
it('should accept space between key/value', async function () {
await new Tag(token, [], liquid).render(scope)
expect(spy).to.have.been.calledWithMatch({}, {
bb: 2
})
})
it('should resolve number value hash', async function () {
await new Tag(token, [], liquid).render(scope)
expect(spy).to.have.been.calledWithMatch(scope, {
cc: 2.3
})
})
it('should resolve property access hash', async function () {
await new Tag(token, [], liquid).render(scope)
expect(spy).to.have.been.calledWithMatch(scope, {
dd: 'uoo'
})
})
})
})
+69
View File
@@ -0,0 +1,69 @@
import * as chai from 'chai'
import * as sinonChai from 'sinon-chai'
import * as sinon from 'sinon'
import Scope from 'src/scope/scope'
import Filter from 'src/template/filter'
import Value from 'src/template/value'
chai.use(sinonChai)
const expect = chai.expect
const add = (l, r) => l + r
describe('Value', function () {
beforeEach(() => Filter.clear())
it('should throw when value string illegal', function () {
expect(function () {
new Value('/')
}).to.throw(/illegal value string/)
})
it('should parse value string', function () {
const tpl: any = new Value('foo')
expect(tpl.initial).to.equal('foo')
expect(tpl.filters).to.deep.equal([])
})
it('should parse value string with a simple filter', function () {
Filter.register('add', add)
const tpl: any = new Value('foo | add: 3, "foo"')
expect(tpl.initial).to.equal('foo')
expect(tpl.filters.length).to.equal(1)
expect(tpl.filters[0].impl).to.equal(add)
})
it('should parse value string with filters', function () {
const tpl: any = new Value('foo | add: "|" | add')
expect(tpl.initial).to.equal('foo')
expect(tpl.filters.length).to.equal(2)
})
it('should eval value', function () {
Filter.register('date', (l, r) => l + r)
Filter.register('time', (l, r) => l + 3 * r)
const tpl = new Value('foo.bar[0] | date: "b" | time:2')
const scope = new Scope({
foo: { bar: ['a'] }
})
expect(tpl.value(scope)).to.equal('ab6')
})
it('should reserve type', function () {
Filter.register('arr', () => [1])
const tpl = new Value('"x" | arr')
expect(tpl.value(new Scope())).to.deep.equal([1])
})
it('should eval filter with correct arguments', function () {
const date = sinon.stub().returns('y')
const time = sinon.spy()
Filter.register('date', date)
Filter.register('time', time)
const tpl = new Value('foo.bar | date: "b" | time:2')
const scope = new Scope({
foo: { bar: 'bar' }
})
tpl.value(scope)
expect(date).to.have.been.calledWith('bar', 'b')
expect(time).to.have.been.calledWith('y', 2)
})
})