enable cache: parse and render

This commit is contained in:
harttle
2016-06-20 00:34:54 +08:00
parent aa1df95714
commit 54bc206d36
16 changed files with 182 additions and 118 deletions
+6 -6
View File
@@ -4,7 +4,7 @@ var sinon = require("sinon");
var expect = chai.expect;
chai.use(sinonChai);
var liquid = require('..')(), ctx;
var engine = require('..')(), ctx;
function test(func, cb){
try{
@@ -20,7 +20,7 @@ describe('error', function() {
it('should throw TokenizationError when tag illegal', function() {
test(function(){
liquid.render('{% -a %}', {});
engine.parseAndRender('{% -a %}', {});
}, function(err){
expect(err.name).to.equal('TokenizationError');
expect(err.message).to.equal('illegal tag: {% -a %}');
@@ -31,7 +31,7 @@ describe('error', function() {
it('should throw correct error info', function() {
test(function(){
liquid.render('{%if true%}\naaa{%endif%}\n{% -a %}\n3', {});
engine.parseAndRender('{%if true%}\naaa{%endif%}\n{% -a %}\n3', {});
}, function(err){
expect(err.input).to.equal('{% -a %}');
expect(err.line).to.equal(3);
@@ -40,7 +40,7 @@ describe('error', function() {
it('should throw ParseError when filter not exist', function() {
test(function(){
liquid.render('{{ a | xz }}', {});
engine.parseAndRender('{{ a | xz }}', {});
}, function(err){
expect(err.name).to.equal('ParseError');
expect(err.message).to.equal('filter "xz" not found');
@@ -50,7 +50,7 @@ describe('error', function() {
});
it('should throw ParseError when tag not exist', function() {
test(function(){
liquid.render('{% a %}', {});
engine.parseAndRender('{% a %}', {});
}, function(err){
expect(err.name).to.equal('ParseError');
expect(err.message).to.equal('tag a not found');
@@ -61,7 +61,7 @@ describe('error', function() {
it('should throw ParseError when tag not closed', function() {
test(function(){
liquid.render('{% if %}', {});
engine.parseAndRender('{% if %}', {});
}, function(err){
expect(err.name).to.equal('ParseError');
expect(err.message).to.equal('tag {% if %} not closed');
+1 -1
View File
@@ -18,7 +18,7 @@ function test(src, dst) {
category: 'bar'
}]
};
expect(liquid.render(src, ctx)).to.equal(dst);
expect(liquid.parseAndRender(src, ctx)).to.equal(dst);
}
describe('filters', function() {
+38 -22
View File
@@ -1,31 +1,47 @@
const chai = require("chai");
const expect = chai.expect;
var liquid = require('..')(),
ctx;
function test(src, dst) {
ctx = {
date: new Date(),
foo: 'bar',
arr: [-2, 'a'],
obj: {
foo: 'bar'
},
posts: [{
category: 'foo'
}, {
category: 'bar'
}]
};
expect(liquid.render(src, ctx)).to.equal(dst);
}
const should = chai.should;
const Liquid = require('..');
describe('liquid', function() {
var engine, ctx;
beforeEach(function() {
engine = Liquid();
ctx = {
date: new Date(),
foo: 'bar',
arr: [-2, 'a'],
obj: {
foo: 'bar'
},
posts: [{
category: 'foo'
}, {
category: 'bar'
}]
};
});
it('should output object', function() {
test('{{obj}}', '{"foo":"bar"}');
engine.parseAndRender('{{obj}}', ctx).should.equal('{"foo":"bar"}');
});
it('should output array', function() {
test('{{arr}}', '[-2,"a"]');
engine.parseAndRender('{{arr}}', ctx).should.equal('[-2,"a"]');
});
it('should parse html', function() {
(function(){
engine.parse('{{obj}}');
}).should.not.throw();
(function(){
engine.parse('<html><head>{{obj}}</head></html>');
}).should.not.throw();
});
it('should render template multiple times', function() {
var template = engine.parse('{{obj}}');
engine.render(template, ctx).should.equal('{"foo":"bar"}');
engine.render(template, ctx).should.equal('{"foo":"bar"}');
});
it('should render filters', function() {
var template = engine.parse('<p>{{arr | join: "_"}}</p>');
engine.render(template, ctx).should.equal('<p>-2_a</p>');
});
});
+1 -1
View File
@@ -8,7 +8,7 @@ chai.use(sinonChai);
var filter = require('../filter.js')();
var tag = require('../tag.js')();
var Template = require('../template.js');
var Template = require('../parser.js');
describe('template', function() {
var scope, template, add = (l, r) => l + r;
+2 -12
View File
@@ -9,13 +9,12 @@ var tag = require('../tag.js')();
var Scope = require('../scope.js');
var filter = require('../filter')();
var Render = require('../render.js');
var Template = require('../template.js')(tag, filter);
var Template = require('../parser.js')(tag, filter);
describe('render', function() {
var scope, render;
beforeEach(function() {
render = Render(filter, tag);
scope = Scope.factory({
foo: {
bar: ['a', 2]
@@ -23,16 +22,7 @@ describe('render', function() {
});
filter.clear();
tag.clear();
});
it('should stringify object', function() {
expect(Render.stringify({
foo: 'bar'
})).to.equal('{"foo":"bar"}');
});
it('should stringify object', function() {
expect(Render.stringify([1, 2, 3])).to.equal('[1,2,3]');
render = Render();
});
it('should render html', function() {
+2 -2
View File
@@ -5,12 +5,12 @@ var liquid = require('..')(),
ctx, src, dst;
function test(src, dst) {
expect(liquid.render(src, ctx)).to.equal(dst);
expect(liquid.parseAndRender(src, ctx)).to.equal(dst);
}
function testThrow(src, pattern) {
expect(function() {
liquid.render(src, ctx);
liquid.parseAndRender(src, ctx);
}).to.throw(pattern);
}