📄 ecmaunit.js
字号:
/***************************************************************************** * * Copyright (c) 2003-2004 EcmaUnit Contributors. All rights reserved. * * This software is distributed under the terms of the EcmaUnit * License. See LICENSE.txt for license text. For a list of EcmaUnit * Contributors see CREDITS.txt. * *****************************************************************************/// $Id$/* Object-oriented prototype-based unit test suite*/function TestCase() { /* a single test case */ this.name = 'TestCase';};TestCase.prototype.initialize = function(reporter) { // this array's contents will be displayed when done (if it // contains anything) this._exceptions = new Array(); this._reporter = reporter;};TestCase.prototype.setUp = function() { /* this will be called on before each test method that is ran */};TestCase.prototype.tearDown = function() { /* this will be called after each test method that has been ran */};TestCase.prototype.assertEquals = function(var1, var2, message) { /* assert whether 2 vars have the same value */ if (!message) { message = ''; } else { message = "'" + message + "' "; } if (var1 != var2 && (!(var1 instanceof Array && var2 instanceof Array) || !this._arrayDeepCompare(var1, var2))) { this._throwException('Assertion ' + message + 'failed: ' + var1 + ' != ' + var2); };};TestCase.prototype.assertNotEquals = function(var1, var2, message) { /* assert whether 2 vars have different values */ if (!message) { message = ''; } else { message = "'" + message + "' "; } if (var1 && var1.toSource && var2 && var2.toSource) { if (var1.toSource() == var2.toSource()) { this._throwException('Assertion ' + message + 'failed: ' + var1 + ' == ' + var2); }; } else { if (var1 == var2) { this._throwException('Assertion ' + message + 'failed: ' + var1 + ' == ' + var2); }; };};TestCase.prototype.debug = function(msg) { this._reporter.debug(msg);}TestCase.prototype.assert = function(statement, message) { /* assert whether a variable resolves to true */ if (!statement) { if (!message) message = (statement && statement.toString) ? statement.toString() : statement; this._throwException('Assertion \'' + message + '\' failed'); };};TestCase.prototype.assertTrue = TestCase.prototype.assert;TestCase.prototype.assertFalse = function(statement, message) { /* assert whether a variable resolves to false */ if (statement) { if (!message) message = statement.toString ? statement.toString() : statement; this._throwException('AssertFalse \'' + message + '\' failed'); };};TestCase.prototype.assertThrows = function(func, exception, context) { /* assert whether a certain exception is raised */ if (!context) { context = null; }; var exception_thrown = false; // remove the first three args, they're the function's normal args var args = []; for (var i=3; i < arguments.length; i++) { args.push(arguments[i]); }; try { func.apply(context, args); } catch(e) { // allow catching undefined exceptions too if (exception === undefined) { } else if (exception) { var isinstance = false; try { if (e instanceof exception) { isinstance = true; }; } catch(f) { }; if (!isinstance) { if (exception.toSource && e.toSource) { exception = exception.toSource(); e = e.toSource(); }; if (exception.toString && e.toString) { exception = exception.toString(); e = e.toString(); }; if (e != exception) { this._throwException('Function threw the wrong ' + 'exception ' + e.toString() + ', while expecting ' + exception.toString()); }; }; }; exception_thrown = true; }; if (!exception_thrown) { if (exception) { this._throwException("function didn\'t raise exception \'" + exception.toString() + "'"); } else { this._throwException('function didn\'t raise exception'); }; };};TestCase.prototype.runTests = function() { /* find all methods of which the name starts with 'test' and call them */ var ret = this._runHelper(); this._reporter.summarize(ret[0], ret[1], this._exceptions);};TestCase.prototype._runHelper = function() { /* this actually runs the tests return value is an array [total tests ran, total time spent (ms)] */ var now = new Date(); var starttime = now.getTime(); var numtests = 0; for (var attr in this) { if (attr.substr(0, 4) == 'test') { this.setUp(); try { this[attr](); this._reporter.reportSuccess(this.name, attr); } catch(e) { var raw = e; if (e && e.name && e.message) { // Microsoft e = e.name + ': ' + e.message; } this._reporter.reportError(this.name, attr, e, raw); this._exceptions.push(new Array(this.name, attr, e, raw)); }; this.tearDown(); numtests++; }; }; var now = new Date(); var totaltime = now.getTime() - starttime; return new Array(numtests, totaltime);};TestCase.prototype._throwException = function(message) { var lineno = this._getLineNo(); if (lineno) { message = 'line ' + lineno + ' - ' + message; }; throw(message);};TestCase.prototype._getLineNo = function() { /* tries to get the line no in Moz */ var stack = undefined; try {notdefined()} catch(e) {stack = e.stack}; if (stack) { stack = stack.toString().split('\n'); for (var i=0; i < stack.length; i++) { var line = stack[i].split('@')[1]; if (line.indexOf('ecmaunit') == -1) { // return the first line after we get out of ecmaunit var chunks = line.split(':'); var lineno = chunks[chunks.length - 1]; if (lineno != '0') { return lineno; }; }; }; } else { return false; };};TestCase.prototype._arrayDeepCompare = function(a1, a2) { if (!(a1 instanceof Array && a2 instanceof Array)) { return false; }; if (a1.length != a2.length) { return false; }; for (var i=0; i < a1.length; i++) { if (a1[i] instanceof Array) { if (!this._arrayDeepCompare(a1[i], a2[i])) { return false; }; } else if (a1[i] != a2[i]) { return false; }; }; return true;};function TestSuite(reporter) { /* run a suite of tests */ if (reporter) { this._reporter = reporter; this._tests = new Array(); this._exceptions = new Array(); };};TestSuite.prototype.registerTest = function(test) { /* register a test */ if (!test) { throw('TestSuite.registerTest() requires a testcase as argument'); };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -