-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
82 lines (67 loc) · 2.43 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var subject = require(__dirname);
var assert = require('assert');
/* jshint mocha:true */
describe('vmplate', function() {
it('throws on an unclosed code section', function() {
assert.throws(function() { subject('<%= oops'); }, /SyntaxError/);
});
it('throws on syntax error', function() {
assert.throws(function() { subject('<%= ) %>'); }, /SyntaxError/);
});
it('interpolates one value', function() {
var render = subject('me <%= one %> and I');
assert.equal(render({ one: 'myself' }), 'me myself and I');
});
it('interpolates multiple values', function() {
var render = subject('<%= one %> <%= two %> and <%= three %>');
var output = render({ one: 'me', two: 'myself', three: 'I' });
assert.equal(output, 'me myself and I');
});
it('evaluates code without output', function() {
var render = subject('<% var who = "george"; %>by <%= who %>!');
assert.equal(render(), 'by george!');
});
it('evaluates any valid javascript', function() {
var render = subject('<% for(var i = 0; i < 3; i++){ %>hey<% } %>!');
assert.equal(render(), 'heyheyhey!');
});
it('does not evaluate empty code blocks', function() {
var render = subject('o<%= %>hai');
assert.equal(render(), 'ohai');
});
it('iterates over an array in the locals context', function() {
var render = subject(
'<% for(var i = 0; i < arr.length; i++){ %>' +
'<%= arr[i] %>' +
'<% } %>'
);
assert.equal(render({ arr: [1, 2, 3] }), '123');
});
it('calls a function in the locals context', function() {
var render = subject('<%= upcase(yell) %>!');
render.locals.upcase = function(str) {
return str.toUpperCase();
};
assert.equal(render({ yell: 'yay' }), 'YAY!');
});
it('inherits locals from the instance', function() {
var render = subject('<%= foo %> <%= bar %>');
render.locals.foo = 'omg';
render.locals.bar = 'wow';
assert.equal(render({ foo: 'yay' }), 'yay wow');
});
it('can pass in instance locals', function() {
var render = subject('<%= foo %> <%= bar %>', {
foo: 'omg',
bar: 'wow'
});
assert.equal(render({ foo: 'yay' }), 'yay wow');
});
it('inherits locals from the factory', function() {
var render = subject('<%= foo %> <%= bar %> <%= baz %>');
subject.locals.foo = 'omg';
subject.locals.bar = 'wow';
render.locals.baz = 'amaze';
assert.equal(render({ foo: 'yay' }), 'yay wow amaze');
});
});