/* jshint mocha: true */ /* eslint-env node, mocha */ /** * Module dependencies. */ var ejs = require('..'); var fs = require('fs'); var read = fs.readFileSync; var assert = require('assert'); var path = require('path'); var LRU = require('lru-cache'); try { fs.mkdirSync(__dirname + '/tmp'); } catch (ex) { if (ex.code !== 'EEXIST') { throw ex; } } // From https://gist.github.com/pguillory/729616 function hook_stdio(stream, callback) { var old_write = stream.write; stream.write = (function() { return function(string, encoding, fd) { callback(string, encoding, fd); }; })(stream.write); return function() { stream.write = old_write; }; } /** * Load fixture `name`. */ function fixture(name) { return read('test/fixtures/' + name, 'utf8'); } /** * User fixtures. */ var users = []; users.push({name: 'geddy'}); users.push({name: 'neil'}); users.push({name: 'alex'}); suite('ejs.compile(str, options)', function () { test('compile to a function', function () { var fn = ejs.compile('

yay

'); assert.equal(fn(), '

yay

'); }); test('empty input works', function () { var fn = ejs.compile(''); assert.equal(fn(), ''); }); test('throw if there are syntax errors', function () { try { ejs.compile(fixture('fail.ejs')); } catch (err) { assert.ok(err.message.indexOf('compiling ejs') > -1); try { ejs.compile(fixture('fail.ejs'), {filename: 'fail.ejs'}); } catch (err) { assert.ok(err.message.indexOf('fail.ejs') > -1); return; } } throw new Error('no error reported when there should be'); }); test('allow customizing delimiter local var', function () { var fn; fn = ejs.compile('

', {delimiter: '?'}); assert.equal(fn({name: 'geddy'}), '

geddy

'); fn = ejs.compile('

<:= name :>

', {delimiter: ':'}); assert.equal(fn({name: 'geddy'}), '

geddy

'); fn = ejs.compile('

<$= name $>

', {delimiter: '$'}); assert.equal(fn({name: 'geddy'}), '

geddy

'); }); test('default to using ejs.delimiter', function () { var fn; ejs.delimiter = '&'; fn = ejs.compile('

<&= name &>

'); assert.equal(fn({name: 'geddy'}), '

geddy

'); fn = ejs.compile('

<|= name |>

', {delimiter: '|'}); assert.equal(fn({name: 'geddy'}), '

geddy

'); delete ejs.delimiter; }); test('support custom escape function', function () { var customEscape; var fn; customEscape = function customEscape(str) { return !str ? '' : str.toUpperCase(); }; fn = ejs.compile('HELLO <%= name %>', {escape: customEscape}); assert.equal(fn({name: 'world'}), 'HELLO WORLD'); }); test('strict mode works', function () { assert.equal(ejs.render(fixture('strict.ejs'), {}, {strict: true}), 'true'); }); }); suite('client mode', function () { test('have a working client option', function () { var fn; var str; var preFn; fn = ejs.compile('

<%= foo %>

', {client: true}); str = fn.toString(); if (!process.env.running_under_istanbul) { eval('var preFn = ' + str); assert.equal(preFn({foo: 'bar'}), '

bar

'); } }); test('support client mode without locals', function () { var fn; var str; var preFn; fn = ejs.compile('

<%= "foo" %>

', {client: true}); str = fn.toString(); if (!process.env.running_under_istanbul) { eval('var preFn = ' + str); assert.equal(preFn(), '

foo

'); } }); test('not include rethrow() in client mode if compileDebug is false', function () { var fn = ejs.compile('

<%= "foo" %>

', { client: true, compileDebug: false }); // There could be a `rethrow` in the function declaration assert((fn.toString().match(/rethrow/g) || []).length <= 1); }); test('support custom escape function in client mode', function () { var customEscape; var fn; var str; customEscape = function customEscape(str) { return !str ? '' : str.toUpperCase(); }; fn = ejs.compile('HELLO <%= name %>', {escape: customEscape, client: true}); str = fn.toString(); if (!process.env.running_under_istanbul) { eval('var preFn = ' + str); assert.equal(preFn({name: 'world'}), 'HELLO WORLD'); // eslint-disable-line no-undef } }); test('escape filename in errors in client mode', function () { assert.throws(function () { var fn = ejs.compile('<% throw new Error("whoops"); %>', {client: true, filename: '