forked from gianlucamancini/gulp-nunjucks-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
95 lines (79 loc) · 2.17 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
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
var path = require('path');
var assert = require('assert');
var gutil = require('gulp-util');
var nunjucks = require('./');
var dir = String.prototype.concat.bind(__dirname, '/');
function createFile(filePath, contents) {
var absPath = dir(filePath);
var base = path.dirname(absPath);
return new gutil.File({
base: base,
path: absPath,
contents: new Buffer(contents)
});
}
it('should render Nunjucks templates to HTML', function(cb) {
var opts = {
locals: {
name: 'James'
}
};
nunjucks(opts)
.on('error', cb)
.on('data', function(file) {
assert.equal(file.path, dir('fixture/fixture.html'));
assert.equal(file.relative, 'fixture.html');
assert(/<p>James<\/p>/.test(file.contents.toString('utf8')));
cb();
})
.write(createFile('fixture/fixture.html', '<p>{{ name }}</p>'));
});
it('should render Nunjucks templates with custom tags to HTML', function(cb) {
var opts = {
locals: {
name: 'Bond'
},
tags: {variableStart: '{@', variableEnd: '@}'}
};
nunjucks(opts)
.on('error', cb)
.on('data', function(file) {
assert(/<p>Bond<\/p>/.test(file.contents.toString('utf8')));
cb();
})
.write(createFile('fixture/fixture.html', '<p>{@ name @}</p>'));
});
it('should render Nunjucks templates asynchronously', function(cb) {
function asyncFn(name, cb) {
cb(null, 'Hello ' + name);
}
var opts = {
setUp: function(env) {
env.addFilter('greet', function(str, cb) {
asyncFn(str, cb);
}, true);
return env;
}
};
nunjucks(opts)
.on('error', cb)
.on('data', function(file) {
assert(/Hello World/.test(file.contents.toString('utf8')));
cb();
})
.write(createFile('fixture/fixture.html', '{{ "World"|greet }}'));
});
it('should render Nunjucks templates with different extension', function(cb) {
var opts = {
ext: '.html'
};
nunjucks(opts)
.on('error', cb)
.on('data', function(file) {
assert.equal(file.path, dir('fixture/fixture.html'));
assert.equal(file.relative, 'fixture.html');
cb();
})
.write(createFile('fixture/fixture.nunjucks', 'Hello World'));
});