headless-squirrel 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +55 -0
- data/Rakefile +27 -0
- data/bin/jstest +10 -0
- data/lib/headless_squirrel.rb +2 -0
- data/lib/headless_squirrel/console.rb +24 -0
- data/lib/headless_squirrel/runner.rb +70 -0
- data/lib/headless_squirrel/test.rb +17 -0
- data/lib/headless_squirrel/test_case.rb +158 -0
- data/test/fixtures/a_unit_test.html +27 -0
- data/test/fixtures/a_unit_test.js +22 -0
- data/test/fixtures/syntax_error.html +27 -0
- data/test/fixtures/syntax_error.js +5 -0
- data/test/fixtures/type_error.html +27 -0
- data/test/fixtures/type_error.js +1 -0
- data/test/regression/prototype/unit/ajax_test.html +29 -0
- data/test/regression/prototype/unit/array_test.html +28 -0
- data/test/regression/prototype/unit/assets/prototype.js +4900 -0
- data/test/regression/prototype/unit/assets/unittest.css +50 -0
- data/test/regression/prototype/unit/assets/unittest.js +615 -0
- data/test/regression/prototype/unit/assets/unittest_transport.js +1 -0
- data/test/regression/prototype/unit/base_test.html +27 -0
- data/test/regression/prototype/unit/class_test.html +27 -0
- data/test/regression/prototype/unit/date_test.html +27 -0
- data/test/regression/prototype/unit/dom_test.html +304 -0
- data/test/regression/prototype/unit/element_mixins_test.html +30 -0
- data/test/regression/prototype/unit/enumerable_test.html +35 -0
- data/test/regression/prototype/unit/event_test.html +31 -0
- data/test/regression/prototype/unit/fixtures/ajax.html +2 -0
- data/test/regression/prototype/unit/fixtures/ajax.js +42 -0
- data/test/regression/prototype/unit/fixtures/array.html +1 -0
- data/test/regression/prototype/unit/fixtures/class.js +83 -0
- data/test/regression/prototype/unit/fixtures/content.html +1 -0
- data/test/regression/prototype/unit/fixtures/data.json +1 -0
- data/test/regression/prototype/unit/fixtures/dom.css +84 -0
- data/test/regression/prototype/unit/fixtures/dom.html +278 -0
- data/test/regression/prototype/unit/fixtures/dom.js +17 -0
- data/test/regression/prototype/unit/fixtures/element_mixins.html +4 -0
- data/test/regression/prototype/unit/fixtures/element_mixins.js +2 -0
- data/test/regression/prototype/unit/fixtures/empty.html +0 -0
- data/test/regression/prototype/unit/fixtures/empty.js +1 -0
- data/test/regression/prototype/unit/fixtures/enumerable.html +8 -0
- data/test/regression/prototype/unit/fixtures/enumerable.js +23 -0
- data/test/regression/prototype/unit/fixtures/event.html +4 -0
- data/test/regression/prototype/unit/fixtures/form.html +108 -0
- data/test/regression/prototype/unit/fixtures/function.js +13 -0
- data/test/regression/prototype/unit/fixtures/hash.js +25 -0
- data/test/regression/prototype/unit/fixtures/hello.js +1 -0
- data/test/regression/prototype/unit/fixtures/logo.gif +0 -0
- data/test/regression/prototype/unit/fixtures/object.html +6 -0
- data/test/regression/prototype/unit/fixtures/object.js +7 -0
- data/test/regression/prototype/unit/fixtures/position.html +9 -0
- data/test/regression/prototype/unit/fixtures/selector.html +71 -0
- data/test/regression/prototype/unit/fixtures/string.js +8 -0
- data/test/regression/prototype/unit/fixtures/unittest.html +18 -0
- data/test/regression/prototype/unit/form_test.html +135 -0
- data/test/regression/prototype/unit/function_test.html +27 -0
- data/test/regression/prototype/unit/hash_test.html +27 -0
- data/test/regression/prototype/unit/number_test.html +27 -0
- data/test/regression/prototype/unit/object_test.html +32 -0
- data/test/regression/prototype/unit/periodical_executer_test.html +27 -0
- data/test/regression/prototype/unit/position_test.html +36 -0
- data/test/regression/prototype/unit/prototype_test.html +27 -0
- data/test/regression/prototype/unit/range_test.html +27 -0
- data/test/regression/prototype/unit/regexp_test.html +27 -0
- data/test/regression/prototype/unit/selector_test.html +98 -0
- data/test/regression/prototype/unit/string_test.html +27 -0
- data/test/regression/prototype/unit/tests/ajax_test.js +379 -0
- data/test/regression/prototype/unit/tests/array_test.js +186 -0
- data/test/regression/prototype/unit/tests/base_test.js +43 -0
- data/test/regression/prototype/unit/tests/class_test.js +136 -0
- data/test/regression/prototype/unit/tests/date_test.js +5 -0
- data/test/regression/prototype/unit/tests/dom_test.js +1493 -0
- data/test/regression/prototype/unit/tests/element_mixins_test.js +32 -0
- data/test/regression/prototype/unit/tests/enumerable_test.js +271 -0
- data/test/regression/prototype/unit/tests/event_test.js +235 -0
- data/test/regression/prototype/unit/tests/form_test.js +382 -0
- data/test/regression/prototype/unit/tests/function_test.js +133 -0
- data/test/regression/prototype/unit/tests/hash_test.js +178 -0
- data/test/regression/prototype/unit/tests/number_test.js +44 -0
- data/test/regression/prototype/unit/tests/object_test.js +180 -0
- data/test/regression/prototype/unit/tests/periodical_executer_test.js +15 -0
- data/test/regression/prototype/unit/tests/position_test.js +44 -0
- data/test/regression/prototype/unit/tests/prototype_test.js +43 -0
- data/test/regression/prototype/unit/tests/range_test.js +58 -0
- data/test/regression/prototype/unit/tests/regexp_test.js +42 -0
- data/test/regression/prototype/unit/tests/selector_test.js +408 -0
- data/test/regression/prototype/unit/tests/string_test.js +548 -0
- data/test/regression/prototype/unit/tests/unittest_test.js +148 -0
- data/test/regression/prototype/unit/unittest_test.html +45 -0
- data/test/regression/prototype/upstream +1 -0
- data/test/regression/scriptaculous/unit/_ajax_inplaceeditor_ipce_alt_text.html +1 -0
- data/test/regression/scriptaculous/unit/_ajax_inplaceeditor_ipce_collection.js +1 -0
- data/test/regression/scriptaculous/unit/_ajax_inplaceeditor_result.html +1 -0
- data/test/regression/scriptaculous/unit/_ajax_inplaceeditor_result2.html +1 -0
- data/test/regression/scriptaculous/unit/_ajax_inplaceeditor_tagged.html +1 -0
- data/test/regression/scriptaculous/unit/_ajax_inplaceeditor_text.html +1 -0
- data/test/regression/scriptaculous/unit/_ajax_updater_result.html +20 -0
- data/test/regression/scriptaculous/unit/_autocomplete_result.html +11 -0
- data/test/regression/scriptaculous/unit/_autocomplete_result_nobr.html +1 -0
- data/test/regression/scriptaculous/unit/ajax_autocompleter_test.html +243 -0
- data/test/regression/scriptaculous/unit/ajax_inplaceeditor_test.html +895 -0
- data/test/regression/scriptaculous/unit/assets/builder.js +134 -0
- data/test/regression/scriptaculous/unit/assets/controls.js +963 -0
- data/test/regression/scriptaculous/unit/assets/dragdrop.js +973 -0
- data/test/regression/scriptaculous/unit/assets/effects.js +1122 -0
- data/test/regression/scriptaculous/unit/assets/javascripttest.rb +203 -0
- data/test/regression/scriptaculous/unit/assets/prototype.js +4320 -0
- data/test/regression/scriptaculous/unit/assets/scriptaculous.js +66 -0
- data/test/regression/scriptaculous/unit/assets/slider.js +273 -0
- data/test/regression/scriptaculous/unit/assets/sound.js +57 -0
- data/test/regression/scriptaculous/unit/assets/unittest.js +566 -0
- data/test/regression/scriptaculous/unit/bdd_test.html +150 -0
- data/test/regression/scriptaculous/unit/builder_test.html +262 -0
- data/test/regression/scriptaculous/unit/dragdrop_test.html +131 -0
- data/test/regression/scriptaculous/unit/effects_test.html +547 -0
- data/test/regression/scriptaculous/unit/element_test.html +116 -0
- data/test/regression/scriptaculous/unit/icon.png +0 -0
- data/test/regression/scriptaculous/unit/index.html +70 -0
- data/test/regression/scriptaculous/unit/loading_test.html +41 -0
- data/test/regression/scriptaculous/unit/position_clone_test.html +312 -0
- data/test/regression/scriptaculous/unit/slider_test.html +437 -0
- data/test/regression/scriptaculous/unit/sortable_test.html +205 -0
- data/test/regression/scriptaculous/unit/string_test.html +71 -0
- data/test/regression/scriptaculous/unit/unittest_test.html +154 -0
- data/test/regression/scriptaculous/upstream +1 -0
- data/test/test_helper.rb +20 -0
- data/test/unit/runner_test.rb +165 -0
- data/test/unit/test_case_test.rb +207 -0
- data/test/unit/test_test.rb +24 -0
- metadata +209 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
body, div, p, h1, h2, h3, ul, ol, span, a, table, td, form, img, li {
|
2
|
+
font-family: sans-serif;
|
3
|
+
}
|
4
|
+
|
5
|
+
body {
|
6
|
+
font-size:0.8em;
|
7
|
+
}
|
8
|
+
|
9
|
+
#log {
|
10
|
+
padding-bottom: 1em;
|
11
|
+
border-bottom: 2px solid #000;
|
12
|
+
margin-bottom: 2em;
|
13
|
+
}
|
14
|
+
|
15
|
+
.logsummary {
|
16
|
+
margin-top: 1em;
|
17
|
+
margin-bottom: 1em;
|
18
|
+
padding: 1ex;
|
19
|
+
border: 1px solid #000;
|
20
|
+
font-weight: bold;
|
21
|
+
}
|
22
|
+
|
23
|
+
.logtable {
|
24
|
+
width:100%;
|
25
|
+
border-collapse: collapse;
|
26
|
+
border: 1px dotted #666;
|
27
|
+
}
|
28
|
+
|
29
|
+
.logtable td, .logtable th {
|
30
|
+
text-align: left;
|
31
|
+
padding: 3px 8px;
|
32
|
+
border: 1px dotted #666;
|
33
|
+
}
|
34
|
+
|
35
|
+
.logtable .passed {
|
36
|
+
background-color: #cfc;
|
37
|
+
}
|
38
|
+
|
39
|
+
.logtable .failed, .logtable .error {
|
40
|
+
background-color: #fcc;
|
41
|
+
}
|
42
|
+
|
43
|
+
.logtable td div.action_buttons {
|
44
|
+
display: inline;
|
45
|
+
}
|
46
|
+
|
47
|
+
.logtable td div.action_buttons input {
|
48
|
+
margin: 0 5px;
|
49
|
+
font-size: 10px;
|
50
|
+
}
|
@@ -0,0 +1,615 @@
|
|
1
|
+
// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
|
2
|
+
// (c) 2005 Jon Tirsen (http://www.tirsen.com)
|
3
|
+
// (c) 2005 Michael Schuerig (http://www.schuerig.de/michael/)
|
4
|
+
//
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
// a copy of this software and associated documentation files (the
|
7
|
+
// "Software"), to deal in the Software without restriction, including
|
8
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
// distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
// permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
// the following conditions:
|
12
|
+
//
|
13
|
+
// The above copyright notice and this permission notice shall be
|
14
|
+
// included in all copies or substantial portions of the Software.
|
15
|
+
//
|
16
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
|
25
|
+
// experimental, Firefox-only
|
26
|
+
Event.simulateMouse = function(element, eventName) {
|
27
|
+
var options = Object.extend({
|
28
|
+
pointerX: 0,
|
29
|
+
pointerY: 0,
|
30
|
+
buttons: 0
|
31
|
+
}, arguments[2] || {});
|
32
|
+
var oEvent = document.createEvent("MouseEvents");
|
33
|
+
oEvent.initMouseEvent(eventName, true, true, document.defaultView,
|
34
|
+
options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
|
35
|
+
false, false, false, false, 0, $(element));
|
36
|
+
|
37
|
+
if(this.mark) Element.remove(this.mark);
|
38
|
+
|
39
|
+
var style = 'position: absolute; width: 5px; height: 5px;' +
|
40
|
+
'top: #{pointerY}px; left: #{pointerX}px;'.interpolate(options) +
|
41
|
+
'border-top: 1px solid red; border-left: 1px solid red;'
|
42
|
+
|
43
|
+
this.mark = new Element('div', { style: style });
|
44
|
+
this.mark.appendChild(document.createTextNode(" "));
|
45
|
+
document.body.appendChild(this.mark);
|
46
|
+
|
47
|
+
if(this.step)
|
48
|
+
alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options));
|
49
|
+
|
50
|
+
$(element).dispatchEvent(oEvent);
|
51
|
+
};
|
52
|
+
|
53
|
+
// Note: Due to a fix in Firefox 1.0.5/6 that probably fixed "too much", this doesn't work in 1.0.6 or DP2.
|
54
|
+
// You need to downgrade to 1.0.4 for now to get this working
|
55
|
+
// See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much
|
56
|
+
Event.simulateKey = function(element, eventName) {
|
57
|
+
var options = Object.extend({
|
58
|
+
ctrlKey: false,
|
59
|
+
altKey: false,
|
60
|
+
shiftKey: false,
|
61
|
+
metaKey: false,
|
62
|
+
keyCode: 0,
|
63
|
+
charCode: 0
|
64
|
+
}, arguments[2] || {});
|
65
|
+
|
66
|
+
var oEvent = document.createEvent("KeyEvents");
|
67
|
+
oEvent.initKeyEvent(eventName, true, true, window,
|
68
|
+
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
|
69
|
+
options.keyCode, options.charCode );
|
70
|
+
$(element).dispatchEvent(oEvent);
|
71
|
+
};
|
72
|
+
|
73
|
+
Event.simulateKeys = function(element, command) {
|
74
|
+
for(var i=0; i<command.length; i++) {
|
75
|
+
Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)});
|
76
|
+
}
|
77
|
+
};
|
78
|
+
|
79
|
+
var Test = {
|
80
|
+
Unit: {
|
81
|
+
inspect: Object.inspect // security exception workaround
|
82
|
+
}
|
83
|
+
};
|
84
|
+
|
85
|
+
Test.Unit.Logger = Class.create({
|
86
|
+
initialize: function(id) {
|
87
|
+
if (typeof id === 'string')
|
88
|
+
id = document.getElementById(id);
|
89
|
+
this.element = id;
|
90
|
+
if (this.element) this._createLogTable();
|
91
|
+
this.tbody = this.element.getElementsByTagName('tbody')[0];
|
92
|
+
},
|
93
|
+
|
94
|
+
start: function(testName) {
|
95
|
+
if (!this.element) return;
|
96
|
+
var tr = document.createElement('tr');
|
97
|
+
var first = document.createElement('td');
|
98
|
+
first.appendChild(document.createTextNode(testName));
|
99
|
+
tr.appendChild(first);
|
100
|
+
tr.appendChild(document.createElement('td'));
|
101
|
+
tr.appendChild(document.createElement('td'));
|
102
|
+
this.tbody.appendChild(tr);
|
103
|
+
},
|
104
|
+
|
105
|
+
setStatus: function(status) {
|
106
|
+
this.getLastLogLine().className = status;
|
107
|
+
this.getLastLogLine().getElementsByTagName('td')[1].innerHTML = status;
|
108
|
+
},
|
109
|
+
|
110
|
+
finish: function(status, summary) {
|
111
|
+
if (!this.element) return;
|
112
|
+
this.setStatus(status);
|
113
|
+
this.message(summary);
|
114
|
+
},
|
115
|
+
|
116
|
+
message: function(message) {
|
117
|
+
if (!this.element) return;
|
118
|
+
this.getMessageCell().innerHTML = this._toHTML(message);
|
119
|
+
},
|
120
|
+
|
121
|
+
summary: function(summary) {
|
122
|
+
if (!this.element) return;
|
123
|
+
this.element.getElementsByTagName('div')[0].innerHTML = this._toHTML(summary);
|
124
|
+
},
|
125
|
+
|
126
|
+
getLastLogLine: function() {
|
127
|
+
var trs = this.element.getElementsByTagName('tr');
|
128
|
+
return trs[trs.length - 1];
|
129
|
+
},
|
130
|
+
|
131
|
+
getMessageCell: function() {
|
132
|
+
return this.getLastLogLine().getElementsByTagName('td')[2];
|
133
|
+
},
|
134
|
+
|
135
|
+
_createLogTable: function() {
|
136
|
+
var html = '<div class="logsummary">running...</div>' +
|
137
|
+
'<table class="logtable">' +
|
138
|
+
'<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' +
|
139
|
+
'<tbody class="loglines"></tbody>' +
|
140
|
+
'</table>';
|
141
|
+
this.element.innerHTML = html;
|
142
|
+
|
143
|
+
},
|
144
|
+
|
145
|
+
appendActionButtons: function(actions) {
|
146
|
+
/* actions = $H(actions);
|
147
|
+
if (!actions.any()) return;
|
148
|
+
var div = new Element("div", {className: 'action_buttons'});
|
149
|
+
actions.inject(div, function(container, action) {
|
150
|
+
var button = new Element("input").setValue(action.key).observe("click", action.value);
|
151
|
+
button.type = "button";
|
152
|
+
return container.insert(button);
|
153
|
+
});
|
154
|
+
this.getMessageCell().insert(div); */
|
155
|
+
},
|
156
|
+
|
157
|
+
_toHTML: function(txt) {
|
158
|
+
return txt.escapeHTML().replace(/\n/g,"<br />");
|
159
|
+
}
|
160
|
+
});
|
161
|
+
|
162
|
+
Test.Unit.runners = [];
|
163
|
+
Test.Unit.run = true;
|
164
|
+
Test.Unit.AutoRunner = {
|
165
|
+
run: function() {
|
166
|
+
if (!Test.Unit.run) return;
|
167
|
+
Test.Unit.run = false;
|
168
|
+
for (var i=0; i < Test.Unit.runners.length; i++) {
|
169
|
+
Test.Unit.runners[i].run();
|
170
|
+
};
|
171
|
+
}
|
172
|
+
};
|
173
|
+
Event.observe(window, "load", Test.Unit.AutoRunner.run);
|
174
|
+
|
175
|
+
Test.Unit.Runner = Class.create({
|
176
|
+
initialize: function(testcases) {
|
177
|
+
var options = this.options = Object.extend({
|
178
|
+
testLog: 'testlog'
|
179
|
+
}, arguments[1] || {});
|
180
|
+
|
181
|
+
options.resultsURL = this.queryParams.resultsURL;
|
182
|
+
|
183
|
+
this.tests = this.getTests(testcases);
|
184
|
+
this.currentTest = 0;
|
185
|
+
Test.Unit.runners.push(this);
|
186
|
+
},
|
187
|
+
|
188
|
+
run: function() {
|
189
|
+
this.logger = new Test.Unit.Logger(this.options.testLog);
|
190
|
+
this.runTests.bind(this).delay(0.1);
|
191
|
+
},
|
192
|
+
|
193
|
+
queryParams: window.location.search.parseQuery(),
|
194
|
+
|
195
|
+
getTests: function(testcases) {
|
196
|
+
var tests, options = this.options;
|
197
|
+
if (this.queryParams.tests) tests = this.queryParams.tests.split(',');
|
198
|
+
else if (options.tests) tests = options.tests;
|
199
|
+
else if (options.test) tests = [option.test];
|
200
|
+
else tests = Object.keys(testcases).grep(/^test/);
|
201
|
+
|
202
|
+
return tests.map(function(test) {
|
203
|
+
if (testcases[test])
|
204
|
+
return new Test.Unit.Testcase(test, testcases[test], testcases.setup, testcases.teardown);
|
205
|
+
}).compact();
|
206
|
+
},
|
207
|
+
|
208
|
+
getResult: function() {
|
209
|
+
var results = {
|
210
|
+
tests: this.tests.length,
|
211
|
+
assertions: 0,
|
212
|
+
failures: 0,
|
213
|
+
errors: 0
|
214
|
+
};
|
215
|
+
|
216
|
+
return this.tests.inject(results, function(results, test) {
|
217
|
+
results.assertions += test.assertions;
|
218
|
+
results.failures += test.failures;
|
219
|
+
results.errors += test.errors;
|
220
|
+
return results;
|
221
|
+
});
|
222
|
+
},
|
223
|
+
|
224
|
+
postResults: function() {
|
225
|
+
if (window.postUnittestResults) {
|
226
|
+
window.postUnittestResults(this.getResult());
|
227
|
+
} else if (this.options.resultsURL) {
|
228
|
+
new Ajax.Request(this.options.resultsURL,
|
229
|
+
{ method: 'get', parameters: this.getResult(), asynchronous: false });
|
230
|
+
}
|
231
|
+
},
|
232
|
+
|
233
|
+
runTests: function() {
|
234
|
+
var test = this.tests[this.currentTest], actions;
|
235
|
+
|
236
|
+
if (!test) return this.finish();
|
237
|
+
if (!test.isWaiting) this.logger.start(test.name);
|
238
|
+
test.run();
|
239
|
+
if(test.isWaiting) {
|
240
|
+
this.logger.message("Waiting for " + test.timeToWait + "ms");
|
241
|
+
setTimeout(this.runTests.bind(this), test.timeToWait || 1000);
|
242
|
+
return;
|
243
|
+
}
|
244
|
+
|
245
|
+
this.logger.finish(test.status(), test.summary());
|
246
|
+
if (actions = test.actions) this.logger.appendActionButtons(actions);
|
247
|
+
this.currentTest++;
|
248
|
+
// tail recursive, hopefully the browser will skip the stackframe
|
249
|
+
this.runTests();
|
250
|
+
},
|
251
|
+
|
252
|
+
finish: function() {
|
253
|
+
this.postResults();
|
254
|
+
this.logger.summary(this.summary());
|
255
|
+
},
|
256
|
+
|
257
|
+
summary: function() {
|
258
|
+
return '#{tests} tests, #{assertions} assertions, #{failures} failures, #{errors} errors'
|
259
|
+
.interpolate(this.getResult());
|
260
|
+
}
|
261
|
+
});
|
262
|
+
|
263
|
+
Test.Unit.MessageTemplate = Class.create({
|
264
|
+
initialize: function(string) {
|
265
|
+
var parts = [];
|
266
|
+
(string || '').scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/, function(part) {
|
267
|
+
parts.push(part[0]);
|
268
|
+
});
|
269
|
+
this.parts = parts;
|
270
|
+
},
|
271
|
+
|
272
|
+
evaluate: function(params) {
|
273
|
+
return this.parts.map(function(part) {
|
274
|
+
return part == '?' ? Test.Unit.inspect(params.shift()) : part.replace(/\\\?/, '?');
|
275
|
+
}).join('');
|
276
|
+
}
|
277
|
+
});
|
278
|
+
|
279
|
+
Test.Unit.Assertions = (function() {
|
280
|
+
var MessageTemplate = Test.Unit.MessageTemplate;
|
281
|
+
|
282
|
+
function buildMessage(message, template) {
|
283
|
+
var args = Array.prototype.slice.call(arguments, 2);
|
284
|
+
return (message ? message + '\n' : '') + new MessageTemplate(template).evaluate(args);
|
285
|
+
}
|
286
|
+
|
287
|
+
function flunk(message) {
|
288
|
+
this.assertBlock(message || 'Flunked', function() { return false });
|
289
|
+
}
|
290
|
+
|
291
|
+
function assertBlock(message, block) {
|
292
|
+
try {
|
293
|
+
block.call(this) ? this.pass() : this.fail(message);
|
294
|
+
} catch(e) { this.error(e) }
|
295
|
+
}
|
296
|
+
|
297
|
+
function assert(expression, message) {
|
298
|
+
message = buildMessage(message || 'assert', 'got <?>', expression);
|
299
|
+
this.assertBlock(message, function() { return expression });
|
300
|
+
}
|
301
|
+
|
302
|
+
function assertEqual(expected, actual, message) {
|
303
|
+
message = buildMessage(message || 'assertEqual', 'expected: <?>, actual: <?>', expected, actual);
|
304
|
+
this.assertBlock(message, function() { return expected == actual });
|
305
|
+
}
|
306
|
+
|
307
|
+
function assertNotEqual(expected, actual, message) {
|
308
|
+
message = buildMessage(message || 'assertNotEqual', 'expected: <?>, actual: <?>', expected, actual);
|
309
|
+
this.assertBlock(message, function() { return expected != actual });
|
310
|
+
}
|
311
|
+
|
312
|
+
function assertEnumEqual(expected, actual, message) {
|
313
|
+
expected = $A(expected);
|
314
|
+
actual = $A(actual);
|
315
|
+
message = buildMessage(message || 'assertEnumEqual', 'expected: <?>, actual: <?>', expected, actual);
|
316
|
+
this.assertBlock(message, function() {
|
317
|
+
return expected.length == actual.length && expected.zip(actual).all(function(pair) { return pair[0] == pair[1] });
|
318
|
+
});
|
319
|
+
}
|
320
|
+
|
321
|
+
function assertEnumNotEqual(expected, actual, message) {
|
322
|
+
expected = $A(expected);
|
323
|
+
actual = $A(actual);
|
324
|
+
message = buildMessage(message || 'assertEnumNotEqual', '<?> was the same as <?>', expected, actual);
|
325
|
+
this.assertBlock(message, function() {
|
326
|
+
return expected.length != actual.length || expected.zip(actual).any(function(pair) { return pair[0] != pair[1] });
|
327
|
+
});
|
328
|
+
}
|
329
|
+
|
330
|
+
function assertPairEqual(pair) {
|
331
|
+
return pair.all(Object.isArray) ?
|
332
|
+
pair[0].zip(pair[1]).all(assertPairEqual) : pair[0] == pair[1];
|
333
|
+
}
|
334
|
+
|
335
|
+
function assertHashEqual(expected, actual, message) {
|
336
|
+
expected = $H(expected);
|
337
|
+
actual = $H(actual);
|
338
|
+
var expected_array = expected.toArray().sort(), actual_array = actual.toArray().sort();
|
339
|
+
message = buildMessage(message || 'assertHashEqual', 'expected: <?>, actual: <?>', expected, actual);
|
340
|
+
// from now we recursively zip & compare nested arrays
|
341
|
+
function block() {
|
342
|
+
return expected_array.length == actual_array.length &&
|
343
|
+
expected_array.zip(actual_array).all(assertPairEqual);
|
344
|
+
}
|
345
|
+
this.assertBlock(message, block);
|
346
|
+
}
|
347
|
+
|
348
|
+
function assertHashNotEqual(expected, actual, message) {
|
349
|
+
expected = $H(expected);
|
350
|
+
actual = $H(actual);
|
351
|
+
var expected_array = expected.toArray().sort(), actual_array = actual.toArray().sort();
|
352
|
+
message = buildMessage(message || 'assertHashNotEqual', '<?> was the same as <?>', expected, actual);
|
353
|
+
// from now we recursively zip & compare nested arrays
|
354
|
+
function block() {
|
355
|
+
return !(expected_array.length == actual_array.length &&
|
356
|
+
expected_array.zip(actual_array).all(assertPairEqual));
|
357
|
+
};
|
358
|
+
this.assertBlock(message, block);
|
359
|
+
}
|
360
|
+
|
361
|
+
function assertIdentical(expected, actual, message) {
|
362
|
+
message = buildMessage(message || 'assertIdentical', 'expected: <?>, actual: <?>', expected, actual);
|
363
|
+
this.assertBlock(message, function() { return expected === actual });
|
364
|
+
}
|
365
|
+
|
366
|
+
function assertNotIdentical(expected, actual, message) {
|
367
|
+
message = buildMessage(message || 'assertNotIdentical', 'expected: <?>, actual: <?>', expected, actual);
|
368
|
+
this.assertBlock(message, function() { return expected !== actual });
|
369
|
+
}
|
370
|
+
|
371
|
+
function assertNull(obj, message) {
|
372
|
+
message = buildMessage(message || 'assertNull', 'got <?>', obj);
|
373
|
+
this.assertBlock(message, function() { return obj === null });
|
374
|
+
}
|
375
|
+
|
376
|
+
function assertNotNull(obj, message) {
|
377
|
+
message = buildMessage(message || 'assertNotNull', 'got <?>', obj);
|
378
|
+
this.assertBlock(message, function() { return obj !== null });
|
379
|
+
}
|
380
|
+
|
381
|
+
function assertUndefined(obj, message) {
|
382
|
+
message = buildMessage(message || 'assertUndefined', 'got <?>', obj);
|
383
|
+
this.assertBlock(message, function() { return typeof obj == "undefined" });
|
384
|
+
}
|
385
|
+
|
386
|
+
function assertNotUndefined(obj, message) {
|
387
|
+
message = buildMessage(message || 'assertNotUndefined', 'got <?>', obj);
|
388
|
+
this.assertBlock(message, function() { return typeof obj != "undefined" });
|
389
|
+
}
|
390
|
+
|
391
|
+
function assertNullOrUndefined(obj, message) {
|
392
|
+
message = buildMessage(message || 'assertNullOrUndefined', 'got <?>', obj);
|
393
|
+
this.assertBlock(message, function() { return obj == null });
|
394
|
+
}
|
395
|
+
|
396
|
+
function assertNotNullOrUndefined(obj, message) {
|
397
|
+
message = buildMessage(message || 'assertNotNullOrUndefined', 'got <?>', obj);
|
398
|
+
this.assertBlock(message, function() { return obj != null });
|
399
|
+
}
|
400
|
+
|
401
|
+
function assertMatch(expected, actual, message) {
|
402
|
+
message = buildMessage(message || 'assertMatch', 'regex <?> did not match <?>', expected, actual);
|
403
|
+
this.assertBlock(message, function() { return new RegExp(expected).exec(actual) });
|
404
|
+
}
|
405
|
+
|
406
|
+
function assertNoMatch(expected, actual, message) {
|
407
|
+
message = buildMessage(message || 'assertNoMatch', 'regex <?> matched <?>', expected, actual);
|
408
|
+
this.assertBlock(message, function() { return !(new RegExp(expected).exec(actual)) });
|
409
|
+
}
|
410
|
+
|
411
|
+
function assertHidden(element, message) {
|
412
|
+
message = buildMessage(message || 'assertHidden', '? isn\'t hidden.', element);
|
413
|
+
this.assertBlock(message, function() { return element.style.display == 'none' });
|
414
|
+
}
|
415
|
+
|
416
|
+
function assertInstanceOf(expected, actual, message) {
|
417
|
+
message = buildMessage(message || 'assertInstanceOf', '<?> was not an instance of the expected type', actual);
|
418
|
+
this.assertBlock(message, function() { return actual instanceof expected });
|
419
|
+
}
|
420
|
+
|
421
|
+
function assertNotInstanceOf(expected, actual, message) {
|
422
|
+
message = buildMessage(message || 'assertNotInstanceOf', '<?> was an instance of the expected type', actual);
|
423
|
+
this.assertBlock(message, function() { return !(actual instanceof expected) });
|
424
|
+
}
|
425
|
+
|
426
|
+
function assertRespondsTo(method, obj, message) {
|
427
|
+
message = buildMessage(message || 'assertRespondsTo', 'object doesn\'t respond to <?>', method);
|
428
|
+
this.assertBlock(message, function() { return (method in obj && typeof obj[method] == 'function') });
|
429
|
+
}
|
430
|
+
|
431
|
+
function assertRaise(exceptionName, method, message) {
|
432
|
+
message = buildMessage(message || 'assertRaise', '<?> exception expected but none was raised', exceptionName);
|
433
|
+
var block = function() {
|
434
|
+
try {
|
435
|
+
method();
|
436
|
+
return false;
|
437
|
+
} catch(e) {
|
438
|
+
if (e.name == exceptionName) return true;
|
439
|
+
else throw e;
|
440
|
+
}
|
441
|
+
};
|
442
|
+
this.assertBlock(message, block);
|
443
|
+
}
|
444
|
+
|
445
|
+
function assertNothingRaised(method, message) {
|
446
|
+
try {
|
447
|
+
method();
|
448
|
+
this.assert(true, "Expected nothing to be thrown");
|
449
|
+
} catch(e) {
|
450
|
+
message = buildMessage(message || 'assertNothingRaised', '<?> was thrown when nothing was expected.', e);
|
451
|
+
this.flunk(message);
|
452
|
+
}
|
453
|
+
}
|
454
|
+
|
455
|
+
function isVisible(element) {
|
456
|
+
element = $(element);
|
457
|
+
if(!element.parentNode) return true;
|
458
|
+
this.assertNotNull(element);
|
459
|
+
if(element.style && Element.getStyle(element, 'display') == 'none')
|
460
|
+
return false;
|
461
|
+
return isVisible.call(this, element.parentNode);
|
462
|
+
}
|
463
|
+
|
464
|
+
function assertVisible(element, message) {
|
465
|
+
message = buildMessage(message, '? was not visible.', element);
|
466
|
+
this.assertBlock(message, function() { return isVisible.call(this, element) });
|
467
|
+
}
|
468
|
+
|
469
|
+
function assertNotVisible(element, message) {
|
470
|
+
message = buildMessage(message, '? was not hidden and didn\'t have a hidden parent either.', element);
|
471
|
+
this.assertBlock(message, function() { return !isVisible.call(this, element) });
|
472
|
+
}
|
473
|
+
|
474
|
+
function assertElementsMatch() {
|
475
|
+
var message, pass = true, expressions = $A(arguments), elements = $A(expressions.shift());
|
476
|
+
if (elements.length != expressions.length) {
|
477
|
+
message = buildMessage('assertElementsMatch', 'size mismatch: ? elements, ? expressions (?).', elements.length, expressions.length, expressions);
|
478
|
+
this.flunk(message);
|
479
|
+
pass = false;
|
480
|
+
}
|
481
|
+
elements.zip(expressions).all(function(pair, index) {
|
482
|
+
var element = $(pair.first()), expression = pair.last();
|
483
|
+
if (element.match(expression)) return true;
|
484
|
+
message = buildMessage('assertElementsMatch', 'In index <?>: expected <?> but got ?', index, expression, element);
|
485
|
+
this.flunk(message);
|
486
|
+
pass = false;
|
487
|
+
}.bind(this))
|
488
|
+
|
489
|
+
if (pass) this.assert(true, "Expected all elements to match.");
|
490
|
+
}
|
491
|
+
|
492
|
+
function assertElementMatches(element, expression, message) {
|
493
|
+
this.assertElementsMatch([element], expression);
|
494
|
+
}
|
495
|
+
|
496
|
+
return {
|
497
|
+
buildMessage: buildMessage,
|
498
|
+
flunk: flunk,
|
499
|
+
assertBlock: assertBlock,
|
500
|
+
assert: assert,
|
501
|
+
assertEqual: assertEqual,
|
502
|
+
assertNotEqual: assertNotEqual,
|
503
|
+
assertEnumEqual: assertEnumEqual,
|
504
|
+
assertEnumNotEqual: assertEnumNotEqual,
|
505
|
+
assertHashEqual: assertHashEqual,
|
506
|
+
assertHashNotEqual: assertHashNotEqual,
|
507
|
+
assertIdentical: assertIdentical,
|
508
|
+
assertNotIdentical: assertNotIdentical,
|
509
|
+
assertNull: assertNull,
|
510
|
+
assertNotNull: assertNotNull,
|
511
|
+
assertUndefined: assertUndefined,
|
512
|
+
assertNotUndefined: assertNotUndefined,
|
513
|
+
assertNullOrUndefined: assertNullOrUndefined,
|
514
|
+
assertNotNullOrUndefined: assertNotNullOrUndefined,
|
515
|
+
assertMatch: assertMatch,
|
516
|
+
assertNoMatch: assertNoMatch,
|
517
|
+
assertHidden: assertHidden,
|
518
|
+
assertInstanceOf: assertInstanceOf,
|
519
|
+
assertNotInstanceOf: assertNotInstanceOf,
|
520
|
+
assertRespondsTo: assertRespondsTo,
|
521
|
+
assertRaise: assertRaise,
|
522
|
+
assertNothingRaised: assertNothingRaised,
|
523
|
+
assertVisible: assertVisible,
|
524
|
+
assertNotVisible: assertNotVisible,
|
525
|
+
assertElementsMatch: assertElementsMatch,
|
526
|
+
assertElementMatches: assertElementMatches
|
527
|
+
}
|
528
|
+
})();
|
529
|
+
|
530
|
+
Test.Unit.Testcase = Class.create(Test.Unit.Assertions, {
|
531
|
+
initialize: function(name, test, setup, teardown) {
|
532
|
+
this.name = name;
|
533
|
+
this.test = test || Prototype.emptyFunction;
|
534
|
+
this.setup = setup || Prototype.emptyFunction;
|
535
|
+
this.teardown = teardown || Prototype.emptyFunction;
|
536
|
+
this.messages = [];
|
537
|
+
this.actions = {};
|
538
|
+
},
|
539
|
+
|
540
|
+
isWaiting: false,
|
541
|
+
timeToWait: 1000,
|
542
|
+
assertions: 0,
|
543
|
+
failures: 0,
|
544
|
+
errors: 0,
|
545
|
+
isRunningFromRake: window.location.port == 4711,
|
546
|
+
|
547
|
+
wait: function(time, nextPart) {
|
548
|
+
this.isWaiting = true;
|
549
|
+
this.test = nextPart;
|
550
|
+
this.timeToWait = time;
|
551
|
+
},
|
552
|
+
|
553
|
+
run: function(rethrow) {
|
554
|
+
try {
|
555
|
+
try {
|
556
|
+
if (!this.isWaiting) this.setup();
|
557
|
+
this.isWaiting = false;
|
558
|
+
this.test();
|
559
|
+
} finally {
|
560
|
+
if(!this.isWaiting) {
|
561
|
+
this.teardown();
|
562
|
+
}
|
563
|
+
}
|
564
|
+
}
|
565
|
+
catch(e) {
|
566
|
+
if (rethrow) throw e;
|
567
|
+
this.error(e, this);
|
568
|
+
}
|
569
|
+
},
|
570
|
+
|
571
|
+
summary: function() {
|
572
|
+
var msg = '#{assertions} assertions, #{failures} failures, #{errors} errors\n';
|
573
|
+
return msg.interpolate(this) + this.messages.join("\n");
|
574
|
+
},
|
575
|
+
|
576
|
+
pass: function() {
|
577
|
+
this.assertions++;
|
578
|
+
},
|
579
|
+
|
580
|
+
fail: function(message) {
|
581
|
+
this.failures++;
|
582
|
+
var line = "";
|
583
|
+
try {
|
584
|
+
throw new Error("stack");
|
585
|
+
} catch(e){
|
586
|
+
line = (/\.html:(\d+)/.exec(e.stack || '') || ['',''])[1];
|
587
|
+
}
|
588
|
+
this.messages.push("Failure: " + message + (line ? " Line #" + line : ""));
|
589
|
+
},
|
590
|
+
|
591
|
+
info: function(message) {
|
592
|
+
this.messages.push("Info: " + message);
|
593
|
+
},
|
594
|
+
|
595
|
+
error: function(error, test) {
|
596
|
+
this.errors++;
|
597
|
+
this.actions['retry with throw'] = function() { test.run(true) };
|
598
|
+
this.messages.push(error.name + ": "+ error.message + ", error=(" + Test.Unit.inspect(error) + ")");
|
599
|
+
},
|
600
|
+
|
601
|
+
status: function() {
|
602
|
+
if (this.failures > 0) return 'failed';
|
603
|
+
if (this.errors > 0) return 'error';
|
604
|
+
return 'passed';
|
605
|
+
},
|
606
|
+
|
607
|
+
benchmark: function(operation, iterations) {
|
608
|
+
var startAt = new Date();
|
609
|
+
(iterations || 1).times(operation);
|
610
|
+
var timeTaken = ((new Date())-startAt);
|
611
|
+
this.info((arguments[2] || 'Operation') + ' finished ' +
|
612
|
+
iterations + ' iterations in ' + (timeTaken/1000)+'s' );
|
613
|
+
return timeTaken;
|
614
|
+
}
|
615
|
+
});
|