-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.es6
113 lines (87 loc) · 2.72 KB
/
test.es6
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
var path = require('path');
var fs = require('fs'),
exists = fs.existsSync || path.existsSync;
var assert = require('assert');
var snakeskin = require('./snakeskin');
var testFolder = path.resolve(__dirname, 'tests');
var tpls = {};
global.i18n = function (str) {
return str;
};
snakeskin.compile(
fs.readFileSync(path.join(__dirname, 'test.ss')),
{
context: tpls,
prettyPrint: true
}
);
var errorPath = path.join(__dirname, 'error.txt');
var asserts = [],
prfx = -1;
function run(params) {
var options = JSON.stringify(params),
debug = params.debug = {};
prfx++;
fs.readdirSync(testFolder).forEach((file) => {
if (path.extname(file) === '.ss') {
let src = path.join(testFolder, file),
txt = String(fs.readFileSync(src)).split('###');
txt.forEach((el, i) => {
txt[i] = el.trim();
});
let starts = txt[0].split(/[\n\r]+/),
results = txt[2].split('***');
let obj = {
tpl: txt[1],
id: path.basename(file, '.ss'),
js: []
};
if (!prfx) {
asserts.push(obj);
}
try {
let start = Date.now();
let res = snakeskin.compile(txt[1], params, {
file: path.join(testFolder, file)
});
if (!prfx) {
console.log(`${file} ${Date.now() - start}ms`);
}
fs.writeFileSync(`${src}_${prfx}.js`, res);
} catch (err) {
fs.writeFileSync(errorPath, `File: ${file}\n\n${err.message}${debug['code'] ? `\n\nCode:\n\n${debug['code']}` : ''}`);
throw err;
}
let tpl = require(`./tests/${file}_${prfx}.js`).init(snakeskin);
starts.forEach((el, i) => {
let params = el.split(' ; '),
res = '';
try {
obj.js.push(`equal(${params[0]}(${params.slice(1)}).trim(), '${results[i].trim()}');`);
// eval нужен чтобы сохранить информацию о типах
res = eval(`tpl.${params[0]}(${params.slice(1)}).trim()`);
assert.equal(
res,
results[i].trim()
);
} catch (err) {
console.error(`File: ${file} - ${prfx} (${options}), Tpl: ${params[0]}`);
fs.writeFileSync(
errorPath,
`File: ${file} - ${prfx} (${options}), Tpl: ${params[0]}\n\nResult:\n${res}\n\nExpected:\n${results[i].trim()}\n\nTest:\n${txt[1]}\n\nCode:\n${debug['code']}`
);
throw err;
}
});
}
});
}
run({commonJS: true, prettyPrint: true, throws: true});
run({commonJS: true, prettyPrint: true, throws: true});
run({commonJS: true, prettyPrint: true, throws: true, inlineIterators: true});
run({commonJS: true, prettyPrint: true, throws: true, stringBuffer: true});
run({commonJS: true, prettyPrint: true, throws: true, stringBuffer: true, inlineIterators: true});
fs.writeFileSync(path.join(__dirname, 'tests', 'tests.html'), tpls.test(asserts));
if (exists(errorPath)) {
fs.unlinkSync(errorPath);
}