mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
feature: interpolate bracket access, close #14
This commit is contained in:
+101
-56
@@ -4,69 +4,114 @@ const expect = chai.expect;
|
||||
var Scope = require('../src/scope.js');
|
||||
|
||||
describe('scope', function() {
|
||||
var scope, ctx;
|
||||
beforeEach(function() {
|
||||
ctx = {
|
||||
foo: 'bar'
|
||||
};
|
||||
scope = Scope.factory(ctx);
|
||||
});
|
||||
var scope, ctx;
|
||||
beforeEach(function() {
|
||||
ctx = {
|
||||
foo: 'zoo',
|
||||
bar: {
|
||||
zoo: 'coo',
|
||||
"Mr.Smith": 'John',
|
||||
arr: ['a', 'b']
|
||||
}
|
||||
};
|
||||
scope = Scope.factory(ctx);
|
||||
});
|
||||
|
||||
it('should get direct property', function() {
|
||||
expect(scope.get('foo')).equal('bar');
|
||||
});
|
||||
describe('#propertyAccessSeq()', function() {
|
||||
it('should handle dot syntax', function() {
|
||||
expect(scope.propertyAccessSeq('foo.bar'))
|
||||
.to.deep.equal(['foo', 'bar']);
|
||||
});
|
||||
it('should handle [<String>] syntax', function() {
|
||||
expect(scope.propertyAccessSeq('foo["bar"]'))
|
||||
.to.deep.equal(['foo', 'bar']);
|
||||
});
|
||||
it('should handle [<Identifier>] syntax', function() {
|
||||
expect(scope.propertyAccessSeq('foo[foo]'))
|
||||
.to.deep.equal(['foo', 'zoo']);
|
||||
});
|
||||
it('should handle nested access', function() {
|
||||
expect(scope.propertyAccessSeq('foo[bar.zoo]'))
|
||||
.to.deep.equal(['foo', 'coo']);
|
||||
expect(scope.propertyAccessSeq('foo[bar["zoo"]]'))
|
||||
.to.deep.equal(['foo', 'coo']);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get undefined property', function() {
|
||||
function fn() {
|
||||
scope.get('notdefined');
|
||||
}
|
||||
expect(fn).to.not.throw();
|
||||
expect(scope.get('notdefined')).to.equal(undefined);
|
||||
expect(scope.get('')).to.equal(undefined);
|
||||
expect(scope.get(false)).to.equal(undefined);
|
||||
});
|
||||
describe('#get()', function() {
|
||||
it('should get direct property', function() {
|
||||
expect(scope.get('foo')).equal('zoo');
|
||||
});
|
||||
|
||||
it('should throw undefined in strict mode', function() {
|
||||
scope = Scope.factory(ctx, {
|
||||
strict: true
|
||||
});
|
||||
it('should get undefined property', function() {
|
||||
function fn() {
|
||||
scope.get('notdefined');
|
||||
}
|
||||
expect(fn).to.not.throw();
|
||||
expect(scope.get('notdefined')).to.equal(undefined);
|
||||
expect(scope.get('')).to.equal(undefined);
|
||||
expect(scope.get(false)).to.equal(undefined);
|
||||
});
|
||||
|
||||
function fn() {
|
||||
scope.get('notdefined');
|
||||
}
|
||||
expect(fn).to.throw(/undefined variable: notdefined/);
|
||||
});
|
||||
it('should throw undefined in strict mode', function() {
|
||||
scope = Scope.factory(ctx, {
|
||||
strict: true
|
||||
});
|
||||
|
||||
it('should get all properties when arguments empty', function() {
|
||||
expect(scope.get()).deep.equal(ctx);
|
||||
});
|
||||
function fn() {
|
||||
scope.get('notdefined');
|
||||
}
|
||||
expect(fn).to.throw(/undefined variable: notdefined/);
|
||||
});
|
||||
|
||||
it('should access child property via dot syntax', function() {
|
||||
scope.set('oo.bar', 'FOO');
|
||||
expect(scope.get('oo.bar')).to.equal('FOO');
|
||||
});
|
||||
it('should get all properties when arguments empty', function() {
|
||||
expect(scope.get()).deep.equal(ctx);
|
||||
});
|
||||
|
||||
it('should access child property via [<Number>] syntax', function() {
|
||||
scope.set('bar', ['a', {'b': [1,2]}]);
|
||||
expect(scope.get('bar[0]')).to.equal('a');
|
||||
expect(scope.get('bar[1].b')).to.deep.equal([1, 2]);
|
||||
expect(scope.get('bar[1].b[1]')).to.equal(2);
|
||||
});
|
||||
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 push scope', function() {
|
||||
scope.set('bar', 'bar');
|
||||
scope.push({
|
||||
foo: 'foo'
|
||||
});
|
||||
expect(scope.get('foo')).to.equal('foo');
|
||||
expect(scope.get('bar')).to.equal('bar');
|
||||
});
|
||||
it('should access child property via [<String>] syntax', function() {
|
||||
expect(scope.get('bar["zoo"]')).to.equal('coo');
|
||||
});
|
||||
|
||||
it('should pop scope', function() {
|
||||
scope.push({
|
||||
foo: 'foo'
|
||||
});
|
||||
scope.pop();
|
||||
expect(scope.get('foo')).to.equal('bar');
|
||||
});
|
||||
it('should access child property via [<Number>] syntax', function() {
|
||||
expect(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 support nested case', function() {
|
||||
scope.set('posts', {
|
||||
"first": {"name": "A Nice Day"}
|
||||
});
|
||||
scope.set('category', {
|
||||
"diary": ["first"]
|
||||
});
|
||||
expect(scope.get('posts[category.diary[0]].name'), 'A Nice Day');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.push(), .pop()', function() {
|
||||
it('should push scope', function() {
|
||||
scope.set('bar', 'bar');
|
||||
scope.push({
|
||||
foo: 'foo'
|
||||
});
|
||||
expect(scope.get('foo')).to.equal('foo');
|
||||
expect(scope.get('bar')).to.equal('bar');
|
||||
});
|
||||
|
||||
it('should pop scope', function() {
|
||||
scope.push({
|
||||
foo: 'foo'
|
||||
});
|
||||
scope.pop();
|
||||
expect(scope.get('foo')).to.equal('zoo');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+3
-5
@@ -13,8 +13,7 @@ describe('expression', function() {
|
||||
scope = Scope.factory({
|
||||
one: 1,
|
||||
two: 2,
|
||||
x: 'XXX',
|
||||
z: 'z'
|
||||
x: 'XXX'
|
||||
});
|
||||
});
|
||||
|
||||
@@ -41,7 +40,6 @@ describe('expression', function() {
|
||||
expect(evalExp('one<=two', scope)).to.equal(true);
|
||||
expect(evalExp('x contains "x"', scope)).to.equal(false);
|
||||
expect(evalExp('x contains "X"', scope)).to.equal(true);
|
||||
expect(evalExp('x contains z', scope)).to.equal(false);
|
||||
expect(evalExp('"<=" == "<="', scope)).to.equal(true);
|
||||
});
|
||||
|
||||
@@ -51,7 +49,7 @@ describe('expression', function() {
|
||||
});
|
||||
|
||||
it("should eval range expression", function() {
|
||||
expect(evalExp('(2..4)', scope)).to.deep.equal([2,3,4]);
|
||||
expect(evalExp('(two..4)', scope)).to.deep.equal([2,3,4]);
|
||||
expect(evalExp('(2..4)', scope)).to.deep.equal([2, 3, 4]);
|
||||
expect(evalExp('(two..4)', scope)).to.deep.equal([2, 3, 4]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user