strict_filters/strict_variables when creating engine

This commit is contained in:
harttle
2016-11-07 00:01:02 +08:00
parent 1e7873afc0
commit ddd06e12b5
8 changed files with 203 additions and 90 deletions
+4 -5
View File
@@ -1,5 +1,4 @@
const chai = require("chai");
const should = chai.should();
const expect = chai.expect;
const Liquid = require('..');
const mock = require('mock-fs');
@@ -152,10 +151,10 @@ describe('liquid', function() {
it('should be disabled by default', function() {
return engine.renderFile('files/foo')
.then(x => expect(x).to.equal('foo'))
.then(x => mock({
.then(() => mock({
'/root/files/foo.html': 'bar'
}))
.then(x => engine.renderFile('files/foo'))
.then(() => engine.renderFile('files/foo'))
.then(x => expect(x).to.equal('bar'));
});
it('should respect cache=true option', function() {
@@ -166,10 +165,10 @@ describe('liquid', function() {
});
return engine.renderFile('files/foo')
.then(x => expect(x).to.equal('foo'))
.then(x => mock({
.then(() => mock({
'/root/files/foo.html': 'bar'
}))
.then(x => engine.renderFile('files/foo'))
.then(() => engine.renderFile('files/foo'))
.then(x => expect(x).to.equal('foo'));
});
});
+86 -48
View File
@@ -1,65 +1,103 @@
const chai = require("chai");
const expect = chai.expect;
const mock = require('mock-fs');
chai.use(require("chai-as-promised"));
var engine = require('../..')(), ctx;
function test(promise, cb){
return promise
.then((result) => {
return cb({});
})
.catch((error) => {
return cb(error);
});
}
var engine = require('../..')();
var strictEngine = require('../..')({
strict_variables: true,
strict_filters: true
});
describe('error', function() {
it('should throw TokenizationError when tag illegal', function() {
return test(engine.parseAndRender('{% . a %}', {}), function(err){
expect(err.name).to.equal('TokenizationError');
expect(err.message).to.equal('illegal tag: {% . a %}');
expect(err.input).to.equal('{% . a %}');
expect(err.line).to.equal(1);
describe('TokenizationError', function() {
it('should throw TokenizationError when tag illegal', function() {
return engine.parseAndRender('{% . a %}', {}).catch(function(err) {
expect(err.name).to.equal('TokenizationError');
expect(err.message).to.equal('illegal tag: {% . a %}');
expect(err.input).to.equal('{% . a %}');
expect(err.line).to.equal(1);
});
});
it('should throw TokenizationError when tag syntax illegal', function() {
return engine.parseAndRender('{% . a }', {}).catch(function(err) {
expect(err.name).to.equal('TokenizationError');
expect(err.message).to.equal('illegal tag: {% . a }');
expect(err.input).to.equal('{% . a }');
expect(err.line).to.equal(1);
});
});
it('should throw TokenizationError when filter syntax illegal', function() {
return engine.parseAndRender('{{ a|| }}', {}).catch(function(err) {
expect(err.name).to.equal('TokenizationError');
expect(err.message).to.equal('{{ a|| }');
expect(err.input).to.equal('{{ a|| }');
expect(err.line).to.equal(1);
});
});
});
it('should throw correct error info', function() {
return test(engine.parseAndRender('{%if true%}\naaa{%endif%}\n{% -a %}\n3', {}), function(err){
expect(err.input).to.equal('{% -a %}');
expect(err.line).to.equal(3);
describe('TypeError', function() {
it('should not throw when variable undefined by default', function() {
return expect(engine.parseAndRender('X{{a}}Y')).to.eventually.equal('XY');
});
it('should throw TypeError when variable not defined', function() {
return expect(strictEngine.parseAndRender('{{a}}')).to.eventually
.be.rejected
.then(function(e){
expect(e).to.have.property('name', 'TypeError');
expect(e).to.have.property('message', 'undefined variable: a');
});
});
it('should throw TypeError when filter not defined', function() {
return expect(strictEngine.parseAndRender('{{1 | a}}')).to.eventually
.be.rejected
.then(function(e){
expect(e).to.have.property('name', 'TypeError');
expect(e).to.have.property('message', 'undefined filter: a');
});
});
});
it('should throw correct error info for files', function() {
mock({
"/foo.html": '<html>\n<head>\n\n{% raw %}\n\n'
describe('ParseError', function() {
it('should throw correct error info', function() {
var src = '{%if true%}\naaa{%endif%}\n{% -a %}\n3';
return engine.parseAndRender(src).catch(function(err) {
expect(err.name).to.equal('ParseError');
expect(err.input).to.equal('{% -a %}');
expect(err.line).to.equal(3);
});
});
return test(engine.renderFile('/foo.html', {}), function(err){
mock.restore();
expect(err.input).to.equal('{% raw %}');
expect(err.line).to.equal(4);
expect(err.file).to.equal('/foo.html');
it('should throw correct error info for files', function() {
mock({
"/foo.html": '<html>\n<head>\n\n{% raw %}\n\n'
});
return engine.renderFile('/foo.html').catch(function(err) {
mock.restore();
expect(err.name).to.equal('ParseError');
expect(err.input).to.equal('{% raw %}');
expect(err.line).to.equal(4);
expect(err.file).to.equal('/foo.html');
});
});
it('should throw ParseError when tag not exist', function() {
return engine.parseAndRender('{% a %}').catch(function(err) {
expect(err.name).to.equal('ParseError');
expect(err.message).to.equal('tag a not found');
expect(err.input).to.equal('{% a %}');
expect(err.line).to.equal(1);
});
});
it('should throw ParseError when tag not closed', function() {
return engine.parseAndRender('{% if %}').catch(function(err) {
expect(err.name).to.equal('ParseError');
expect(err.message).to.equal('tag {% if %} not closed');
expect(err.input).to.equal('{% if %}');
expect(err.line).to.equal(1);
});
});
});
it('should throw ParseError when tag not exist', function() {
return test(engine.parseAndRender('{% a %}', {}), function(err){
expect(err.name).to.equal('ParseError');
expect(err.message).to.equal('tag a not found');
expect(err.input).to.equal('{% a %}');
expect(err.line).to.equal(1);
});
});
it('should throw ParseError when tag not closed', function() {
return test(engine.parseAndRender('{% if %}', {}), function(err){
expect(err.name).to.equal('ParseError');
expect(err.message).to.equal('tag {% if %} not closed');
expect(err.input).to.equal('{% if %}');
expect(err.line).to.equal(1);
});
});
});
+47 -2
View File
@@ -11,7 +11,7 @@ describe('util/underscore', function() {
expect(_.isString('foo')).to.be.true;
});
it('should return true String instance', function() {
expect(_.isString(new String('foo'))).to.be.true;
expect(_.isString(String('foo'))).to.be.true;
});
it('should return false for 123 ', function() {
expect(_.isString(123)).to.be.false;
@@ -53,7 +53,7 @@ describe('util/underscore', function() {
expect(_.isArray("foo")).to.be.false;
});
});
describe('.echo()', function(){
describe('.echo()', function() {
it('should be transparent', function() {
expect(_.echo('foo')('bar')).to.equal('bar');
});
@@ -63,4 +63,49 @@ describe('util/underscore', function() {
expect(log).to.have.been.calledWith('[foo]', 'bar');
});
});
describe('.assign()', function() {
it('should handle null dst', function() {
expect(_.assign(null, {
foo: 'bar'
})).to.deep.equal({
foo: 'bar'
});
});
it('should assign 2 objects', function() {
var src = {
foo: 'foo',
bar: 'bar'
};
var dst = {
foo: 'bar',
kaa: 'kaa'
};
expect(_.assign(dst, src)).to.deep.equal({
foo: 'foo',
bar: 'bar',
kaa: 'kaa'
});
});
it('should assign 3 objects', function() {
expect(_.assign({
foo: 'foo'
}, {
bar: 'bar'
}, {
car: 'car'
})).to.deep.equal({
foo: 'foo',
bar: 'bar',
car: 'car'
});
});
});
describe('.uniq()', function() {
it('should handle empty array', function() {
expect(_.uniq([])).to.deep.equal([]);
});
it('should do uniq', function() {
expect(_.uniq([1, 'a', 'a', 1])).to.deep.equal([1, 'a']);
});
});
});