refactor filters and corresponding tests

This commit is contained in:
harttle
2016-10-24 00:05:17 +08:00
parent 300c3710c6
commit 365a047736
10 changed files with 358 additions and 242 deletions
+35 -38
View File
@@ -1,27 +1,25 @@
var chai = require("chai");
var should = chai.should();
var expect = chai.expect;
const chai = require("chai");
const expect = chai.expect;
var Scope = require('../src/scope.js');
describe('scope', function() {
var scope, ctx;
beforeEach(function(){
beforeEach(function() {
ctx = {
foo: 'bar',
bar: ['a', {b: [1, 2]}]
foo: 'bar'
};
scope = Scope.factory(ctx);
});
it('should get property', function() {
scope.get('foo').should.equal('bar');
it('should get direct property', function() {
expect(scope.get('foo')).equal('bar');
});
it('should get undefined property', function() {
function fn(){
scope.get('notdefined');
}
function fn() {
scope.get('notdefined');
}
expect(fn).to.not.throw();
expect(scope.get('notdefined')).to.equal(undefined);
expect(scope.get('')).to.equal(undefined);
@@ -29,47 +27,46 @@ describe('scope', function() {
});
it('should throw undefined in strict mode', function() {
scope = Scope.factory(ctx, {
strict: true
});
function fn(){
scope.get('notdefined');
}
expect(fn).to.throw(/undefined variable: notdefined/);
scope = Scope.factory(ctx, {
strict: true
});
function fn() {
scope.get('notdefined');
}
expect(fn).to.throw(/undefined variable: notdefined/);
});
it('should get all property', function() {
scope.get().should.deep.equal(ctx);
it('should get all properties when arguments empty', function() {
expect(scope.get()).deep.equal(ctx);
});
it('should set property', function() {
scope.set('foo', 'FOO');
scope.get('foo').should.equal('FOO');
});
it('should set child property', function() {
it('should access child property via dot syntax', function() {
scope.set('oo.bar', 'FOO');
scope.get('oo.bar').should.equal('FOO');
expect(scope.get('oo.bar')).to.equal('FOO');
});
it('should get desendent property', function() {
scope.get('bar[0]').should.equal('a');
scope.get('bar[1].b').should.deep.equal([1, 2]);
scope.get('bar[1].b[1]').should.equal(2);
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 push scope', function() {
scope.push({foo: 'foo', foo1: 'foo1'});
scope.get('foo').should.equal('foo');
scope.get('foo1').should.equal('foo1');
scope.get('bar[1].b[1]').should.equal(2);
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', foo1: 'foo1'});
scope.push({
foo: 'foo'
});
scope.pop();
expect(scope.get('foo')).to.equal('bar');
expect(scope.get('foo1')).to.equal(undefined);
expect(scope.get('bar[1].b[1]')).to.equal(2);
});
});