Skip to content

Commit

Permalink
EJS tests to make Daniel happy :P
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jun 5, 2012
1 parent 80dca92 commit 26f8ded
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 44 deletions.
41 changes: 23 additions & 18 deletions lib/template/adapters/ejs/template.js
Expand Up @@ -24,6 +24,7 @@ ejs.Template = function (p) {
var params = p || {};

this.mode = null;
this.truncate = false;
this.templateText = params.text ||
// If you don't want to use Fleegix.js,
// override getTemplateTextFromNode to use
Expand All @@ -42,7 +43,7 @@ ejs.Template = function (p) {
};

ejs.Template.prototype = new function () {
var _REGEX = /(<%%)|(%%>)|(<%=)|(<%-)|(<%#)|(<%)|(%>\n)|(%>)|(\n)/;
var _REGEX = /(<%%)|(%%>)|(<%=)|(<%-)|(<%#)|(<%)|(%>)|(-%>)/;
this.modes = {
EVAL: 'eval'
, ESCAPED: 'escaped'
Expand All @@ -67,7 +68,7 @@ ejs.Template.prototype = new function () {
// Requires the fleegix.xhr module
if (typeof fleegix.xhr == 'undefined') {
throw('Requires fleegix.xhr module.'); }
var _this = this;
var self = this;
var url = params.url;
var noCache = params.preventCache || false;
// Found text in cache, and caching is turned on
Expand All @@ -79,11 +80,11 @@ ejs.Template.prototype = new function () {
// Callback for setting templateText and caching --
// used for both sync and async loading
var callback = function (s) {
_this.templateText = s;
self.templateText = s;
ejs.templateTextCache[url] = s;
// Use afterLoaded hook if set
if (typeof _this.afterLoaded == 'function') {
_this.afterLoaded();
if (typeof self.afterLoaded == 'function') {
self.afterLoaded();
}
};
var opts;
Expand Down Expand Up @@ -169,18 +170,21 @@ ejs.Template.prototype = new function () {
return arr;
};
this.scanLine = function (line) {
var _this = this;
var _addOutput = function () {
// Preserve literal slashes
line = line.replace(/\\/g, '\\\\');
// Convert linebreaks
line = line.replace(/\n/, '\\n');
line = line.replace(/\r/, '\\r');
// Escape double-quotes -- this will be the delimiter
// during execution
line = line.replace(/"/g, '\\"');
_this.source += '_output += "' + line + '";';
};
var self = this
, _addOutput = function () {
if (self.truncate) {
line = line.replace('\n', '');
}
// Preserve literal slashes
line = line.replace(/\\/g, '\\\\');
// Convert linebreaks
line = line.replace(/\n/g, '\\n');
line = line.replace(/\r/g, '\\r');
// Escape double-quotes -- this will be the delimiter
// during execution
line = line.replace(/"/g, '\\"');
self.source += '_output += "' + line + '";';
};
switch (line) {
case '<%':
this.mode = this.modes.EVAL;
Expand All @@ -199,11 +203,12 @@ ejs.Template.prototype = new function () {
this.source += '_output += "' + line + '";';
break;
case '%>':
case '%>\n':
case '-%>':
if (this.mode == this.modes.LITERAL) {
_addOutput();
}
this.mode = null;
this.truncate = line.indexOf('-') == 0;
break;
default:
// In script mode, depends on type of tag
Expand Down
177 changes: 151 additions & 26 deletions test/ejs.js
Expand Up @@ -3,32 +3,157 @@ require('../lib/geddy');

var Template = require('../lib/template/adapters/ejs/template.js').Template
, assert = require('assert')
, tests;

tests = new (function () {

this.testRenderVar = function () {
// Create a template out of the markup in the file
var templ = new Template({text: "<% var foo = 'FOO'; %><%= foo; %>"});
templ.process({data: {}});
assert.equal('FOO', templ.markup);
};

this.testRenderData = function () {
// Create a template out of the markup in the file
var templ = new Template({text: "<%= foo; %>"});
templ.process({data: {foo: 'FOO'}});
assert.equal('FOO', templ.markup);
};

this.testEscapeData = function () {
// Create a template out of the markup in the file
var templ = new Template({text: "<%= foo; %>"});
templ.process({data: {foo: '<FOO>'}});
assert.equal('&lt;FOO&gt;', templ.markup);
};

})();
, tests
, render = function (str, d) {
var data = d || {};
var templ = new Template({text: str});
templ.process({data: data});
return templ.markup;
};

tests = {
'test rendering a single variable': function () {
var str = "<% var foo = 'FOO'; %><%= foo; %>"
, actual = render(str);
assert.equal('FOO', actual);
}

, 'test rendering passed-in data': function () {
var str = "<%= foo; %>"
, actual = render(str, {foo: 'FOO'});
assert.equal('FOO', actual);
}

, 'test escaping': function () {
assert.equal('&lt;script&gt;', render('<%= "<script>" %>'));
assert.equal('<script>', render('<%- "<script>" %>'));
}

, 'test HTML equality': function () {
assert.equal('<p>yay</p>', render('<p>yay</p>'));
}

, 'test basic conditional': function () {
var str = '<% if (name) { %><p><%= name %></p><% } %>'
, actual = render(str, {name: 'mde'});
assert.equal('<p>mde</p>', actual);
}

, 'test newlines': function () {
var html = '\n<p>mde</p>\n<p>mde@fleegix.org</p>\n'
, str = '<% if (name) { %>\n<p><%= name %></p>\n<p><%= email %></p>\n<% } %>'
, data = { name: 'mde', email: 'mde@fleegix.org' };
assert.equal(html, render(str, data));
}

, 'test single quotes': function () {
var html = '<p>WAHOO</p>'
, str = "<p><%= up('wahoo') %></p>"
, data = {up: function (str){ return str.toUpperCase(); }};
assert.equal(html, render(str, data));
}

, 'test single quotes in HTML': function () {
var html = '<p>WAHOO that\'s cool</p>'
, str = '<p><%= up(\'wahoo\') %> that\'s cool</p>'
, data = {up: function (str){ return str.toUpperCase(); }};
assert.equal(html, render(str, data));
}

, 'test multiple single quotes': function () {
var html = "<p>couldn't shouldn't can't</p>"
, str = "<p>couldn't shouldn't can't</p>";
assert.equal(html, render(str));
}

, 'test single quotes inside tags': function () {
var html = '<p>string</p>'
, str = "<p><%= 'string' %></p>";
assert.equal(html, render(str));
}

, 'test back-slashes in the document': function () {
var html = "<p>backslash: '\\'</p>"
, str = "<p>backslash: '\\'</p>";
assert.equal(html, render(str));
}

, 'test double quotes': function (){
var html = '<p>WAHOO</p>'
, str = '<p><%= up("wahoo") %></p>'
, data = {up: function (str){ return str.toUpperCase(); }};
assert.equal(html, render(str, data));
}

, 'test multiple double quotes': function () {
var html = '<p>just a "test" wahoo</p>'
, str = '<p>just a "test" wahoo</p>';
assert.equal(html, render(str));
}

, 'test whitespace': function (){
var html = '<p>foo</p>'
, str = '<p><%="foo"%></p>';
assert.equal(html, render(str));

var html = '<p>foo</p>'
, str = '<p><%=bar%></p>';
assert.equal(html, render(str, {bar: 'foo'}));
}

, 'test iteration': function (){
var html = '<p>foo</p>',
str = '<% for (var key in items) { %>'
+ '<p><%= items[key] %></p>'
+ '<% } %>';
assert.equal(html, render(str, {items: ['foo']}));

var html = '<p>foo</p>',
str = '<% items.forEach(function (item){ %>'
+ '<p><%= item %></p>'
+ '<% }) %>';
assert.equal(html, render(str, {items: ['foo']}));
}

, 'test slurp' : function () {
var expected = 'me\nhere'
, str = 'me<% %>\nhere';
assert.equal(expected, render(str));

var expected = 'mehere'
, str = 'me<% -%>\nhere';
assert.equal(expected, render(str));

var expected = 'me\nhere'
, str = 'me<% -%>\n\nhere';
assert.equal(expected, render(str));

var expected = 'me inbetween \nhere'
, str = 'me <%= x %> \nhere';
assert.equal(expected, render(str,{x:'inbetween'}));

var expected = 'me inbetween here'
, str = 'me <%= x -%> \nhere';
assert.equal(expected, render(str, {x:'inbetween'}));

var expected = 'me <p>inbetween</p> here'
, str = 'me <%- x -%> \nhere';
assert.equal(expected, render(str,{x:'<p>inbetween</p>'}));

var expected = '\n Hallo 0\n\n Hallo 1\n\n'
, str = '<% for(var i in [1,2]) { %>\n' +
' Hallo <%= i %>\n' +
'<% } %>\n';
assert.equal(expected, render(str));

var expected = ' Hallo 0\n Hallo 1\n'
, str = '<% for(var i in [1,2]) { -%>\n' +
' Hallo <%= i %>\n' +
'<% } -%>\n';
assert.equal(expected, render(str));
}

};

for (var p in tests) {
if (typeof tests[p] == 'function') {
Expand Down

0 comments on commit 26f8ded

Please sign in to comment.