run-utils.js
3.31 KB
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
var fs = require('fs');
var assert = require('assert');
var pug = require('../');
var uglify = require('uglify-js');
var mkdirp = require('mkdirp').sync;
var filters = {
custom: function (str, options) {
assert(options.opt === 'val');
assert(options.num === 2);
return 'BEGIN' + str + 'END';
}
};
// test cases
function writeFileSync(filename, data) {
try {
if (fs.readFileSync(filename, 'utf8') === data.toString('utf8')) {
return;
}
} catch (ex) {
if (ex.code !== 'ENOENT') {
throw ex;
}
}
fs.writeFileSync(filename, data);
}
function findCases(dir) {
return fs.readdirSync(dir).filter(function(file){
return ~file.indexOf('.pug');
}).map(function(file){
return file.replace('.pug', '');
});
}
function testSingle(it, suffix, test){
var name = test.replace(/[-.]/g, ' ');
it(name, function(){
var path = __dirname + '/cases' + suffix + '/' + test + '.pug';
var str = fs.readFileSync(path, 'utf8');
var fn = pug.compile(str, {
filename: path,
pretty: true,
basedir: __dirname + '/cases' + suffix,
filters: filters,
filterAliases: {'markdown': 'markdown-it'},
});
var actual = fn({ title: 'Pug' });
writeFileSync(__dirname + '/output' + suffix + '/' + test + '.html', actual);
var html = fs.readFileSync(__dirname + '/cases' + suffix + '/' + test + '.html', 'utf8').trim().replace(/\r/g, '');
var clientCode = uglify.minify(pug.compileClient(str, {
filename: path,
pretty: true,
compileDebug: false,
basedir: __dirname + '/cases' + suffix,
filters: filters,
filterAliases: {'markdown': 'markdown-it'},
}), {output: {beautify: true}, mangle: false, compress: false, fromString: true}).code;
var clientCodeDebug = uglify.minify(pug.compileClient(str, {
filename: path,
pretty: true,
compileDebug: true,
basedir: __dirname + '/cases' + suffix,
filters: filters,
filterAliases: {'markdown': 'markdown-it'},
}), {output: {beautify: true}, mangle: false, compress: false, fromString: true}).code;
writeFileSync(__dirname + '/output' + suffix + '/' + test + '.js', uglify.minify(pug.compileClient(str, {
filename: path,
pretty: false,
compileDebug: false,
basedir: __dirname + '/cases' + suffix,
filters: filters,
filterAliases: {'markdown': 'markdown-it'},
}), {output: {beautify: true}, mangle: false, compress: false, fromString: true}).code);
if (/filter/.test(test)) {
actual = actual.replace(/\n| /g, '');
html = html.replace(/\n| /g, '');
}
if (/mixins-unused/.test(test)) {
assert(/never-called/.test(str), 'never-called is in the pug file for mixins-unused');
assert(!/never-called/.test(clientCode), 'never-called should be removed from the code');
}
expect(actual.trim()).toEqual(html);
actual = Function('pug', clientCode + '\nreturn template;')()({ title: 'Pug' });
if (/filter/.test(test)) {
actual = actual.replace(/\n| /g, '');
}
expect(actual.trim()).toEqual(html);
actual = Function('pug', clientCodeDebug + '\nreturn template;')()({ title: 'Pug' });
if (/filter/.test(test)) {
actual = actual.replace(/\n| /g, '');
}
expect(actual.trim()).toEqual(html);
});
}
module.exports = {
filters,
findCases,
testSingle,
};