⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unittest.js

📁 原名JSPackager
💻 JS
📖 第 1 页 / 共 2 页
字号:
Event.simulateMouse = function (element, eventName) {var options = Object.extend({pointerX:0, pointerY:0, buttons:0, ctrlKey:false, altKey:false, shiftKey:false, metaKey:false}, arguments[2] || {});var oEvent = document.createEvent("MouseEvents");oEvent.initMouseEvent(eventName, true, true, document.defaultView, options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY, options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, 0, $(element));if (this.mark) {Element.remove(this.mark);}this.mark = document.createElement("div");this.mark.appendChild(document.createTextNode(" "));document.body.appendChild(this.mark);this.mark.style.position = "absolute";this.mark.style.top = options.pointerY + "px";this.mark.style.left = options.pointerX + "px";this.mark.style.width = "5px";this.mark.style.height = "5px;";this.mark.style.borderTop = "1px solid red;";this.mark.style.borderLeft = "1px solid red;";if (this.step) {alert("[" + new Date().getTime().toString() + "] " + eventName + "/" + Test.Unit.inspect(options));}$(element).dispatchEvent(oEvent);};Event.simulateKey = function (element, eventName) {var options = Object.extend({ctrlKey:false, altKey:false, shiftKey:false, metaKey:false, keyCode:0, charCode:0}, arguments[2] || {});var oEvent = document.createEvent("KeyEvents");oEvent.initKeyEvent(eventName, true, true, window, options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.keyCode, options.charCode);$(element).dispatchEvent(oEvent);};Event.simulateKeys = function (element, command) {for (var i = 0; i < command.length; i++) {Event.simulateKey(element, "keypress", {charCode:command.charCodeAt(i)});}};var Test = {};Test.Unit = {};Test.Unit.inspect = Object.inspect;Test.Unit.Logger = Class.create();Test.Unit.Logger.prototype = {initialize:function (log) {this.log = $(log);if (this.log) {this._createLogTable();}}, start:function (testName) {if (!this.log) {return;}this.testName = testName;this.lastLogLine = document.createElement("tr");this.statusCell = document.createElement("td");this.nameCell = document.createElement("td");this.nameCell.className = "nameCell";this.nameCell.appendChild(document.createTextNode(testName));this.messageCell = document.createElement("td");this.lastLogLine.appendChild(this.statusCell);this.lastLogLine.appendChild(this.nameCell);this.lastLogLine.appendChild(this.messageCell);this.loglines.appendChild(this.lastLogLine);}, finish:function (status, summary) {if (!this.log) {return;}this.lastLogLine.className = status;this.statusCell.innerHTML = status;this.messageCell.innerHTML = this._toHTML(summary);this.addLinksToResults();}, message:function (message) {if (!this.log) {return;}this.messageCell.innerHTML = this._toHTML(message);}, summary:function (summary) {if (!this.log) {return;}this.logsummary.innerHTML = this._toHTML(summary);}, _createLogTable:function () {this.log.innerHTML = "<div id=\"logsummary\"></div>" + "<table id=\"logtable\">" + "<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>" + "<tbody id=\"loglines\"></tbody>" + "</table>";this.logsummary = $("logsummary");this.loglines = $("loglines");}, _toHTML:function (txt) {return txt.escapeHTML().replace(/\n/g, "<br/>");}, addLinksToResults:function () {$$("tr.failed .nameCell").each(function (td) {td.title = "Run only this test";Event.observe(td, "click", function () {window.location.search = "?tests=" + td.innerHTML;});});$$("tr.passed .nameCell").each(function (td) {td.title = "Run all tests";Event.observe(td, "click", function () {window.location.search = "";});});}};Test.Unit.Runner = Class.create();Test.Unit.Runner.prototype = {initialize:function (testcases) {this.options = Object.extend({testLog:"testlog"}, arguments[1] || {});this.options.resultsURL = this.parseResultsURLQueryParameter();this.options.tests = this.parseTestsQueryParameter();if (this.options.testLog) {this.options.testLog = $(this.options.testLog) || null;}if (this.options.tests) {this.tests = [];for (var i = 0; i < this.options.tests.length; i++) {if (/^test/.test(this.options.tests[i])) {this.tests.push(new Test.Unit.Testcase(this.options.tests[i], testcases[this.options.tests[i]], testcases["setup"], testcases["teardown"]));}}} else {if (this.options.test) {this.tests = [new Test.Unit.Testcase(this.options.test, testcases[this.options.test], testcases["setup"], testcases["teardown"])];} else {this.tests = [];for (var testcase in testcases) {if (/^test/.test(testcase)) {this.tests.push(new Test.Unit.Testcase(this.options.context ? " -> " + this.options.titles[testcase] : testcase, testcases[testcase], testcases["setup"], testcases["teardown"]));}}}}this.currentTest = 0;this.logger = new Test.Unit.Logger(this.options.testLog);setTimeout(this.runTests.bind(this), 1000);}, parseResultsURLQueryParameter:function () {return window.location.search.parseQuery()["resultsURL"];}, parseTestsQueryParameter:function () {if (window.location.search.parseQuery()["tests"]) {return window.location.search.parseQuery()["tests"].split(",");}}, getResult:function () {var hasFailure = false;for (var i = 0; i < this.tests.length; i++) {if (this.tests[i].errors > 0) {return "ERROR";}if (this.tests[i].failures > 0) {hasFailure = true;}}if (hasFailure) {return "FAILURE";} else {return "SUCCESS";}}, postResults:function () {if (this.options.resultsURL) {new Ajax.Request(this.options.resultsURL, {method:"get", parameters:"result=" + this.getResult(), asynchronous:false});}}, runTests:function () {var test = this.tests[this.currentTest];if (!test) {this.postResults();this.logger.summary(this.summary());return;}if (!test.isWaiting) {this.logger.start(test.name);}test.run();if (test.isWaiting) {this.logger.message("Waiting for " + test.timeToWait + "ms");setTimeout(this.runTests.bind(this), test.timeToWait || 1000);} else {this.logger.finish(test.status(), test.summary());this.currentTest++;this.runTests();}}, summary:function () {var assertions = 0;var failures = 0;var errors = 0;var messages = [];for (var i = 0; i < this.tests.length; i++) {assertions += this.tests[i].assertions;failures += this.tests[i].failures;errors += this.tests[i].errors;}return ((this.options.context ? this.options.context + ": " : "") + this.tests.length + " tests, " + assertions + " assertions, " + failures + " failures, " + errors + " errors");}};Test.Unit.Assertions = Class.create();Test.Unit.Assertions.prototype = {initialize:function () {this.assertions = 0;this.failures = 0;this.errors = 0;this.messages = [];}, summary:function () {return (this.assertions + " assertions, " + this.failures + " failures, " + this.errors + " errors" + "\n" + this.messages.join("\n"));}, pass:function () {this.assertions++;}, fail:function (message) {this.failures++;this.messages.push("Failure: " + message);}, info:function (message) {this.messages.push("Info: " + message);}, error:function (error) {this.errors++;this.messages.push(error.name + ": " + error.message + "(" + Test.Unit.inspect(error) + ")");}, status:function () {if (this.failures > 0) {return "failed";}if (this.errors > 0) {return "error";}return "passed";}, assert:function (expression) {var message = arguments[1] || "assert: got \"" + Test.Unit.inspect(expression) + "\"";try {expression ? this.pass() : this.fail(message);}catch (e) {this.error(e);}}, assertEqual:function (expected, actual) {var message = arguments[2] || "assertEqual";try {(expected == actual) ? this.pass() : this.fail(message + ": expected \"" + Test.Unit.inspect(expected) + "\", actual \"" + Test.Unit.inspect(actual) + "\"");}catch (e) {this.error(e);}}, assertInspect:function (expected, actual) {var message = arguments[2] || "assertInspect";try {(expected == actual.inspect()) ? this.pass() : this.fail(message + ": expected \"" + Test.Unit.inspect(expected) + "\", actual \"" + Test.Unit.inspect(actual) + "\"");}catch (e) {this.error(e);}}, assertEnumEqual:function (expected, actual) {var message = arguments[2] || "assertEnumEqual";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -