mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
feat: promise support for drops, working on #65
This commit is contained in:
+101
-121
@@ -1,5 +1,6 @@
|
||||
import * as chai from 'chai'
|
||||
import Scope, { Context } from '../../../src/scope/scope'
|
||||
import Scope from '../../../src/scope/scope'
|
||||
import { Context } from '../../../src/scope/context'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
@@ -20,168 +21,156 @@ describe('scope', function () {
|
||||
})
|
||||
|
||||
describe('#propertyAccessSeq()', function () {
|
||||
it('should handle dot syntax', function () {
|
||||
expect(scope.propertyAccessSeq('foo.bar'))
|
||||
it('should handle dot syntax', async function () {
|
||||
expect(await scope.propertyAccessSeq('foo.bar'))
|
||||
.to.deep.equal(['foo', 'bar'])
|
||||
})
|
||||
it('should handle [<String>] syntax', function () {
|
||||
expect(scope.propertyAccessSeq('foo["bar"]'))
|
||||
it('should handle [<String>] syntax', async function () {
|
||||
expect(await scope.propertyAccessSeq('foo["bar"]'))
|
||||
.to.deep.equal(['foo', 'bar'])
|
||||
})
|
||||
it('should handle [<Identifier>] syntax', function () {
|
||||
expect(scope.propertyAccessSeq('foo[foo]'))
|
||||
it('should handle [<Identifier>] syntax', async function () {
|
||||
expect(await scope.propertyAccessSeq('foo[foo]'))
|
||||
.to.deep.equal(['foo', 'zoo'])
|
||||
})
|
||||
it('should handle nested access 1', function () {
|
||||
expect(scope.propertyAccessSeq('foo[bar.zoo]'))
|
||||
it('should handle nested access 1', async function () {
|
||||
expect(await scope.propertyAccessSeq('foo[bar.zoo]'))
|
||||
.to.deep.equal(['foo', 'coo'])
|
||||
})
|
||||
it('should handle nested access 2', function () {
|
||||
expect(scope.propertyAccessSeq('foo[bar["zoo"]]'))
|
||||
it('should handle nested access 2', async function () {
|
||||
expect(await scope.propertyAccessSeq('foo[bar["zoo"]]'))
|
||||
.to.deep.equal(['foo', 'coo'])
|
||||
})
|
||||
it('should handle nested access 3', function () {
|
||||
expect(scope.propertyAccessSeq('bar["foo"].zoo'))
|
||||
it('should handle nested access 3', async function () {
|
||||
expect(await scope.propertyAccessSeq('bar["foo"].zoo'))
|
||||
.to.deep.equal(['bar', 'foo', 'zoo'])
|
||||
})
|
||||
it('should handle nested access 4', function () {
|
||||
expect(scope.propertyAccessSeq('foo[0].bar'))
|
||||
it('should handle nested access 4', async function () {
|
||||
expect(await scope.propertyAccessSeq('foo[0].bar'))
|
||||
.to.deep.equal(['foo', '0', 'bar'])
|
||||
})
|
||||
it('should handle nested access 5', function () {
|
||||
expect(scope.propertyAccessSeq('foo[one].bar'))
|
||||
it('should handle nested access 5', async function () {
|
||||
expect(await scope.propertyAccessSeq('foo[one].bar'))
|
||||
.to.deep.equal(['foo', '1', 'bar'])
|
||||
})
|
||||
it('should handle nested access 6', function () {
|
||||
expect(scope.propertyAccessSeq('foo[two].bar'))
|
||||
it('should handle nested access 6', async function () {
|
||||
expect(await scope.propertyAccessSeq('foo[two].bar'))
|
||||
.to.deep.equal(['foo', 'undefined', 'bar'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('#get()', function () {
|
||||
it('should get direct property', function () {
|
||||
expect(scope.get('foo')).equal('zoo')
|
||||
it('should get direct property', async function () {
|
||||
expect(await await scope.get('foo')).equal('zoo')
|
||||
})
|
||||
|
||||
it('undefined property should yield undefined', function () {
|
||||
function fn () {
|
||||
scope.get('notdefined')
|
||||
}
|
||||
expect(fn).to.not.throw()
|
||||
expect(scope.get('notdefined')).to.equal(undefined)
|
||||
expect(scope.get(false as any)).to.equal(undefined)
|
||||
it('undefined property should yield undefined', async function () {
|
||||
expect(scope.get('notdefined')).to.be.rejected
|
||||
expect(await scope.get('notdefined')).to.equal(undefined)
|
||||
expect(await scope.get(false as any)).to.equal(undefined)
|
||||
})
|
||||
|
||||
it('should throw for invalid path', function () {
|
||||
function fn () {
|
||||
scope.get('')
|
||||
}
|
||||
expect(fn).to.throw('invalid path:""')
|
||||
it('should throw for invalid path', async function () {
|
||||
expect(scope.get('')).to.be.rejectedWith('invalid path:""')
|
||||
})
|
||||
|
||||
it('should throw when [] unbalanced', function () {
|
||||
expect(function () {
|
||||
scope.get('foo[bar')
|
||||
}).to.throw(/unbalanced \[\]/)
|
||||
it('should throw when [] unbalanced', async function () {
|
||||
expect(scope.get('foo[bar')).to.be.rejectedWith(/unbalanced \[\]/)
|
||||
})
|
||||
|
||||
it('should throw when "" unbalanced', function () {
|
||||
expect(function () {
|
||||
scope.get('foo["bar]')
|
||||
}).to.throw(/unbalanced "/)
|
||||
it('should throw when "" unbalanced', async function () {
|
||||
expect(scope.get('foo["bar]')).to.be.rejectedWith(/unbalanced "/)
|
||||
})
|
||||
|
||||
it("should throw when '' unbalanced", function () {
|
||||
expect(function () {
|
||||
scope.get("foo['bar]")
|
||||
}).to.throw(/unbalanced '/)
|
||||
it("should throw when '' unbalanced", async function () {
|
||||
expect(scope.get("foo['bar]")).to.be.rejectedWith(/unbalanced '/)
|
||||
})
|
||||
|
||||
it('should respect to to_liquid', function () {
|
||||
it('should respect to to_liquid', async function () {
|
||||
const scope = new Scope({ foo: {
|
||||
to_liquid: () => ({ bar: 'BAR' }),
|
||||
bar: 'bar'
|
||||
} })
|
||||
expect(scope.get('foo.bar')).to.equal('BAR')
|
||||
expect(await scope.get('foo.bar')).to.equal('BAR')
|
||||
})
|
||||
|
||||
it('should respect to toLiquid', function () {
|
||||
it('should respect to toLiquid', async function () {
|
||||
const scope = new Scope({ foo: {
|
||||
toLiquid: () => ({ bar: 'BAR' }),
|
||||
bar: 'bar'
|
||||
} })
|
||||
expect(scope.get('foo.bar')).to.equal('BAR')
|
||||
expect(await scope.get('foo.bar')).to.equal('BAR')
|
||||
})
|
||||
|
||||
it('should access child property via dot syntax', function () {
|
||||
expect(scope.get('bar.zoo')).to.equal('coo')
|
||||
expect(scope.get('bar.arr')).to.deep.equal(['a', 'b'])
|
||||
it('should access child property via dot syntax', async function () {
|
||||
expect(await scope.get('bar.zoo')).to.equal('coo')
|
||||
expect(await scope.get('bar.arr')).to.deep.equal(['a', 'b'])
|
||||
})
|
||||
|
||||
it('should access child property via [<String>] syntax', function () {
|
||||
expect(scope.get('bar["zoo"]')).to.equal('coo')
|
||||
it('should access child property via [<String>] syntax', async function () {
|
||||
expect(await scope.get('bar["zoo"]')).to.equal('coo')
|
||||
})
|
||||
|
||||
it('should access child property via [<Number>] syntax', function () {
|
||||
expect(scope.get('bar.arr[0]')).to.equal('a')
|
||||
it('should access child property via [<Number>] syntax', async function () {
|
||||
expect(await scope.get('bar.arr[0]')).to.equal('a')
|
||||
})
|
||||
|
||||
it('should access child property via [<Identifier>] syntax', function () {
|
||||
expect(scope.get('bar[foo]')).to.equal('coo')
|
||||
it('should access child property via [<Identifier>] syntax', async function () {
|
||||
expect(await scope.get('bar[foo]')).to.equal('coo')
|
||||
})
|
||||
|
||||
it('should return undefined when not exist', function () {
|
||||
expect(scope.get('foo.foo.foo')).to.be.undefined
|
||||
it('should return undefined when not exist', async function () {
|
||||
expect(await scope.get('foo.foo.foo')).to.be.undefined
|
||||
})
|
||||
it('should return string length as size', function () {
|
||||
expect(scope.get('foo.size')).to.equal(3)
|
||||
it('should return string length as size', async function () {
|
||||
expect(await scope.get('foo.size')).to.equal(3)
|
||||
})
|
||||
it('should return array length as size', function () {
|
||||
expect(scope.get('bar.arr.size')).to.equal(2)
|
||||
it('should return array length as size', async function () {
|
||||
expect(await scope.get('bar.arr.size')).to.equal(2)
|
||||
})
|
||||
it('should return size property if exists', function () {
|
||||
expect(scope.get('zoo.size')).to.equal(4)
|
||||
it('should return size property if exists', async function () {
|
||||
expect(await scope.get('zoo.size')).to.equal(4)
|
||||
})
|
||||
it('should return undefined if do not have size and length', function () {
|
||||
expect(scope.get('one.size')).to.equal(undefined)
|
||||
it('should return undefined if do not have size and length', async function () {
|
||||
expect(await scope.get('one.size')).to.equal(undefined)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#set', function () {
|
||||
it('should set nested value', function () {
|
||||
scope.set('posts', {
|
||||
it('should set nested value', async function () {
|
||||
await scope.set('posts', {
|
||||
'first': {
|
||||
'name': 'A Nice Day'
|
||||
}
|
||||
})
|
||||
scope.set('category', {
|
||||
await scope.set('category', {
|
||||
'diary': ['first']
|
||||
})
|
||||
expect(scope.get('posts[category.diary[0]].name'), 'A Nice Day')
|
||||
expect(await scope.get('posts[category.diary[0]].name'), 'A Nice Day')
|
||||
})
|
||||
|
||||
it('should create parent if needed', function () {
|
||||
scope.set('a.b.c.d', 'COO')
|
||||
expect(scope.get('a.b.c.d')).to.equal('COO')
|
||||
it('should create parent if needed', async function () {
|
||||
await scope.set('a.b.c.d', 'COO')
|
||||
expect(await scope.get('a.b.c.d')).to.equal('COO')
|
||||
})
|
||||
it('should keep other properties of parent', function () {
|
||||
it('should keep other properties of parent', async function () {
|
||||
scope.push({ obj: { foo: 'FOO' } })
|
||||
scope.set('obj.bar', 'BAR')
|
||||
expect(scope.get('obj.foo')).to.equal('FOO')
|
||||
await scope.set('obj.bar', 'BAR')
|
||||
expect(await scope.get('obj.foo')).to.equal('FOO')
|
||||
})
|
||||
it('should abort if property cannot be set', function () {
|
||||
it('should abort if property cannot be set', async function () {
|
||||
scope.push({ obj: { foo: 'FOO' } })
|
||||
scope.set('obj.foo.bar', 'BAR')
|
||||
expect(scope.get('obj.foo')).to.equal('FOO')
|
||||
await scope.set('obj.foo.bar', 'BAR')
|
||||
expect(await scope.get('obj.foo')).to.equal('FOO')
|
||||
})
|
||||
it("should set parents' corresponding value", function () {
|
||||
it("should set parents' corresponding value", async function () {
|
||||
scope.push({})
|
||||
scope.set('foo', 'bar')
|
||||
await scope.set('foo', 'bar')
|
||||
scope.pop()
|
||||
expect(scope.get('foo')).to.equal('bar')
|
||||
expect(await scope.get('foo')).to.equal('bar')
|
||||
})
|
||||
})
|
||||
describe('strictVariables', function () {
|
||||
describe('strictVariables', async function () {
|
||||
let scope: Scope
|
||||
beforeEach(function () {
|
||||
scope = new Scope(ctx, {
|
||||
@@ -189,66 +178,57 @@ describe('scope', function () {
|
||||
} as any)
|
||||
})
|
||||
it('should throw when variable not defined', function () {
|
||||
function fn () {
|
||||
scope.get('notdefined')
|
||||
}
|
||||
expect(fn).to.throw(/undefined variable: notdefined/)
|
||||
return expect(scope.get('notdefined')).to.be.rejectedWith(/undefined variable: notdefined/)
|
||||
})
|
||||
it('should throw when deep variable not exist', function () {
|
||||
scope.set('foo', 'FOO')
|
||||
function fn () {
|
||||
scope.get('foo.bar.not.defined')
|
||||
}
|
||||
expect(fn).to.throw(/undefined variable: bar/)
|
||||
it('should throw when deep variable not exist', async function () {
|
||||
await scope.set('foo', 'FOO')
|
||||
return expect(scope.get('foo.bar.not.defined')).to.be.rejectedWith(/undefined variable: bar/)
|
||||
})
|
||||
it('should throw when itself not defined', function () {
|
||||
scope.set('foo', 'bar')
|
||||
function fn () {
|
||||
scope.get('foo.BAR')
|
||||
}
|
||||
expect(fn).to.throw(/undefined variable: BAR/)
|
||||
it('should throw when itself not defined', async function () {
|
||||
await scope.set('foo', 'bar')
|
||||
return expect(scope.get('foo.BAR')).to.be.rejectedWith(/undefined variable: BAR/)
|
||||
})
|
||||
it('should find variable in parent scope', function () {
|
||||
scope.set('foo', 'foo')
|
||||
it('should find variable in parent scope', async function () {
|
||||
await scope.set('foo', 'foo')
|
||||
scope.push({
|
||||
'bar': 'bar'
|
||||
})
|
||||
expect(scope.get('foo')).to.equal('foo')
|
||||
expect(await scope.get('foo')).to.equal('foo')
|
||||
})
|
||||
})
|
||||
|
||||
describe('.getAll()', function () {
|
||||
it('should get all properties when arguments empty', function () {
|
||||
expect(scope.getAll()).deep.equal(ctx)
|
||||
it('should get all properties when arguments empty', async function () {
|
||||
expect(await scope.getAll()).deep.equal(ctx)
|
||||
})
|
||||
})
|
||||
|
||||
describe('.push()', function () {
|
||||
it('should push scope', function () {
|
||||
scope.set('bar', 'bar')
|
||||
it('should push scope', async function () {
|
||||
await scope.set('bar', 'bar')
|
||||
scope.push({
|
||||
foo: 'foo'
|
||||
})
|
||||
expect(scope.get('foo')).to.equal('foo')
|
||||
expect(scope.get('bar')).to.equal('bar')
|
||||
expect(await scope.get('foo')).to.equal('foo')
|
||||
expect(await scope.get('bar')).to.equal('bar')
|
||||
})
|
||||
it('should hide deep properties by push', function () {
|
||||
scope.set('bar', { bar: 'bar' })
|
||||
it('should hide deep properties by push', async function () {
|
||||
await scope.set('bar', { bar: 'bar' })
|
||||
scope.push({ bar: { foo: 'foo' } })
|
||||
expect(scope.get('bar.foo')).to.equal('foo')
|
||||
expect(scope.get('bar.bar')).to.equal(undefined)
|
||||
expect(await scope.get('bar.foo')).to.equal('foo')
|
||||
expect(await scope.get('bar.bar')).to.equal(undefined)
|
||||
})
|
||||
})
|
||||
describe('.pop()', function () {
|
||||
it('should pop scope', function () {
|
||||
it('should pop scope', async function () {
|
||||
scope.push({
|
||||
foo: 'foo'
|
||||
})
|
||||
scope.pop()
|
||||
expect(scope.get('foo')).to.equal('zoo')
|
||||
expect(await scope.get('foo')).to.equal('zoo')
|
||||
})
|
||||
})
|
||||
it('should pop specified scope', function () {
|
||||
it('should pop specified scope', async function () {
|
||||
const scope1 = {
|
||||
foo: 'foo'
|
||||
}
|
||||
@@ -257,11 +237,11 @@ describe('scope', function () {
|
||||
}
|
||||
scope.push(scope1)
|
||||
scope.push(scope2)
|
||||
expect(scope.get('foo')).to.equal('foo')
|
||||
expect(scope.get('bar')).to.equal('bar')
|
||||
expect(await scope.get('foo')).to.equal('foo')
|
||||
expect(await scope.get('bar')).to.equal('bar')
|
||||
scope.pop(scope1)
|
||||
expect(scope.get('foo')).to.equal('zoo')
|
||||
expect(scope.get('bar')).to.equal('bar')
|
||||
expect(await scope.get('foo')).to.equal('zoo')
|
||||
expect(await scope.get('bar')).to.equal('bar')
|
||||
})
|
||||
it('should throw when specified scope not found', function () {
|
||||
const scope1 = {
|
||||
|
||||
Reference in New Issue
Block a user