corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 //
2 // simple testing interface
3 // written by Einar Lielmanis, einar@jsbeautifier.org
4 //
5 // usage:
6 //
7 // var t = new SanityTest(function (x) { return x; }, 'my function');
8 // t.expect('input', 'output');
9 // t.expect('a', 'a');
10 // output_somewhere(t.results()); // good for <pre>, html safe-ish
11 // alert(t.results_raw()); // html unescaped
12  
13  
14 function SanityTest(func, name_of_test) {
15  
16 var test_func = func || function(x) {
17 return x;
18 };
19  
20 var test_name = name_of_test || '';
21  
22 var n_failed = 0;
23 var n_succeeded = 0;
24  
25 var failures = [];
26  
27 this.test_function = function(func, name) {
28 test_func = func;
29 test_name = name || '';
30 };
31  
32 this.get_exitcode = function() {
33 return n_succeeded === 0 || n_failed !== 0 ? 1 : 0;
34 };
35  
36 this.expect = function(parameters, expected_value) {
37 // multi-parameter calls not supported (I don't need them now).
38 var result = test_func(parameters);
39 // proper array checking is a pain. i'll maybe do it later, compare strings representations instead
40 if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') === expected_value.join(', '))) {
41 n_succeeded += 1;
42 } else {
43 n_failed += 1;
44 failures.push([test_name, parameters, expected_value, result]);
45 }
46 };
47  
48  
49 this.results_raw = function() {
50 var results = '';
51 if (n_failed === 0) {
52 if (n_succeeded === 0) {
53 results = 'No tests run.';
54 } else {
55 results = 'All ' + n_succeeded + ' tests passed.';
56 }
57 } else {
58 for (var i = 0; i < failures.length; i++) {
59 var f = failures[i];
60 if (f[0]) {
61 f[0] = f[0] + ' ';
62 }
63 results += '==== ' + f[0] + '============================================================\n';
64 results += '---- input -------\n' + this.prettyprint(f[1]) + '\n';
65 results += '---- expected ----\n' + this.prettyprint(f[2]) + '\n';
66 results += '---- output ------\n' + this.prettyprint(f[3]) + '\n';
67 results += '---- expected-ws ------\n' + this.prettyprint_whitespace(f[2]) + '\n';
68 results += '---- output-ws ------\n' + this.prettyprint_whitespace(f[3]) + '\n';
69 results += '================================================================\n\n';
70 }
71 results += n_failed + ' tests failed.\n';
72 }
73 return results;
74 };
75  
76  
77 this.results = function() {
78 return this.lazy_escape(this.results_raw());
79 };
80  
81 this.prettyprint_whitespace = function(something, quote_strings) {
82 return (this.prettyprint(something, quote_strings)
83 .replace(/\r\n/g, '\\r\n')
84 .replace(/\n/g, '\\n\n')
85 .replace(/\r/g, '\\r\n')
86 .replace(/ /g, '_')
87 .replace(/\t/g, '===|'));
88 };
89  
90 this.prettyprint = function(something, quote_strings) {
91 var type = typeof something;
92 switch (type.toLowerCase()) {
93 case 'string':
94 if (quote_strings) {
95 return "'" + something.replace("'", "\\'") + "'";
96 }
97 return something;
98 case 'number':
99 return '' + something;
100 case 'boolean':
101 return something ? 'true' : 'false';
102 case 'undefined':
103 return 'undefined';
104 case 'object':
105 if (something instanceof Array) {
106 var x = [];
107 var expected_index = 0;
108 for (var k in something) {
109 if (k === expected_index) {
110 x.push(this.prettyprint(something[k], true));
111 expected_index += 1;
112 } else {
113 x.push('\n' + k + ': ' + this.prettyprint(something[k], true));
114 }
115 }
116 return '[' + x.join(', ') + ']';
117 }
118 return 'object: ' + something;
119 default:
120 return type + ': ' + something;
121 }
122 };
123  
124  
125 this.lazy_escape = function(str) {
126 return str.replace(/g, '&lt;').replace(/\>/g, '&gt;').replace(/\n/g, '<br />');
127 };
128  
129  
130 this.log = function() {
131 if (window.console) {
132 if (console.firebug) {
133 console.log.apply(console, Array.prototype.slice.call(arguments));
134 } else {
135 console.log.call(console, Array.prototype.slice.call(arguments));
136 }
137 }
138 };
139  
140 }
141  
142 if (typeof module !== 'undefined' && module.exports) {
143 module.exports = SanityTest;
144 }