Making good progress on async

This commit is contained in:
Tim Hardy
2016-09-25 00:13:29 -05:00
parent 87e284159d
commit bb8468708a
8 changed files with 255 additions and 233 deletions
+50 -30
View File
@@ -1,8 +1,10 @@
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const should = chai.should();
const expect = chai.expect;
const should = chai.should;
const Liquid = require('..');
const mock = require('mock-fs');
chai.use(chaiAsPromised);
describe('liquid', function() {
var engine, ctx;
@@ -27,13 +29,13 @@ describe('liquid', function() {
mock.restore();
});
it('should output object', function() {
engine.parseAndRender('{{obj}}', ctx).should.equal('{"foo":"bar"}');
return engine.parseAndRender('{{obj}}', ctx).should.eventually.equal('{"foo":"bar"}');
});
it('should output array', function() {
engine.parseAndRender('{{arr}}', ctx).should.equal('[-2,"a"]');
return engine.parseAndRender('{{arr}}', ctx).should.eventually.equal('[-2,"a"]');
});
it('should output undefined to empty', function() {
engine.parseAndRender('foo{{zzz}}bar', ctx).should.equal('foobar');
return engine.parseAndRender('foo{{zzz}}bar', ctx).should.eventually.equal('foobar');
});
it('should parse html', function() {
(function() {
@@ -45,46 +47,55 @@ describe('liquid', function() {
});
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"}');
return engine.render(template, ctx)
.then((result) => {
expect(result).to.equal('{"foo":"bar"}');
return engine.render(template, ctx);
})
.then((result) => {
return expect(result).to.equal('{"foo":"bar"}');
});
// 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>');
return engine.render(template, ctx).should.eventually.equal('<p>-2_a</p>');
});
describe('#renderFile()', function(){
it('should render file', function() {
engine.renderFile('/root/files/foo.html', ctx).should.equal('foo');
return engine.renderFile('/root/files/foo.html', ctx).should.eventually.equal('foo');
});
it('should render file relative to root', function() {
engine.renderFile('files/foo.html', ctx).should.equal('foo');
return engine.renderFile('files/foo.html', ctx).should.eventually.equal('foo');
});
it('should render file with context', function() {
engine.renderFile('/root/files/name.html', ctx).should.equal('My name is harttle.');
return engine.renderFile('/root/files/name.html', ctx).should.eventually.equal('My name is harttle.');
});
it('should render file with default extname', function() {
engine.renderFile('files/name', ctx).should.equal('My name is harttle.');
});
});
describe('#express()', function() {
it('should render templates', function() {
engine.express()('/root/files/name.html', ctx, function(err, html) {
expect(err).to.equal(null);
expect(html).to.equal('My name is harttle.');
});
});
it('should pass error when file not found', function() {
engine.express()('/root/files/name1.html', ctx, function(err, html) {
expect(err.code).to.equal('ENOENT');
});
return engine.renderFile('files/name', ctx).should.eventually.equal('My name is harttle.');
});
});
// todo: make these async
// describe('#express()', function() {
// it('should render templates', function() {
// engine.express()('/root/files/name.html', ctx, function(err, html) {
// expect(err).to.equal(null);
// expect(html).to.equal('My name is harttle.');
// });
// });
// it('should pass error when file not found', function() {
// engine.express()('/root/files/name1.html', ctx, function(err, html) {
// expect(err.code).to.equal('ENOENT');
// });
// });
// });
describe('cache', function() {
it('should be disabled by default', function() {
mock({
'/root/files/foo.html': 'bar'
});
engine.renderFile('files/foo', ctx).should.equal('bar');
return engine.renderFile('files/foo', ctx).should.eventually.equal('bar');
});
it('should respect cache=true option', function() {
engine = Liquid({
@@ -92,11 +103,20 @@ describe('liquid', function() {
extname: '.html',
cache: true
});
engine.renderFile('files/foo', ctx).should.equal('foo');
mock({
'/root/files/foo.html': 'bar'
});
engine.renderFile('files/foo', ctx).should.equal('foo');
return engine.renderFile('files/foo', ctx)
.then((result) => {
return expect(result).to.equal('foo');
})
.then((result) => {
mock({
'/root/files/foo.html': 'bar'
});
return engine.renderFile('files/foo', ctx);
})
.then((result) => {
return expect(result).to.equal('foo');
});
});
});
});
+92 -101
View File
@@ -1,11 +1,14 @@
// temporary
var Promise = require('any-promise');
const Promise = require('any-promise');
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const should = chai.should();
const expect = chai.expect;
const Liquid = require('..');
const mock = require('mock-fs');
chai.use(require("chai-as-promised"));
chai.use(chaiAsPromised);
var liquid = Liquid({
root: '/',
extname: '.html'
@@ -13,17 +16,14 @@ var liquid = Liquid({
ctx, src, dst;
function test(src, dst) {
liquid.parseAndRender(src, ctx)
return liquid.parseAndRender(src, ctx)
.then((result) => {
expect(result.to.equal(dst));
return expect(result).to.equal(dst);
});
//expect(liquid.parseAndRender(src, ctx)).to.equal(dst);
}
function testThrow(src, pattern) {
expect(function() {
liquid.parseAndRender(src, ctx);
}).to.throw(pattern);
return liquid.parseAndRender(src, ctx).should.eventually.be.rejectedWith(pattern);
}
describe('tags', function() {
@@ -67,76 +67,63 @@ describe('tags', function() {
mock.restore();
});
it('should support assign', function() {
test('{% assign foo="bar" %}{{foo}}', 'bar');
test('{% assign foo=(1..3) %}{{foo}}', '[1,2,3]');
test('{% assign foo="a b" | capitalize | split: " " | first %}{{foo}}', 'A');
});
it('should support assign 1', function() { return test('{% assign foo="bar" %}{{foo}}', 'bar'); });
it('should support assign 2', function() { return test('{% assign foo=(1..3) %}{{foo}}', '[1,2,3]'); });
it('should support assign 3', function() { return test('{% assign foo="a b" | capitalize | split: " " | first %}{{foo}}', 'A'); });
it('should support raw', function() {
testThrow('{% raw%}', /{% raw%} not closed/);
test('{% raw %}{{ 5 | plus: 6 }}{% endraw %} is equal to 11.', '{{ 5 | plus: 6 }} is equal to 11.');
test('{% raw %}\n{{ foo}} \n{% endraw %}', '\n{{ foo}} \n');
});
it('should support raw 1', function() { return testThrow('{% raw%}', /{% raw%} not closed/); });
it('should support raw 2', function() { return test('{% raw %}{{ 5 | plus: 6 }}{% endraw %} is equal to 11.', '{{ 5 | plus: 6 }} is equal to 11.'); });
it('should support raw 3', function() { return test('{% raw %}\n{{ foo}} \n{% endraw %}', '\n{{ foo}} \n'); });
it('should support comment', function() {
testThrow('{% comment %}{% raw%}', /{% comment %} not closed/);
test('My name is {% comment %}super{% endcomment %} Shopify.', 'My name is Shopify.');
test('{% comment %}\n{{ foo}} \n{% endcomment %}', '');
});
it('should support case', function() {
testThrow('{% case "foo"%}', /{% case "foo"%} not closed/);
test('{% case "foo"%}' +
'{% when "foo" %}foo{% when "bar"%}bar' +
'{%endcase%}', 'foo');
test('{% case empty %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}', 'bar');
test('{% case false %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}', '');
test('{% case "a" %}' +
'{% when "b" %}b{% when "c"%}c{%else %}d' +
'{%endcase%}', 'd');
});
it('should support comment 1', function() { return testThrow('{% comment %}{% raw%}', /{% comment %} not closed/); });
it('should support comment 2', function() { return test('My name is {% comment %}super{% endcomment %} Shopify.', 'My name is Shopify.'); });
it('should support comment 3', function() { return test('{% comment %}\n{{ foo}} \n{% endcomment %}', ''); });
it('should support if', function() {
testThrow('{% if false%}yes', /tag {% if false%} not closed/);
test('{%if emptyArray%}a{%endif%}', '');
test('{% if 2==3 %}yes{%else%}no{%endif%}', 'no');
test('{% if 1>=2 and one<two %}a{%endif%}', '');
test('{% if one!=two %}yes{%else%}no{%endif%}', 'yes');
test('{% if false %}1{%elsif true%}2{%else%}3{%endif%}', '2');
test('{%if false%}{%if true%}{%else%}a{%endif%}{%endif%}', '');
});
it('should support case 1', function() { return testThrow('{% case "foo"%}', /{% case "foo"%} not closed/); });
it('should support case 2', function() { return test('{% case "foo"%}' +
'{% when "foo" %}foo{% when "bar"%}bar' +
'{%endcase%}', 'foo'); });
it('should support case 3', function() { return test('{% case empty %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}', 'bar'); });
it('should support case 4', function() { return test('{% case false %}' +
'{% when "foo" %}foo{% when ""%}bar' +
'{%endcase%}', ''); });
it('should support case 5', function() { return test('{% case "a" %}' +
'{% when "b" %}b{% when "c"%}c{%else %}d' +
'{%endcase%}', 'd'); });
it('should support unless', function() {
test('{% unless 1 %}yes{%else%}no{%endunless%}', 'no');
testThrow('{% unless 1>2 %}yes', /tag {% unless 1>2 %} not closed/);
test('{% unless 1>2 %}yes{%endunless%}', 'yes');
test('{% unless true %}{%endunless%}', '');
});
it('should support if 1', function() { return testThrow('{% if false%}yes', /tag {% if false%} not closed/); });
it('should support if 2', function() { return test('{%if emptyArray%}a{%endif%}', ''); });
it('should support if 3', function() { return test('{% if 2==3 %}yes{%else%}no{%endif%}', 'no'); });
it('should support if 4', function() { return test('{% if 1>=2 and one<two %}a{%endif%}', ''); });
it('should support if 5', function() { return test('{% if one!=two %}yes{%else%}no{%endif%}', 'yes'); });
it('should support if 6', function() { return test('{% if false %}1{%elsif true%}2{%else%}3{%endif%}', '2'); });
it('should support if 7', function() { return test('{%if false%}{%if true%}{%else%}a{%endif%}{%endif%}', ''); });
it('should support capture', function() {
test('{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}', 'A');
testThrow('{% capture = %}{%endcapture%}', /= not valid identifier/);
});
it('should support unless 1', function() { return test('{% unless 1 %}yes{%else%}no{%endunless%}', 'no'); });
it('should support unless 2', function() { return testThrow('{% unless 1>2 %}yes', /tag {% unless 1>2 %} not closed/); });
it('should support unless 3', function() { return test('{% unless 1>2 %}yes{%endunless%}', 'yes'); });
it('should support unless 4', function() { return test('{% unless true %}{%endunless%}', ''); });
it('should support capture 1', function() { return test('{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}', 'A'); });
it('should support capture 2', function() { return testThrow('{% capture = %}{%endcapture%}', /= not valid identifier/); });
it('should throw when for capture closed', function() {
testThrow('{%capture c%}{{c}}', /tag .* not closed/);
return testThrow('{%capture c%}{{c}}', /tag .* not closed/);
});
it('should support for', function() {
test('{%for c in alpha%}{{c}}{%endfor%}', 'abc');
return test('{%for c in alpha%}{{c}}{%endfor%}', 'abc');
});
it('should throw when for not closed', function() {
testThrow('{%for c in alpha%}{{c}}', /tag .* not closed/);
return testThrow('{%for c in alpha%}{{c}}', /tag .* not closed/);
});
it('should support for else', function() {
test('{%for c in ""%}a{%else%}b{%endfor%}', 'b');
return test('{%for c in ""%}a{%else%}b{%endfor%}', 'b');
});
it('should support for with forloop', function() {
@@ -149,65 +136,65 @@ describe('tags', function() {
dst = 'true.1.0.false.3.3.2a\n' +
'false.2.1.false.3.2.1b\n' +
'false.3.2.true.3.1.0c\n';
test(src, dst);
return test(src, dst);
});
it('should support for with continue and break', function() {
it('should support for with continue', function() {
src = '{% for i in (1..5) %}' +
'{% if i == 4 %}{% continue %}' +
'{% else %}{{ i }}' +
'{% endif %}' +
'{% endfor %}';
test(src, '1235');
return test(src, '1235');
});
it('should support for with break', function() {
src = '{% for i in (one..5) %}' +
'{% if i == 4 %}{% break %}{% endif %}' +
'{{ i }}' +
'{% endfor %}';
test(src, '123');
return test(src, '123');
});
it('should support for with limit and offset', function() {
it('should support for with limit', function() {
src = '{% for i in (1..5) limit:2 %}{{ i }}{% endfor %}';
test(src, '12');
return test(src, '12');
});
it('should support for with limit and offset', function() {
src = '{% for i in (1..10) limit:2 offset:5%}{{ i }}{% endfor %}';
test(src, '67');
return test(src, '67');
});
it('should support for reversed', function() {
src = '{% for i in (1..5) limit:2 reversed %}{{ i }}{% endfor %}';
test(src, '21');
return test(src, '21');
});
it('should support cycle', function() {
src = "{% cycle '1', '2', '3' %}";
test(src + src + src + src, '1231');
return test(src + src + src + src, '1231');
});
it('should throw when cycle candidates empty', function() {
testThrow('{%cycle%}', /empty candidates/);
return testThrow('{%cycle%}', /empty candidates/);
});
it('should support cycle in for block', function() {
src = '{% for i in (1..5) %}{% cycle one, "e"%}{% endfor %}';
test(src, '1e1e1');
return test(src, '1e1e1');
});
it('should support cycle group', function() {
src = "{% cycle one: '1', '2', '3'%}" +
"{% cycle 1: '1', '2', '3'%}" +
"{% cycle 2: '1', '2', '3'%}";
test(src, '121');
return test(src, '121');
});
it('should support increment', function() {
test('{% increment foo %}{%increment foo%}{{foo}}', '2');
test('{% increment one %}{{one}}', '2');
});
it('should support increment 1', function() { return test('{% increment foo %}{%increment foo%}{{foo}}', '2'); });
it('should support increment 2', function() { return test('{% increment one %}{{one}}', '2'); });
it('should support decrement', function() {
test('{% decrement foo %}{%decrement foo%}{{foo}}', '-2');
test('{% decrement one %}{{one}}', '0');
});
it('should support decrement 1', function() { return test('{% decrement foo %}{%decrement foo%}{{foo}}', '-2'); });
it('should support decrement 2', function() { return test('{% decrement one %}{{one}}', '0'); });
it('should support tablerow', function() {
src = '{% tablerow i in alpha cols:2 %}{{ i }}{% endtablerow %}';
@@ -215,18 +202,18 @@ describe('tags', function() {
'<tr class="row1"><td class="col1">a</td><td class="col2">b</td></tr>' +
'<tr class="row2"><td class="col1">c</td></tr>' +
'</table>';
test(src, dst);
return test(src, dst);
});
it('should support empty tablerow', function() {
src = '{% tablerow i in (1..0) cols:2 %}{{ i }}{% endtablerow %}';
dst = '<table></table>';
test(src, dst);
return test(src, dst);
});
it('should throw when tablerow not closed', function() {
src = '{% tablerow i in (1..0) cols:2 %}{{ i }}';
testThrow(src, /tag .* not closed/);
return testThrow(src, /tag .* not closed/);
});
it('should support tablerow with range', function() {
@@ -236,13 +223,15 @@ describe('tags', function() {
'<tr class="row2"><td class="col1">3</td><td class="col2">4</td></tr>' +
'<tr class="row3"><td class="col1">5</td></tr>' +
'</table>';
test(src, dst);
return test(src, dst);
});
it('tablerow should throw on illegal cols', function() {
testThrow('{% tablerow i in (1..5) cols:0 %}{{ i }}{% endtablerow %}',
it('tablerow should throw on illegal cols 1', function() {
return testThrow('{% tablerow i in (1..5) cols:0 %}{{ i }}{% endtablerow %}',
/illegal cols: 0/);
testThrow('{% tablerow i in (1..5) %}{{ i }}{% endtablerow %}',
});
it('tablerow should throw on illegal cols 2', function() {
return testThrow('{% tablerow i in (1..5) %}{{ i }}{% endtablerow %}',
/illegal cols: undefined/);
});
@@ -252,7 +241,7 @@ describe('tags', function() {
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td></tr>' +
'</table>';
test(src, dst);
return test(src, dst);
});
it('should support tablerow with offset', function() {
@@ -260,29 +249,29 @@ describe('tags', function() {
dst = '<table>' +
'<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>' +
'</table>';
test(src, dst);
return test(src, dst);
});
it.only('should support include', function() {
it('should support include', function() {
return liquid.renderFile('/current.html', ctx).should.eventually.equal('barFOObar');
});
it('should support include with relative path', function() {
expect(liquid.renderFile('relative.html', ctx)).to.equal('barfoobar');
return liquid.renderFile('relative.html', ctx).should.eventually.equal('barfoobar');
});
it('should support include: hash list', function() {
expect(liquid.renderFile('hash.html', ctx)).to.equal('harttle : admin : harttle');
return liquid.renderFile('hash.html', ctx).should.eventually.equal('harttle : admin : harttle');
});
it('should support include: parent scope', function() {
expect(liquid.renderFile('scope.html', ctx)).to.equal('color:yellow, shape:triangle');
return liquid.renderFile('scope.html', ctx).should.eventually.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);
return liquid.renderFile(filepath, ctx).should.eventually.equal(dst);
});
it('should support nested includes', function() {
@@ -292,22 +281,24 @@ describe('tags', function() {
it('should throw when block not closed', function() {
src = '{% layout "default-layout" %}{%block%}bar';
testThrow(src, /tag {%block%} not closed/);
return testThrow(src, /tag {%block%} not closed/);
});
it('should support layout', function() {
src = '{% layout "default-layout" %}{%block%}bar{%endblock%}';
test(src, 'foobarfoo');
return test(src, 'foobarfoo');
});
it('should support layout: multiple blocks', function() {
src = '{% layout "multi-blocks-layout" %}' +
'{%block a%}bara{%endblock%}' +
'{%block b%}barb{%endblock%}';
test(src, 'foobarabarbfoo');
return test(src, 'foobarabarbfoo');
});
it('should support layout: nested', function() {
it('should support layout: nested 1', function() {
src = '{% layout "multi-blocks" %}{% block a%}A{%endblock%}{%block c%}C{%endblock%}';
test(src, 'fooA;C;foo');
return test(src, 'fooA;C;foo');
});
it('should support layout: nested 2', function() {
src = '{% layout "multi-blocks" %}{%block c%}C{%endblock%}';
test(src, 'fooaaa;C;foo');
return test(src, 'fooaaa;C;foo');
});
});