Skip to content

Commit

Permalink
test: defines with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Nov 1, 2016
1 parent c0ed805 commit 11ad9a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
30 changes: 30 additions & 0 deletions test/defines.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var test = require('./util').test;
var doT = require('../doT');
var assert = require('assert');

describe('defines', function() {
describe('without parameters', function() {
it('should render define', function(){
testDef('{{##def.tmp:<div>{{!it.foo}}</div>#}}{{#def.tmp}}');
});

it('should render define if it is passed to doT.compile', function() {
testDef('{{#def.tmp}}', {tmp: '<div>{{!it.foo}}</div>'});
});
});

describe('with parameters', function() {
it('should render define', function(){
testDef('{{##def.tmp:foo:<div>{{!foo}}</div>#}}{{ var bar = it.foo; }}{{# def.tmp:bar }}');
});
});

function testDef(tmpl, defines) {
var fn = doT.compile(tmpl, defines);
assert.equal(fn({foo:'http'}), '<div>http</div>');
assert.equal(fn({foo:'http://abc.com'}), '<div>http:&#47;&#47;abc.com</div>');
assert.equal(fn({}), '<div></div>');
}
});
20 changes: 5 additions & 15 deletions test/dot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ var doT = require("../doT");


describe('doT', function(){
var basictemplate = "<div>{{!it.foo}}</div>",
basiccompiled = doT.template(basictemplate),
definestemplate = "{{##def.tmp:<div>{{!it.foo}}</div>#}}{{#def.tmp}}",
definescompiled = doT.template(definestemplate);
var basictemplate = "<div>{{!it.foo}}</div>";
var basiccompiled = doT.template(basictemplate);

describe('#template()', function(){
it('should return a function', function(){
Expand All @@ -25,19 +23,12 @@ describe('doT', function(){
});
});

describe('defines', function(){
it('should render define', function(){
assert.equal(definescompiled({foo:"http"}), "<div>http</div>");
assert.equal(definescompiled({foo:"http://abc.com"}), "<div>http:&#47;&#47;abc.com</div>");
assert.equal(definescompiled({}), "<div></div>");
});
});

describe('encoding with doNotSkipEncoded=false', function() {
it('should not replace &', function() {
global._encodeHTML = undefined;
doT.templateSettings.doNotSkipEncoded = false;
assert.equal(doT.template(definestemplate)({foo:"&amp;"}), "<div>&amp;</div>");
var fn = doT.template('<div>{{!it.foo}}</div>');
assert.equal(fn({foo:"&amp;"}), "<div>&amp;</div>");
});
});

Expand All @@ -63,10 +54,9 @@ describe('doT', function(){
it('should replace &', function() {
global._encodeHTML = undefined;
doT.templateSettings.doNotSkipEncoded = true;
assert.equal(doT.template(definestemplate)({foo:"&amp;"}), "<div>&#38;amp;</div>");
assert.equal(doT.template('<div>{{!it.foo}}</div>')({foo:"&amp;"}), "<div>&#38;amp;</div>");
assert.equal(doT.template('{{!it.a}}')({a:"& < > / ' \""}), "&#38; &#60; &#62; &#47; &#39; &#34;");
assert.equal(doT.template('{{!"& < > / \' \\""}}')(), "&#38; &#60; &#62; &#47; &#39; &#34;");

});
});

Expand Down

0 comments on commit 11ad9a6

Please sign in to comment.