refactor: switch Context <-> Scope concepts

This commit is contained in:
harttle
2019-03-25 10:36:23 +08:00
parent 76345a64c3
commit 45e3c2bb8e
34 changed files with 272 additions and 272 deletions
+8 -8
View File
@@ -2,16 +2,16 @@ import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import { Filter } from '../../../../src/template/filter/filter'
import Scope from '../../../../src/scope/scope'
import Context from '../../../../src/context/context'
chai.use(sinonChai)
const expect = chai.expect
describe('filter', function () {
let scope: Scope
let ctx: Context
beforeEach(function () {
Filter.clear()
scope = new Scope()
ctx = new Context()
})
it('should create default filter if not registered', async function () {
const result = new Filter('foo', [], false)
@@ -19,28 +19,28 @@ describe('filter', function () {
})
it('should render input if filter not registered', async function () {
expect(await new Filter('undefined', [], false).render('foo', scope)).to.equal('foo')
expect(await new Filter('undefined', [], false).render('foo', ctx)).to.equal('foo')
})
it('should call filter impl with corrct arguments', async function () {
const spy = sinon.spy()
Filter.register('foo', spy)
await new Filter('foo', ['33'], false).render('foo', scope)
await new Filter('foo', ['33'], false).render('foo', ctx)
expect(spy).to.have.been.calledWith('foo', 33)
})
it('should render a simple filter', async function () {
Filter.register('upcase', x => x.toUpperCase())
expect(await new Filter('upcase', [], false).render('foo', scope)).to.equal('FOO')
expect(await new Filter('upcase', [], false).render('foo', ctx)).to.equal('FOO')
})
it('should render filters with argument', async function () {
Filter.register('add', (a, b) => a + b)
expect(await new Filter('add', ['2'], false).render(3, scope)).to.equal(5)
expect(await new Filter('add', ['2'], false).render(3, ctx)).to.equal(5)
})
it('should render filters with multiple arguments', async function () {
Filter.register('add', (a, b, c) => a + b + c)
expect(await new Filter('add', ['2', '"c"'], false).render(3, scope)).to.equal('5c')
expect(await new Filter('add', ['2', '"c"'], false).render(3, ctx)).to.equal('5c')
})
it('should not throw when filter name illegal', function () {
+5 -5
View File
@@ -1,5 +1,5 @@
import * as chai from 'chai'
import Scope from '../../../src/scope/scope'
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'
@@ -12,7 +12,7 @@ describe('Output', function () {
})
it('should stringify objects', async function () {
const scope = new Scope({
const scope = new Context({
foo: { obj: { arr: ['a', 2] } }
})
const output = new Output({ value: 'foo' } as OutputToken, false)
@@ -20,19 +20,19 @@ describe('Output', function () {
return expect(html).to.equal('[object Object]')
})
it('should skip function property', async function () {
const scope = new Scope({ obj: { foo: 'foo', bar: (x: any) => x } })
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]')
})
it('should respect to .toString()', async () => {
const scope = new Scope({ obj: { toString: () => 'FOO' } })
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')
})
it('should respect to .toString()', async () => {
const scope = new Scope({ obj: { toString: () => 'FOO' } })
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')
+12 -12
View File
@@ -1,6 +1,6 @@
import * as chai from 'chai'
import Tag from '../../../src/template/tag/tag'
import Scope from '../../../src/scope/scope'
import Context from '../../../src/context/context'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import Liquid from '../../../src/liquid'
@@ -11,9 +11,9 @@ const expect = chai.expect
const liquid = new Liquid()
describe('tag', function () {
let scope: Scope
let ctx: Context
before(function () {
scope = new Scope({
ctx = new Context({
foo: 'bar',
arr: [2, 1],
bar: {
@@ -51,7 +51,7 @@ describe('tag', function () {
value: 'foo',
name: 'foo'
} as TagToken
await new Tag(token, [], liquid).render(scope)
await new Tag(token, [], liquid).render(ctx)
expect(spy).to.have.been.called
})
@@ -70,30 +70,30 @@ describe('tag', function () {
} as TagToken
})
it('should call tag.render with scope', async function () {
await new Tag(token, [], liquid).render(scope)
expect(spy).to.have.been.calledWithMatch(scope)
await new Tag(token, [], liquid).render(ctx)
expect(spy).to.have.been.calledWithMatch(ctx)
})
it('should resolve identifier hash', async function () {
await new Tag(token, [], liquid).render(scope)
await new Tag(token, [], liquid).render(ctx)
expect(spy).to.have.been.calledWithMatch({}, {
aa: 'bar'
})
})
it('should accept space between key/value', async function () {
await new Tag(token, [], liquid).render(scope)
await new Tag(token, [], liquid).render(ctx)
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, {
await new Tag(token, [], liquid).render(ctx)
expect(spy).to.have.been.calledWithMatch(ctx, {
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, {
await new Tag(token, [], liquid).render(ctx)
expect(spy).to.have.been.calledWithMatch(ctx, {
dd: 'uoo'
})
})
+2 -2
View File
@@ -1,7 +1,7 @@
import * as chai from 'chai'
import * as sinonChai from 'sinon-chai'
import * as sinon from 'sinon'
import Scope from '../../../src/scope/scope'
import Context from '../../../src/context/context'
import { Filter } from '../../../src/template/filter/filter'
import Value from '../../../src/template/value'
@@ -105,7 +105,7 @@ describe('Value', function () {
Filter.register('date', date)
Filter.register('time', time)
const tpl = new Value('foo.bar | date: "b" | time:2', false)
const scope = new Scope({
const scope = new Context({
foo: { bar: 'bar' }
})
await tpl.value(scope)