context and identifier

This commit is contained in:
harttle
2016-06-14 00:31:24 +08:00
parent 5a33b4b84a
commit d502860ca9
6 changed files with 181 additions and 2 deletions
+37
View File
@@ -0,0 +1,37 @@
var chai = require("chai");
var should = chai.should();
chai.use(require("chai-as-promised"));
var context = require('../context.js');
describe('context', function() {
var ctx;
before(function(){
ctx = context.factory({
foo: 'bar',
bar: ['a', {b: [1, 2]}]
});
});
it('should parse literal', function() {
ctx.get('2.3').should.equal(2.3);
ctx.get('(2..4)').should.deep.equal([2,3]);
ctx.get('"foo"').should.equal("foo");
});
it('should get property', function() {
ctx.get('foo').should.equal('bar');
});
it('should get desendent property', function() {
ctx.get('bar[0]').should.equal('a');
ctx.get('bar[1].b').should.deep.equal([1, 2]);
ctx.get('bar[1].b[1]').should.equal(2);
});
it('should merge context', function() {
ctx.merge({foo: 'foo', foo1: 'foo1'});
ctx.get('foo').should.equal('foo');
ctx.get('foo1').should.equal('foo1');
});
});
+76
View File
@@ -0,0 +1,76 @@
var chai = require("chai");
var should = chai.should();
chai.use(require("chai-as-promised"));
var identifier = require('../identifier.js');
describe('identifier', function() {
it('should test boolean literal', function() {
identifier.isLiteral('true').should.equal(true);
identifier.isLiteral('TrUE').should.equal(true);
identifier.isLiteral('false').should.equal(true);
});
it('should test number literal', function() {
identifier.isLiteral('2.3').should.equal(true);
identifier.isLiteral('.3').should.equal(true);
identifier.isLiteral('3.').should.equal(true);
identifier.isLiteral('23').should.equal(true);
});
it('should test string literal', function() {
identifier.isLiteral('""').should.equal(true);
identifier.isLiteral('"a\'b"').should.equal(true);
identifier.isLiteral("''").should.equal(true);
identifier.isLiteral("'a bcd'").should.equal(true);
});
it("should test range literal", function() {
identifier.isLiteral("(12..32)").should.equal(true);
});
it("should test variable", function() {
identifier.isVariable("foo").should.equal(true);
identifier.isVariable("foo.bar.foo").should.equal(true);
identifier.isVariable("foo[0].b").should.equal(true);
});
it('should test none literal', function() {
identifier.isLiteral('2a').should.equal(false);
identifier.isLiteral('"x').should.equal(false);
identifier.isLiteral('a2').should.equal(false);
});
it('should test none variable', function() {
identifier.isVariable("a.").should.equal(false);
identifier.isVariable(".b").should.equal(false);
identifier.isVariable(".").should.equal(false);
identifier.isVariable("0a").should.equal(false);
identifier.isVariable("[0][12].bar[0]").should.equal(false);
});
it('should parse boolean literal', function() {
identifier.parseLiteral('true').should.equal(true);
identifier.parseLiteral('TrUE').should.equal(true);
identifier.parseLiteral('false').should.equal(false);
});
it('should parse number literal', function() {
identifier.parseLiteral('2.3').should.equal(2.3);
identifier.parseLiteral('.32').should.equal(0.32);
identifier.parseLiteral('23.').should.equal(23);
identifier.parseLiteral('23').should.equal(23);
});
it('should parse string literal', function() {
identifier.parseLiteral('"ab\'c"').should.equal("ab\'c");
});
it("should parse range literal", function() {
var arr = identifier.parseLiteral("(12..32)");
arr.length.should.equal(20);
arr[0].should.equal(12);
arr[1].should.equal(13);
arr[19].should.equal(31);
});
});
+1 -1
View File
@@ -29,7 +29,7 @@ describe('tokenizer', function() {
tokens[1].type.should.equal('output');
tokens[1].value.should.equal('foo | date: "%Y-%m-%d"');
});
it('should handle successive output and tags', function(){
it('should handle successive outputs and tags', function(){
var html = '{{foo}}{{bar}}{%foo%}{%bar%}';
var tokens = tokenizer.parse(html);