]> git.sur5r.net Git - contagged/blob - js/unittest.js
PHP5 notice cleanups
[contagged] / js / unittest.js
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 // See scriptaculous.js for full license.
6
7 // experimental, Firefox-only
8 Event.simulateMouse = function(element, eventName) {
9   var options = Object.extend({
10     pointerX: 0,
11     pointerY: 0,
12     buttons: 0
13   }, arguments[2] || {});
14   var oEvent = document.createEvent("MouseEvents");
15   oEvent.initMouseEvent(eventName, true, true, document.defaultView, 
16     options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY, 
17     false, false, false, false, 0, $(element));
18   
19   if(this.mark) Element.remove(this.mark);
20   this.mark = document.createElement('div');
21   this.mark.appendChild(document.createTextNode(" "));
22   document.body.appendChild(this.mark);
23   this.mark.style.position = 'absolute';
24   this.mark.style.top = options.pointerY + "px";
25   this.mark.style.left = options.pointerX + "px";
26   this.mark.style.width = "5px";
27   this.mark.style.height = "5px;";
28   this.mark.style.borderTop = "1px solid red;"
29   this.mark.style.borderLeft = "1px solid red;"
30   
31   if(this.step)
32     alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options));
33   
34   $(element).dispatchEvent(oEvent);
35 };
36
37 // 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.
38 // You need to downgrade to 1.0.4 for now to get this working
39 // See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much
40 Event.simulateKey = function(element, eventName) {
41   var options = Object.extend({
42     ctrlKey: false,
43     altKey: false,
44     shiftKey: false,
45     metaKey: false,
46     keyCode: 0,
47     charCode: 0
48   }, arguments[2] || {});
49
50   var oEvent = document.createEvent("KeyEvents");
51   oEvent.initKeyEvent(eventName, true, true, window, 
52     options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
53     options.keyCode, options.charCode );
54   $(element).dispatchEvent(oEvent);
55 };
56
57 Event.simulateKeys = function(element, command) {
58   for(var i=0; i<command.length; i++) {
59     Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)});
60   }
61 };
62
63 var Test = {}
64 Test.Unit = {};
65
66 // security exception workaround
67 Test.Unit.inspect = function(obj) {
68   var info = [];
69
70   if(typeof obj=="string" || 
71      typeof obj=="number") {
72     return obj;
73   } else {
74     for(property in obj)
75       if(typeof obj[property]!="function")
76         info.push(property + ' => ' + 
77           (typeof obj[property] == "string" ?
78             '"' + obj[property] + '"' :
79             obj[property]));
80   }
81
82   return ("'" + obj + "' #" + typeof obj + 
83     ": {" + info.join(", ") + "}");
84 }
85
86 Test.Unit.Logger = Class.create();
87 Test.Unit.Logger.prototype = {
88   initialize: function(log) {
89     this.log = $(log);
90     if (this.log) {
91       this._createLogTable();
92     }
93   },
94   start: function(testName) {
95     if (!this.log) return;
96     this.testName = testName;
97     this.lastLogLine = document.createElement('tr');
98     this.statusCell = document.createElement('td');
99     this.nameCell = document.createElement('td');
100     this.nameCell.appendChild(document.createTextNode(testName));
101     this.messageCell = document.createElement('td');
102     this.lastLogLine.appendChild(this.statusCell);
103     this.lastLogLine.appendChild(this.nameCell);
104     this.lastLogLine.appendChild(this.messageCell);
105     this.loglines.appendChild(this.lastLogLine);
106   },
107   finish: function(status, summary) {
108     if (!this.log) return;
109     this.lastLogLine.className = status;
110     this.statusCell.innerHTML = status;
111     this.messageCell.innerHTML = this._toHTML(summary);
112   },
113   message: function(message) {
114     if (!this.log) return;
115     this.messageCell.innerHTML = this._toHTML(message);
116   },
117   summary: function(summary) {
118     if (!this.log) return;
119     this.logsummary.innerHTML = this._toHTML(summary);
120   },
121   _createLogTable: function() {
122     this.log.innerHTML =
123     '<div id="logsummary"></div>' +
124     '<table id="logtable">' +
125     '<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' +
126     '<tbody id="loglines"></tbody>' +
127     '</table>';
128     this.logsummary = $('logsummary')
129     this.loglines = $('loglines');
130   },
131   _toHTML: function(txt) {
132     return txt.escapeHTML().replace(/\n/g,"<br/>");
133   }
134 }
135
136 Test.Unit.Runner = Class.create();
137 Test.Unit.Runner.prototype = {
138   initialize: function(testcases) {
139     this.options = Object.extend({
140       testLog: 'testlog'
141     }, arguments[1] || {});
142     this.options.resultsURL = this.parseResultsURLQueryParameter();
143     if (this.options.testLog) {
144       this.options.testLog = $(this.options.testLog) || null;
145     }
146     if(this.options.tests) {
147       this.tests = [];
148       for(var i = 0; i < this.options.tests.length; i++) {
149         if(/^test/.test(this.options.tests[i])) {
150           this.tests.push(new Test.Unit.Testcase(this.options.tests[i], testcases[this.options.tests[i]], testcases["setup"], testcases["teardown"]));
151         }
152       }
153     } else {
154       if (this.options.test) {
155         this.tests = [new Test.Unit.Testcase(this.options.test, testcases[this.options.test], testcases["setup"], testcases["teardown"])];
156       } else {
157         this.tests = [];
158         for(var testcase in testcases) {
159           if(/^test/.test(testcase)) {
160             this.tests.push(new Test.Unit.Testcase(testcase, testcases[testcase], testcases["setup"], testcases["teardown"]));
161           }
162         }
163       }
164     }
165     this.currentTest = 0;
166     this.logger = new Test.Unit.Logger(this.options.testLog);
167     setTimeout(this.runTests.bind(this), 1000);
168   },
169   parseResultsURLQueryParameter: function() {
170     return window.location.search.parseQuery()["resultsURL"];
171   },
172   // Returns:
173   //  "ERROR" if there was an error,
174   //  "FAILURE" if there was a failure, or
175   //  "SUCCESS" if there was neither
176   getResult: function() {
177     var hasFailure = false;
178     for(var i=0;i<this.tests.length;i++) {
179       if (this.tests[i].errors > 0) {
180         return "ERROR";
181       }
182       if (this.tests[i].failures > 0) {
183         hasFailure = true;
184       }
185     }
186     if (hasFailure) {
187       return "FAILURE";
188     } else {
189       return "SUCCESS";
190     }
191   },
192   postResults: function() {
193     if (this.options.resultsURL) {
194       new Ajax.Request(this.options.resultsURL, 
195         { method: 'get', parameters: 'result=' + this.getResult(), asynchronous: false });
196     }
197   },
198   runTests: function() {
199     var test = this.tests[this.currentTest];
200     if (!test) {
201       // finished!
202       this.postResults();
203       this.logger.summary(this.summary());
204       return;
205     }
206     if(!test.isWaiting) {
207       this.logger.start(test.name);
208     }
209     test.run();
210     if(test.isWaiting) {
211       this.logger.message("Waiting for " + test.timeToWait + "ms");
212       setTimeout(this.runTests.bind(this), test.timeToWait || 1000);
213     } else {
214       this.logger.finish(test.status(), test.summary());
215       this.currentTest++;
216       // tail recursive, hopefully the browser will skip the stackframe
217       this.runTests();
218     }
219   },
220   summary: function() {
221     var assertions = 0;
222     var failures = 0;
223     var errors = 0;
224     var messages = [];
225     for(var i=0;i<this.tests.length;i++) {
226       assertions +=   this.tests[i].assertions;
227       failures   +=   this.tests[i].failures;
228       errors     +=   this.tests[i].errors;
229     }
230     return (
231       this.tests.length + " tests, " + 
232       assertions + " assertions, " + 
233       failures   + " failures, " +
234       errors     + " errors");
235   }
236 }
237
238 Test.Unit.Assertions = Class.create();
239 Test.Unit.Assertions.prototype = {
240   initialize: function() {
241     this.assertions = 0;
242     this.failures   = 0;
243     this.errors     = 0;
244     this.messages   = [];
245   },
246   summary: function() {
247     return (
248       this.assertions + " assertions, " + 
249       this.failures   + " failures, " +
250       this.errors     + " errors" + "\n" +
251       this.messages.join("\n"));
252   },
253   pass: function() {
254     this.assertions++;
255   },
256   fail: function(message) {
257     this.failures++;
258     this.messages.push("Failure: " + message);
259   },
260   error: function(error) {
261     this.errors++;
262     this.messages.push(error.name + ": "+ error.message + "(" + Test.Unit.inspect(error) +")");
263   },
264   status: function() {
265     if (this.failures > 0) return 'failed';
266     if (this.errors > 0) return 'error';
267     return 'passed';
268   },
269   assert: function(expression) {
270     var message = arguments[1] || 'assert: got "' + Test.Unit.inspect(expression) + '"';
271     try { expression ? this.pass() : 
272       this.fail(message); }
273     catch(e) { this.error(e); }
274   },
275   assertEqual: function(expected, actual) {
276     var message = arguments[2] || "assertEqual";
277     try { (expected == actual) ? this.pass() :
278       this.fail(message + ': expected "' + Test.Unit.inspect(expected) + 
279         '", actual "' + Test.Unit.inspect(actual) + '"'); }
280     catch(e) { this.error(e); }
281   },
282   assertNotEqual: function(expected, actual) {
283     var message = arguments[2] || "assertNotEqual";
284     try { (expected != actual) ? this.pass() : 
285       this.fail(message + ': got "' + Test.Unit.inspect(actual) + '"'); }
286     catch(e) { this.error(e); }
287   },
288   assertNull: function(obj) {
289     var message = arguments[1] || 'assertNull'
290     try { (obj==null) ? this.pass() : 
291       this.fail(message + ': got "' + Test.Unit.inspect(obj) + '"'); }
292     catch(e) { this.error(e); }
293   },
294   assertHidden: function(element) {
295     var message = arguments[1] || 'assertHidden';
296     this.assertEqual("none", element.style.display, message);
297   },
298   assertNotNull: function(object) {
299     var message = arguments[1] || 'assertNotNull';
300     this.assert(object != null, message);
301   },
302   assertInstanceOf: function(expected, actual) {
303     var message = arguments[2] || 'assertInstanceOf';
304     try { 
305       (actual instanceof expected) ? this.pass() : 
306       this.fail(message + ": object was not an instance of the expected type"); }
307     catch(e) { this.error(e); } 
308   },
309   assertNotInstanceOf: function(expected, actual) {
310     var message = arguments[2] || 'assertNotInstanceOf';
311     try { 
312       !(actual instanceof expected) ? this.pass() : 
313       this.fail(message + ": object was an instance of the not expected type"); }
314     catch(e) { this.error(e); } 
315   },
316   _isVisible: function(element) {
317     element = $(element);
318     if(!element.parentNode) return true;
319     this.assertNotNull(element);
320     if(element.style && Element.getStyle(element, 'display') == 'none')
321       return false;
322     
323     return this._isVisible(element.parentNode);
324   },
325   assertNotVisible: function(element) {
326     this.assert(!this._isVisible(element), Test.Unit.inspect(element) + " was not hidden and didn't have a hidden parent either. " + ("" || arguments[1]));
327   },
328   assertVisible: function(element) {
329     this.assert(this._isVisible(element), Test.Unit.inspect(element) + " was not visible. " + ("" || arguments[1]));
330   }
331 }
332
333 Test.Unit.Testcase = Class.create();
334 Object.extend(Object.extend(Test.Unit.Testcase.prototype, Test.Unit.Assertions.prototype), {
335   initialize: function(name, test, setup, teardown) {
336     Test.Unit.Assertions.prototype.initialize.bind(this)();
337     this.name           = name;
338     this.test           = test || function() {};
339     this.setup          = setup || function() {};
340     this.teardown       = teardown || function() {};
341     this.isWaiting      = false;
342     this.timeToWait     = 1000;
343   },
344   wait: function(time, nextPart) {
345     this.isWaiting = true;
346     this.test = nextPart;
347     this.timeToWait = time;
348   },
349   run: function() {
350     try {
351       try {
352         if (!this.isWaiting) this.setup.bind(this)();
353         this.isWaiting = false;
354         this.test.bind(this)();
355       } finally {
356         if(!this.isWaiting) {
357           this.teardown.bind(this)();
358         }
359       }
360     }
361     catch(e) { this.error(e); }
362   }
363 });