forked from beautifier/js-beautify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitytest.js
146 lines (126 loc) · 4.18 KB
/
sanitytest.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// simple testing interface
// written by Einar Lielmanis, einar@beautifier.io
//
// usage:
//
// var t = new SanityTest(function (x) { return x; }, 'my function');
// t.expect('input', 'output');
// t.expect('a', 'a');
// output_somewhere(t.results()); // good for <pre>, html safe-ish
// alert(t.results_raw()); // html unescaped
function SanityTest(func, name_of_test) {
'use strict';
var test_func = func || function(x) {
return x;
};
var test_name = name_of_test || '';
var n_failed = 0;
var n_succeeded = 0;
var failures = [];
this.test_function = function(func, name) {
test_func = func;
test_name = name || '';
};
this.get_exitcode = function() {
return n_succeeded === 0 || n_failed !== 0 ? 1 : 0;
};
this.expect = function(parameters, expected_value) {
// multi-parameter calls not supported (I don't need them now).
var result = test_func(parameters);
// proper array checking is a pain. i'll maybe do it later, compare strings representations instead
if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') === expected_value.join(', '))) {
n_succeeded += 1;
return true;
} else {
n_failed += 1;
failures.push([test_name, parameters, expected_value, result]);
return false;
}
};
this.results_raw = function() {
var results = '';
if (n_failed === 0) {
if (n_succeeded === 0) {
results = 'No tests run.';
} else {
results = 'All ' + n_succeeded + ' tests passed.';
}
} else {
for (var i = 0; i < failures.length; i++) {
var f = failures[i];
if (f[0]) {
f[0] = f[0] + ' ';
}
results += '==== ' + f[0] + '============================================================\n';
results += '---- input -------\n' + this.prettyprint(f[1]) + '\n';
results += '---- expected ----\n' + this.prettyprint(f[2]) + '\n';
results += '---- output ------\n' + this.prettyprint(f[3]) + '\n';
results += '---- expected-ws ------\n' + this.prettyprint_whitespace(f[2]) + '\n';
results += '---- output-ws ------\n' + this.prettyprint_whitespace(f[3]) + '\n';
results += '================================================================\n\n';
}
results += n_failed + ' tests failed.\n';
}
return results;
};
this.results = function() {
return this.lazy_escape(this.results_raw());
};
this.prettyprint_whitespace = function(something, quote_strings) {
return (this.prettyprint(something, quote_strings)
.replace(/\r\n/g, '\\r\n')
.replace(/\n/g, '\\n\n')
.replace(/\r/g, '\\r\n')
.replace(/ /g, '_')
.replace(/\t/g, '===|'));
};
this.prettyprint = function(something, quote_strings) {
var type = typeof something;
switch (type.toLowerCase()) {
case 'string':
if (quote_strings) {
return "'" + something.replace("'", "\\'") + "'";
}
return something;
case 'number':
return '' + something;
case 'boolean':
return something ? 'true' : 'false';
case 'undefined':
return 'undefined';
case 'object':
if (something instanceof Array) {
var x = [];
var expected_index = 0;
for (var k in something) {
if (k === expected_index) {
x.push(this.prettyprint(something[k], true));
expected_index += 1;
} else {
x.push('\n' + k + ': ' + this.prettyprint(something[k], true));
}
}
return '[' + x.join(', ') + ']';
}
return 'object: ' + something;
default:
return type + ': ' + something;
}
};
this.lazy_escape = function(str) {
return str.replace(/</g, '<').replace(/\>/g, '>').replace(/\n/g, '<br />');
};
this.log = function() {
if (window.console) {
if (console.firebug) {
console.log.apply(console, Array.prototype.slice.call(arguments));
} else {
console.log.call(console, Array.prototype.slice.call(arguments));
}
}
};
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = SanityTest;
}