-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
83 lines (74 loc) · 3.07 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
var formatNumber = require('.');
var res = true;
(function () {
var triadTmpl = function(n) { return '<span>' + n + '</span>' };
var numberTmpl = triadTmpl;
var te = new Te(function (num) {
return formatNumber(num, triadTmpl, numberTmpl);
});
te.st(1, '<span><span>1</span></span>');
te.st(100, '<span><span>100</span></span>');
te.st(10001, '<span><span>10</span><span>001</span></span>');
te.st(-10001, '<span>-<span>10</span><span>001</span></span>');
te.st(-10001.0, '<span>-<span>10</span><span>001</span></span>'); // .0 is skiped
te.st(-10001.505, '<span>-<span>10</span><span>001</span>.505</span>');
te.st(-0.505, '<span>-<span>0</span>.505</span>');
te.st(0.505, '<span><span>0</span>.505</span>');
te.st(0, '<span><span>0</span></span>');
te.st('1', '<span><span>1</span></span>');
te.st('100', '<span><span>100</span></span>');
te.st('10001', '<span><span>10</span><span>001</span></span>');
te.st('-10001', '<span>-<span>10</span><span>001</span></span>');
te.st('-10001.0', '<span>-<span>10</span><span>001</span>.0</span>');
te.st('-10001.505', '<span>-<span>10</span><span>001</span>.505</span>');
te.st('-0.505', '<span>-<span>0</span>.505</span>');
te.st('0.505', '<span><span>0</span>.505</span>');
te.st('0', '<span><span>0</span></span>');
te.st('', '');
te.st(' ', '');
te.st(' 0', '<span><span>0</span></span>');
te.st(' 1', '<span><span>1</span></span>');
te.st(' -10001.505', '<span>-<span>10</span><span>001</span>.505</span>');
res = te.nd(false, true);
})();
(function () {
var triadTmpl = function(n) { return '<span>' + n + '</span>' };
var concat = function(sign, triads, frac) {
var val = triads.join('') + frac;
return sign === '-' ? '(' + val + ')' : val;
}
var numberTmpl = triadTmpl;
var te = new Te(function (num) {
return formatNumber(num, triadTmpl, numberTmpl, concat);
});
te.st(' -10001.505', '<span>(<span>10</span><span>001</span>.505)</span>');
te.nd(true, res);
})();
function Te (fn) {
return {
isAlright: true,
okCount: 0,
failCount: 0,
st: function test(num, expected) {
var result = fn(num);
if (result === expected) {
++this.okCount;
console.log('OK: ' + fn.name + '(' + JSON.stringify(num) + ') === ' + JSON.stringify(expected));
} else {
++this.failCount;
console.error('FAIL: ' + fn.name + '(' + JSON.stringify(num) + ') === ' + result + '\n // Expected: ' + JSON.stringify(expected));
this.isAlright = false;
}
},
nd: function final(shouldExit, prevRes) {
this.isAlright || console.error(this.failCount + ' tests failed!');
this.okCount && console.log(this.okCount + ' tests passed!');
var res = this.isAlright && prevRes;
if (shouldExit) {
process.exit(res ? 0 : -1);
} else {
return res;
}
}
}
}