tag:include

This commit is contained in:
harttle
2016-06-24 02:44:20 +08:00
parent 21bde3cb6d
commit 8fbbec089f
24 changed files with 226 additions and 52 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
const chai = require("chai");
const expect = chai.expect;
var expression = require('../expression.js');
var Scope = require('../scope.js');
var expression = require('../src/expression.js');
var Scope = require('../src/scope.js');
var evalExp = expression.evalExp;
var evalValue = expression.evalValue;
+2 -2
View File
@@ -5,8 +5,8 @@ const expect = chai.expect;
chai.use(sinonChai);
var filter = require('../filter.js')();
var Scope = require('../scope.js');
var filter = require('../src/filter.js')();
var Scope = require('../src/scope.js');
describe('filter', function() {
var scope;
+1 -1
View File
@@ -2,7 +2,7 @@ var chai = require("chai");
var should = chai.should();
var expect = chai.expect;
var lexical = require('../lexical.js');
var lexical = require('../src/lexical.js');
describe('lexical', function() {
it('should test filter syntax', function(){
+48 -9
View File
@@ -2,24 +2,29 @@ const chai = require("chai");
const expect = chai.expect;
const should = chai.should;
const Liquid = require('..');
const mock = require('mock-fs');
describe('liquid', function() {
var engine, ctx;
beforeEach(function() {
engine = Liquid();
ctx = {
date: new Date(),
foo: 'bar',
name: 'harttle',
arr: [-2, 'a'],
obj: {
foo: 'bar'
},
posts: [{
category: 'foo'
}, {
category: 'bar'
}]
}
};
engine = Liquid({
root: '/root/',
extname: '.html'
});
mock({
'/root/files/foo.html': 'foo',
'/root/files/name.html': 'My name is {{name}}.'
});
});
afterEach(function(){
mock.restore();
});
it('should output object', function() {
engine.parseAndRender('{{obj}}', ctx).should.equal('{"foo":"bar"}');
@@ -44,4 +49,38 @@ describe('liquid', function() {
var template = engine.parse('<p>{{arr | join: "_"}}</p>');
engine.render(template, ctx).should.equal('<p>-2_a</p>');
});
it('should render file', function(){
engine.renderFile('/root/files/foo.html', ctx).should.equal('foo');
});
it('should render file relative to root', function(){
engine.renderFile('files/foo.html', ctx).should.equal('foo');
});
it('should render file with context', function(){
engine.renderFile('/root/files/name.html', ctx).should.equal('My name is harttle.');
});
it('should render file with default extname', function() {
engine.renderFile('files/name', ctx).should.equal('My name is harttle.');
});
it('should disable cache by default', function(){
mock({
'/root/files/foo.html': 'bar'
});
engine.renderFile('files/foo', ctx).should.equal('bar');
});
it('should respect cache option', function(){
mock.restore();
engine = Liquid({
root: '/root/',
extname: '.html',
cache: true
});
mock({
'/root/files/foo.html': 'foo'
});
engine.renderFile('files/foo', ctx).should.equal('foo');
mock({
'/root/files/foo.html': 'bar'
});
engine.renderFile('files/foo', ctx).should.equal('foo');
});
});
+3 -3
View File
@@ -6,9 +6,9 @@ const expect = chai.expect;
chai.use(sinonChai);
var filter = require('../filter.js')();
var tag = require('../tag.js')();
var Template = require('../parser.js');
var filter = require('../src/filter.js')();
var tag = require('../src/tag.js')();
var Template = require('../src/parser.js');
describe('template', function() {
var scope, template, add = (l, r) => l + r;
+5 -5
View File
@@ -5,11 +5,11 @@ const expect = chai.expect;
chai.use(sinonChai);
var tag = require('../tag.js')();
var Scope = require('../scope.js');
var filter = require('../filter')();
var Render = require('../render.js');
var Template = require('../parser.js')(tag, filter);
var tag = require('../src/tag.js')();
var Scope = require('../src/scope.js');
var filter = require('../src/filter')();
var Render = require('../src/render.js');
var Template = require('../src/parser.js')(tag, filter);
describe('render', function() {
var scope, render;
+1 -1
View File
@@ -2,7 +2,7 @@ var chai = require("chai");
var should = chai.should();
var expect = chai.expect;
var Scope = require('../scope.js');
var Scope = require('../src/scope.js');
describe('scope', function() {
var scope;
+2 -2
View File
@@ -4,8 +4,8 @@ var sinon = require("sinon");
var expect = chai.expect;
chai.use(sinonChai);
var tag = require('../tag.js')();
var Scope = require('../scope.js');
var tag = require('../src/tag.js')();
var Scope = require('../src/scope.js');
describe('tag', function() {
var scope;
+43 -2
View File
@@ -1,7 +1,11 @@
const chai = require("chai");
const expect = chai.expect;
var liquid = require('..')(),
const Liquid = require('..');
const mock = require('mock-fs');
var liquid = Liquid({
root: '/',
extname: '.html'
}),
ctx, src, dst;
function test(src, dst) {
@@ -26,6 +30,20 @@ describe('tags', function() {
alpha: ['a', 'b', 'c'],
emptyArray: []
};
mock({
'/files/foo.html': 'foo',
'/current.html': 'bar{% include "foo.html" %}bar',
'/relative.html': 'bar{% include "../files/foo.html" %}bar',
'/foo.html': 'FOO',
'/user.html': '{{name}} : {{role}} : {{alias}}',
'/color.html': 'color:{{color}}, shape:{{shape}}',
'/with.html': '{% include "color" with "red", shape: "rect" %}',
'/scope.html': '{% assign shape="triangle" %}{% assign color="yellow" %}{% include "color.html" %}',
'/hash.html': '{% assign name="harttle" %}{% include "user.html", role: "admin", alias: name %}'
});
});
afterEach(function() {
mock.restore();
});
it('should support assign', function() {
@@ -217,4 +235,27 @@ describe('tags', function() {
'</table>';
test(src, dst);
});
it('should support include', function() {
expect(liquid.renderFile('/current.html', ctx)).to.equal('barFOObar');
});
it('should support include with relative path', function() {
expect(liquid.renderFile('relative.html', ctx)).to.equal('barfoobar');
});
it('should support include: hash list', function() {
expect(liquid.renderFile('hash.html', ctx)).to.equal('harttle : admin : harttle');
});
it('should support include: parent scope', function() {
expect(liquid.renderFile('scope.html', ctx)).to.equal('color:yellow, shape:triangle');
});
it('should support include: with', function() {
var filepath = 'with.html';
var dst = 'color:red, shape:rect';
expect(liquid.renderFile(filepath, ctx)).to.equal(dst);
});
});
+1 -1
View File
@@ -2,7 +2,7 @@ var chai = require("chai");
var should = chai.should();
var expect = chai.expect;
var tokenizer = require('../tokenizer.js');
var tokenizer = require('../src/tokenizer.js');
describe('tokenizer', function() {
it('should handle plain HTML', function() {