forked from assemble/assemble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.engines.js
178 lines (157 loc) · 5.4 KB
/
collection.engines.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
require('mocha');
require('should');
var assert = require('assert');
var support = require('./support');
var App = support.resolve();
var Views = App.Views;
var collection, pages;
describe('collection engines', function() {
beforeEach(function() {
pages = new Views();
});
it('should throw an error when engine name is invalid:', function() {
(function() {
pages.engine(null, {});
}).should.throw('expected engine ext to be a string or array.');
});
it('should register an engine to the given extension', function() {
pages.engine('hbs', function() {});
assert(typeof pages.engines['.hbs'] === 'object');
});
it('should set an engine with the given extension', function() {
var hbs = function() {};
hbs.render = function() {};
hbs.renderFile = function() {};
pages.engine('hbs', hbs);
assert(pages.engines['.hbs']);
assert(pages.engines['.hbs'].renderFile);
assert(pages.engines['.hbs'].render);
});
it('should get an engine:', function() {
pages.engine('hbs', function() {});
var hbs = pages.engine('hbs');
assert(typeof hbs === 'object');
assert(hbs.hasOwnProperty('render'));
assert(hbs.hasOwnProperty('compile'));
});
it('should register multiple engines to the given extension', function() {
pages.engine(['hbs', 'md'], function() {});
assert(typeof pages.engines['.hbs'] === 'object');
assert(typeof pages.engines['.md'] === 'object');
});
});
describe('engines', function() {
beforeEach(function() {
pages = new Views();
pages.addView('foo.tmpl', {content: 'A <%= letter %> {{= letter }} C'});
pages.addView('bar.tmpl', {content: 'A <%= letter %> {{ letter }} C'});
});
it('should register an engine:', function() {
pages.engine('a', {render: function() {}});
pages.engines.should.have.property('.a');
});
it('should use custom delimiters:', function(cb) {
pages.engine('tmpl', require('engine-base'), {
delims: ['{{', '}}']
});
pages.render('foo.tmpl', {letter: 'B'}, function(err, res) {
if (err) return cb(err);
res.content.should.equal('A <%= letter %> B C');
cb();
});
});
it('should override individual delims values:', function(cb) {
pages.engine('tmpl', require('engine-base'), {
interpolate: /\{{([^}]+)}}/g,
evaluate: /\{{([^}]+)}}/g,
escape: /\{{-([^}]+)}}/g
});
pages.render('bar.tmpl', {letter: 'B'}, function(err, res) {
if (err) return cb(err);
res.content.should.equal('A <%= letter %> B C');
cb();
});
});
it('should get an engine:', function() {
pages.engine('a', {
render: function() {}
});
var a = pages.engine('a');
a.should.have.property('render');
});
});
describe('engine selection:', function() {
beforeEach(function(cb) {
collection = new Views();
collection.engine('tmpl', require('engine-base'));
collection.engine('hbs', require('engine-handlebars'));
cb();
});
it('should get the engine from file extension:', function(cb) {
var pages = new Views();
pages.engine('tmpl', require('engine-base'));
pages.engine('hbs', require('engine-handlebars'));
pages.addView('a.tmpl', {content: '<%= a %>', locals: {a: 'b'}})
.render(function(err, view) {
if (err) return cb(err);
assert(view.content === 'b');
cb();
});
});
it('should use the engine defined on the collection:', function(cb) {
var posts = new Views({engine: 'hbs'});
posts.engine('tmpl', require('engine-base'));
posts.engine('hbs', require('engine-handlebars'));
posts.addView('a', {content: '{{a}}', locals: {a: 'b'}})
.render(function(err, view) {
if (err) return cb(err);
assert(view.content === 'b');
cb();
});
});
it('should use the engine defined on the view:', function(cb) {
var posts = new Views();
posts.engine('tmpl', require('engine-base'));
posts.engine('hbs', require('engine-handlebars'));
posts.addView('a', {content: '{{a}}', engine: 'hbs', locals: {a: 'b'}})
.render(function(err, view) {
if (err) return cb(err);
assert(view.content === 'b');
cb();
});
});
it('should use the engine defined on view.options:', function(cb) {
var posts = new Views();
posts.engine('tmpl', require('engine-base'));
posts.engine('hbs', require('engine-handlebars'));
posts.addView('a', {content: '{{a}}', data: {a: 'b'}, options: {engine: 'hbs'}})
.render(function(err, view) {
if (err) return cb(err);
assert(view.content === 'b');
cb();
});
});
it('should use the engine defined on view.data:', function(cb) {
var posts = new Views();
posts.engine('tmpl', require('engine-base'));
posts.engine('hbs', require('engine-handlebars'));
posts.addView('a', {content: '{{a}}', locals: {a: 'b'}, data: {engine: 'hbs'}})
.render(function(err, view) {
if (err) return cb(err);
assert(view.content === 'b');
cb();
});
});
it('should use the engine defined on render locals:', function(cb) {
var posts = new Views();
posts.engine('tmpl', require('engine-base'));
posts.engine('hbs', require('engine-handlebars'));
posts.addView('a', {content: '{{a}}', locals: {a: 'b'}})
.render({engine: 'hbs'}, function(err, view) {
if (err) return cb(err);
assert(view.content === 'b');
cb();
});
});
});