-
Notifications
You must be signed in to change notification settings - Fork 10
/
tests.js
69 lines (63 loc) · 1.83 KB
/
tests.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
var UTIL = require('util'),
FS = require('fs'),
PATH = require('path'),
K = require('../lib/krasota'),
matchTop = K.matchTop,
log = K.log,
testFile = process.argv[2],
testPrefix = testFile.replace(/\.js$/, ''),
dir = PATH.dirname(testFile);
FS.readFile(testFile, 'utf8', function(err, input){
if (err) throw err;
log(input);
var tree = matchTop(K.KrasotaJSParser, input, 'tree'),
identity = matchTop(K.KrasotaJSIdentity, tree, 'identity'),
beautify = K.KrasotaJSBeautify(
fileContent(testPrefix + '.beautifiers')
.split(/\s+/)
.filter(function(b) { return b })
.map(function(b) {
b = b.replace(/^krasota/, '..');
return require(b.match(/^\./) ? require('path').resolve(dir, b) : b).KrasotaJS
}),
identity),
serialize = matchTop(K.KrasotaJSSerializer, beautify, 'serialize');
input != serialize &&
FS.writeFileSync(testPrefix + '.result', serialize)
OkOrNot(
fileContent(testPrefix + '.expect', input) == serialize,
testFile);
});
function fileContent(path, or) {
return FS.existsSync(path) ? String(FS.readFileSync(path)) : or || '';
}
function OkOrNot(ok, msg) {
var m = ok ? ['green', 'OK'] : ['red', 'NOT OK'];
console.log(Color(m[0], m[1] + ': ' + msg));
if (!ok) process.exit(1);
}
var colors = {
black: '30',
dgray: '1;30',
red: '31',
lred: '1;31',
green: '32',
lgreen: '1;32',
brown: '33',
yellow: '1;33',
blue: '34',
lblue: '1;34',
purple: '35',
lpurple: '1;35',
cyan: '36',
lcyan: '1;36',
lgray: '37',
white: '1;37'
};
function Color(c, str) {
return [
'\033[', colors[c], 'm',
str,
'\033[m'
].join('');
}