mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 00:55:13 -07:00
coverage
This commit is contained in:
+2
-2
@@ -2,7 +2,7 @@ function TokenizationError(message, input, line) {
|
|||||||
Error.captureStackTrace(this, this.constructor);
|
Error.captureStackTrace(this, this.constructor);
|
||||||
this.name = this.constructor.name;
|
this.name = this.constructor.name;
|
||||||
|
|
||||||
this.message = message || "";
|
this.message = message;
|
||||||
this.input = input;
|
this.input = input;
|
||||||
this.line = line;
|
this.line = line;
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ function ParseError(message, input, line, e) {
|
|||||||
this.name = this.constructor.name;
|
this.name = this.constructor.name;
|
||||||
this.originalError = e;
|
this.originalError = e;
|
||||||
|
|
||||||
this.message = message || "";
|
this.message = message;
|
||||||
this.input = input;
|
this.input = input;
|
||||||
this.line = line;
|
this.line = line;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-27
@@ -15,13 +15,6 @@ var render = {
|
|||||||
// emptyPromise.then(renderTag(template0).then(renderTag(template1).then(renderTag(template2)...
|
// emptyPromise.then(renderTag(template0).then(renderTag(template1).then(renderTag(template2)...
|
||||||
var lastPromise = templates.reduce((promise, template) => {
|
var lastPromise = templates.reduce((promise, template) => {
|
||||||
return promise.then(() => {
|
return promise.then(() => {
|
||||||
if (scope.safeGet('forloop.skip')) {
|
|
||||||
return Promise.resolve('');
|
|
||||||
}
|
|
||||||
if (scope.safeGet('forloop.stop')) {
|
|
||||||
throw new Error('forloop.stop'); // this will stop/break the sequential promise chain and go to the catch
|
|
||||||
}
|
|
||||||
|
|
||||||
var promiseLink = Promise.resolve('');
|
var promiseLink = Promise.resolve('');
|
||||||
switch (template.type) {
|
switch (template.type) {
|
||||||
case 'tag':
|
case 'tag':
|
||||||
@@ -50,38 +43,19 @@ var render = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return promiseLink;
|
return promiseLink;
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
if (error.message === 'forloop.skip') {
|
|
||||||
// the error is a controlled, purposeful stop. so just return the html that we have up to this point
|
|
||||||
return html;
|
|
||||||
} else {
|
|
||||||
// rethrow actual error
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}, Promise.resolve('')); // start the reduce chain with a resolved Promise. After first run, the "promise" argument
|
}, Promise.resolve('')); // start the reduce chain with a resolved Promise. After first run, the "promise" argument
|
||||||
// in our reduce callback will be the returned promise from our "then" above. In this
|
// in our reduce callback will be the returned promise from our "then" above. In this
|
||||||
// case, that's the promise returned from this.renderTag or a resolved promise with raw html.
|
// case, that's the promise returned from this.renderTag or a resolved promise with raw html.
|
||||||
|
|
||||||
return lastPromise
|
return lastPromise;
|
||||||
.then((renderedHtml) => {
|
|
||||||
return renderedHtml;
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
renderTag: function(template, scope, register) {
|
renderTag: function(template, scope, register) {
|
||||||
if (template.name === 'continue') {
|
if (template.name === 'continue') {
|
||||||
scope.set('forloop.skip', true);
|
|
||||||
return Promise.resolve('');
|
return Promise.resolve('');
|
||||||
}
|
}
|
||||||
if (template.name === 'break') {
|
if (template.name === 'break') {
|
||||||
scope.set('forloop.stop', true);
|
|
||||||
scope.set('forloop.skip', true);
|
|
||||||
return Promise.reject(new Error('forloop.stop')); // this will stop the sequential promise chain
|
return Promise.reject(new Error('forloop.stop')); // this will stop the sequential promise chain
|
||||||
}
|
}
|
||||||
return template.render(scope, register);
|
return template.render(scope, register);
|
||||||
|
|||||||
+1
-3
@@ -42,7 +42,7 @@ var Scope = {
|
|||||||
return this.scopes.pop();
|
return this.scopes.pop();
|
||||||
},
|
},
|
||||||
unshift: function(ctx) {
|
unshift: function(ctx) {
|
||||||
if (!ctx) throw new Error('trying to push $(ctx) into scopes');
|
if (!ctx) throw new Error(`trying to push ${ctx} into scopes`);
|
||||||
return this.scopes.unshift(ctx);
|
return this.scopes.unshift(ctx);
|
||||||
},
|
},
|
||||||
shift: function() {
|
shift: function() {
|
||||||
@@ -60,9 +60,7 @@ var Scope = {
|
|||||||
// case for readonly objects
|
// case for readonly objects
|
||||||
obj = obj[key] || {};
|
obj = obj[key] || {};
|
||||||
}
|
}
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
return obj[path] = val;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getPropertyByPath: function(obj, path) {
|
getPropertyByPath: function(obj, path) {
|
||||||
|
|||||||
+4
-1
@@ -1,9 +1,12 @@
|
|||||||
const lexical = require('./lexical.js');
|
const lexical = require('./lexical.js');
|
||||||
const TokenizationError = require('./error.js').TokenizationError;
|
const TokenizationError = require('./error.js').TokenizationError;
|
||||||
|
const _ = require('./util/underscore.js');
|
||||||
|
|
||||||
function parse(html) {
|
function parse(html) {
|
||||||
var tokens = [];
|
var tokens = [];
|
||||||
if (!html) return tokens;
|
if (!_.isString(html)) {
|
||||||
|
throw new TokenizationError('illegal input type');
|
||||||
|
}
|
||||||
|
|
||||||
var syntax = /({%(.*?)%})|({{(.*?)}})/g;
|
var syntax = /({%(.*?)%})|({{(.*?)}})/g;
|
||||||
var result, htmlFragment, token;
|
var result, htmlFragment, token;
|
||||||
|
|||||||
@@ -20,6 +20,12 @@ describe('filter', function() {
|
|||||||
expect(result.error).to.be.an('Error');
|
expect(result.error).to.be.an('Error');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should throw when filter name illegal', function() {
|
||||||
|
expect(function() {
|
||||||
|
filter.construct('/');
|
||||||
|
}).to.throw(/illegal filter/);
|
||||||
|
});
|
||||||
|
|
||||||
it('should parse argument syntax', function() {
|
it('should parse argument syntax', function() {
|
||||||
filter.register('foo', x => x);
|
filter.register('foo', x => x);
|
||||||
var f = filter.construct('foo: a, "b"');
|
var f = filter.construct('foo: a, "b"');
|
||||||
|
|||||||
@@ -21,6 +21,12 @@ describe('template', function() {
|
|||||||
template = Template(tag, filter);
|
template = Template(tag, filter);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should throw when output string illegal', function() {
|
||||||
|
expect(function() {
|
||||||
|
template.parseOutput('/');
|
||||||
|
}).to.throw(/illegal output string/);
|
||||||
|
});
|
||||||
|
|
||||||
it('should parse output string', function() {
|
it('should parse output string', function() {
|
||||||
var tpl = template.parseOutput('foo');
|
var tpl = template.parseOutput('foo');
|
||||||
expect(tpl.type).to.equal('output');
|
expect(tpl.type).to.equal('output');
|
||||||
|
|||||||
@@ -28,9 +28,17 @@ describe('render', function() {
|
|||||||
render = Render();
|
render = Render();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('.renderTemplates()', function(){
|
||||||
|
it('should throw when scope undefined', function() {
|
||||||
|
expect(function(){
|
||||||
|
render.renderTemplates([]);
|
||||||
|
}).to.throw(/scope undefined/);
|
||||||
|
});
|
||||||
|
|
||||||
it('should render html', function() {
|
it('should render html', function() {
|
||||||
return render.renderTemplates([{type: 'html', value: '<p>'}], scope).should.eventually.equal('<p>');
|
return render.renderTemplates([{type: 'html', value: '<p>'}], scope).should.eventually.equal('<p>');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should eval filter with correct arguments', function() {
|
it('should eval filter with correct arguments', function() {
|
||||||
var date = sinon.stub().returns('y');
|
var date = sinon.stub().returns('y');
|
||||||
@@ -43,6 +51,12 @@ describe('render', function() {
|
|||||||
expect(time).to.have.been.calledWith('y', 2);
|
expect(time).to.have.been.calledWith('y', 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('.evalOutput()', function(){
|
||||||
|
it('should throw when scope undefined', function() {
|
||||||
|
expect(function(){
|
||||||
|
render.evalOutput();
|
||||||
|
}).to.throw(/scope undefined/);
|
||||||
|
});
|
||||||
it('should eval output', function() {
|
it('should eval output', function() {
|
||||||
filter.register('date', (l, r) => l + r);
|
filter.register('date', (l, r) => l + r);
|
||||||
filter.register('time', (l, r) => l + 3 * r);
|
filter.register('time', (l, r) => l + 3 * r);
|
||||||
@@ -50,3 +64,4 @@ describe('render', function() {
|
|||||||
expect(render.evalOutput(tpl, scope)).to.equal('ab6');
|
expect(render.evalOutput(tpl, scope)).to.equal('ab6');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|||||||
+42
-3
@@ -53,6 +53,24 @@ describe('scope', function() {
|
|||||||
expect(scope.get(false)).to.equal(undefined);
|
expect(scope.get(false)).to.equal(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should throw when [] unbalanced', function() {
|
||||||
|
expect(function() {
|
||||||
|
scope.get('foo[bar');
|
||||||
|
}).to.throw(/unbalanced \[\]/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw when "" unbalanced', function() {
|
||||||
|
expect(function() {
|
||||||
|
scope.get('foo["bar]');
|
||||||
|
}).to.throw(/unbalanced "/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw when '' unbalanced", function() {
|
||||||
|
expect(function() {
|
||||||
|
scope.get("foo['bar]");
|
||||||
|
}).to.throw(/unbalanced '/);
|
||||||
|
});
|
||||||
|
|
||||||
it('should throw undefined in strict mode', function() {
|
it('should throw undefined in strict mode', function() {
|
||||||
scope = Scope.factory(ctx, {
|
scope = Scope.factory(ctx, {
|
||||||
strict: true
|
strict: true
|
||||||
@@ -87,7 +105,9 @@ describe('scope', function() {
|
|||||||
|
|
||||||
it('should support nested case', function() {
|
it('should support nested case', function() {
|
||||||
scope.set('posts', {
|
scope.set('posts', {
|
||||||
"first": {"name": "A Nice Day"}
|
"first": {
|
||||||
|
"name": "A Nice Day"
|
||||||
|
}
|
||||||
});
|
});
|
||||||
scope.set('category', {
|
scope.set('category', {
|
||||||
"diary": ["first"]
|
"diary": ["first"]
|
||||||
@@ -97,6 +117,11 @@ describe('scope', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('.push(), .pop()', function() {
|
describe('.push(), .pop()', function() {
|
||||||
|
it('should throw when trying to push non-object', function() {
|
||||||
|
expect(function() {
|
||||||
|
scope.push(false);
|
||||||
|
}).to.throw();
|
||||||
|
});
|
||||||
it('should push scope', function() {
|
it('should push scope', function() {
|
||||||
scope.set('bar', 'bar');
|
scope.set('bar', 'bar');
|
||||||
scope.push({
|
scope.push({
|
||||||
@@ -113,14 +138,28 @@ describe('scope', function() {
|
|||||||
scope.pop();
|
scope.pop();
|
||||||
expect(scope.get('foo')).to.equal('zoo');
|
expect(scope.get('foo')).to.equal('zoo');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('.push(), .pop()', function() {
|
||||||
|
it('should throw when trying to unshift non-object', function() {
|
||||||
|
expect(function() {
|
||||||
|
scope.unshift(false);
|
||||||
|
}).to.throw();
|
||||||
|
});
|
||||||
it('should unshift scope', function() {
|
it('should unshift scope', function() {
|
||||||
scope.unshift({foo: 'blue', foo1: 'foo1'})
|
scope.unshift({
|
||||||
|
foo: 'blue',
|
||||||
|
foo1: 'foo1'
|
||||||
|
})
|
||||||
scope.get('foo').should.equal('zoo');
|
scope.get('foo').should.equal('zoo');
|
||||||
scope.get('foo1').should.equal('foo1');
|
scope.get('foo1').should.equal('foo1');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should shift scope', function() {
|
it('should shift scope', function() {
|
||||||
scope.unshift({foo: 'blue', foo1: 'foo1'});
|
scope.unshift({
|
||||||
|
foo: 'blue',
|
||||||
|
foo1: 'foo1'
|
||||||
|
});
|
||||||
scope.shift();
|
scope.shift();
|
||||||
expect(scope.get('foo')).to.equal('zoo');
|
expect(scope.get('foo')).to.equal('zoo');
|
||||||
expect(scope.get('foo1')).to.equal(undefined);
|
expect(scope.get('foo1')).to.equal(undefined);
|
||||||
|
|||||||
@@ -34,6 +34,14 @@ describe('expression', function() {
|
|||||||
expect(evalValue('x', scope)).to.equal('XXX');
|
expect(evalValue('x', scope)).to.equal('XXX');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('.evalExp()', function() {
|
||||||
|
|
||||||
|
it('should throw when scope undefined', function() {
|
||||||
|
expect(function() {
|
||||||
|
evalExp('');
|
||||||
|
}).to.throw(/scope undefined/);
|
||||||
|
});
|
||||||
|
|
||||||
it('should eval simple expression', function() {
|
it('should eval simple expression', function() {
|
||||||
expect(evalExp('1<2', scope)).to.equal(true);
|
expect(evalExp('1<2', scope)).to.equal(true);
|
||||||
expect(evalExp('2<=2', scope)).to.equal(true);
|
expect(evalExp('2<=2', scope)).to.equal(true);
|
||||||
@@ -46,6 +54,7 @@ describe('expression', function() {
|
|||||||
it('should eval complex expression', function() {
|
it('should eval complex expression', function() {
|
||||||
expect(evalExp('1<2 and x contains "x"', scope)).to.equal(false);
|
expect(evalExp('1<2 and x contains "x"', scope)).to.equal(false);
|
||||||
expect(evalExp('1<2 or x contains "x"', scope)).to.equal(true);
|
expect(evalExp('1<2 or x contains "x"', scope)).to.equal(true);
|
||||||
|
expect(evalExp('false or true', scope)).to.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should eval range expression", function() {
|
it("should eval range expression", function() {
|
||||||
@@ -53,3 +62,4 @@ describe('expression', function() {
|
|||||||
expect(evalExp('(two..4)', scope)).to.deep.equal([2, 3, 4]);
|
expect(evalExp('(two..4)', scope)).to.deep.equal([2, 3, 4]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -5,6 +5,12 @@ chai.use(require("chai-as-promised"));
|
|||||||
|
|
||||||
describe('tags/assign', function() {
|
describe('tags/assign', function() {
|
||||||
var liquid = Liquid();
|
var liquid = Liquid();
|
||||||
|
it('should throw when variable expression illegal', function() {
|
||||||
|
var src = '{% assign / %}';
|
||||||
|
var ctx = {};
|
||||||
|
return expect(liquid.parseAndRender(src, ctx)).to.be.rejectedWith(/illegal/);
|
||||||
|
});
|
||||||
|
|
||||||
it('should assign as string', function() {
|
it('should assign as string', function() {
|
||||||
var src = '{% assign foo="bar" %}{{foo}}';
|
var src = '{% assign foo="bar" %}{{foo}}';
|
||||||
return expect(liquid.parseAndRender(src))
|
return expect(liquid.parseAndRender(src))
|
||||||
|
|||||||
@@ -6,6 +6,12 @@ chai.use(require("chai-as-promised"));
|
|||||||
describe('tags/decrement', function() {
|
describe('tags/decrement', function() {
|
||||||
var liquid = Liquid();
|
var liquid = Liquid();
|
||||||
|
|
||||||
|
it('should throw when variable expression illegal', function() {
|
||||||
|
var src = '{% decrement / %}{{one}}';
|
||||||
|
var ctx = {};
|
||||||
|
return expect(liquid.parseAndRender(src, ctx)).to.be.rejectedWith(/illegal/);
|
||||||
|
});
|
||||||
|
|
||||||
it('should support decrement', function() {
|
it('should support decrement', function() {
|
||||||
var src = '{% decrement one %}{{one}}';
|
var src = '{% decrement one %}{{one}}';
|
||||||
var ctx = {
|
var ctx = {
|
||||||
|
|||||||
@@ -13,6 +13,11 @@ describe('tokenizer', function() {
|
|||||||
tokens[0].value.should.equal(html);
|
tokens[0].value.should.equal(html);
|
||||||
tokens[0].type.should.equal('html');
|
tokens[0].type.should.equal('html');
|
||||||
});
|
});
|
||||||
|
it('should throw when non-string passed in', function() {
|
||||||
|
expect(function() {
|
||||||
|
tokenizer.parse({});
|
||||||
|
}).to.throw('illegal input type');
|
||||||
|
});
|
||||||
it('should handle tag syntax', function() {
|
it('should handle tag syntax', function() {
|
||||||
var html = '<p>{% for p in a[1]%}</p>';
|
var html = '<p>{% for p in a[1]%}</p>';
|
||||||
var tokens = tokenizer.parse(html);
|
var tokens = tokenizer.parse(html);
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ describe('util/strftime', function() {
|
|||||||
before(function() {
|
before(function() {
|
||||||
mockUTC();
|
mockUTC();
|
||||||
now = new Date('2016-01-04T13:15:23');
|
now = new Date('2016-01-04T13:15:23');
|
||||||
then = new Date('2016-01-03T03:05:03');
|
then = new Date('2016-03-06T03:05:03');
|
||||||
});
|
});
|
||||||
after(function() {
|
after(function() {
|
||||||
restoreUTC();
|
restoreUTC();
|
||||||
@@ -36,7 +36,7 @@ describe('util/strftime', function() {
|
|||||||
expect(t(now, '%I')).to.equal('01');
|
expect(t(now, '%I')).to.equal('01');
|
||||||
});
|
});
|
||||||
it('should format %j as day of year', function() {
|
it('should format %j as day of year', function() {
|
||||||
expect(t(now, '%j')).to.equal('004');
|
expect(t(then, '%j')).to.equal('066');
|
||||||
});
|
});
|
||||||
it('should format %k as space padded hour', function() {
|
it('should format %k as space padded hour', function() {
|
||||||
expect(t(then, '%k')).to.equal(' 3');
|
expect(t(then, '%k')).to.equal(' 3');
|
||||||
@@ -56,8 +56,13 @@ describe('util/strftime', function() {
|
|||||||
expect(t(then, '%P')).to.equal('am');
|
expect(t(then, '%P')).to.equal('am');
|
||||||
});
|
});
|
||||||
it('should format %q as date suffix', function(){
|
it('should format %q as date suffix', function(){
|
||||||
|
var st = new Date('2016-03-01T03:05:03');
|
||||||
|
var nd = new Date('2016-03-02T03:05:03');
|
||||||
|
var rd = new Date('2016-03-03T03:05:03');
|
||||||
|
expect(t(st, '%q')).to.equal('st');
|
||||||
|
expect(t(nd, '%q')).to.equal('nd');
|
||||||
|
expect(t(rd, '%q')).to.equal('rd');
|
||||||
expect(t(now, '%q')).to.equal('th');
|
expect(t(now, '%q')).to.equal('th');
|
||||||
expect(t(then, '%q')).to.equal('rd');
|
|
||||||
});
|
});
|
||||||
it('should format %s as UNIX seconds', function(){
|
it('should format %s as UNIX seconds', function(){
|
||||||
expect(t(now, '%s')).to.be.match(/\d+/);
|
expect(t(now, '%s')).to.be.match(/\d+/);
|
||||||
|
|||||||
@@ -36,5 +36,13 @@ describe('util/underscore', function() {
|
|||||||
expect(spy).to.have.been.calledOnce;
|
expect(spy).to.have.been.calledOnce;
|
||||||
expect(spy).to.have.been.calledWith('bar', 'foo', obj);
|
expect(spy).to.have.been.calledWith('bar', 'foo', obj);
|
||||||
});
|
});
|
||||||
|
it('should break when returned false', function() {
|
||||||
|
var spy = sinon.stub().returns(false);
|
||||||
|
_.forOwn({
|
||||||
|
'foo': 'foo',
|
||||||
|
'bar': 'foo'
|
||||||
|
}, spy);
|
||||||
|
expect(spy).to.have.been.calledOnce;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user