|
ActiveMQ example source code file (jsunittest.js)
The ActiveMQ jsunittest.js source code/* Jsunittest, version 0.7.3 * (c) 2008 Dr Nic Williams * * Jsunittest is freely distributable under * the terms of an MIT-style license. * For details, see the web site: http://jsunittest.rubyforge.org * *--------------------------------------------------------------------------*/ var JsUnitTest = { Unit: {}, inspect: function(object) { try { if (typeof object == "undefined") return 'undefined'; if (object === null) return 'null'; if (typeof object == "string") { var useDoubleQuotes = arguments[1]; var escapedString = this.gsub(object, /[\x00-\x1f\\]/, function(match) { var character = String.specialChar[match[0]]; return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16); }); if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; return "'" + escapedString.replace(/'/g, '\\\'') + "'"; }; return String(object); } catch (e) { if (e instanceof RangeError) return '...'; throw e; } }, $: function(element) { if (arguments.length > 1) { for (var i = 0, elements = [], length = arguments.length; i < length; i++) elements.push(this.$(arguments[i])); return elements; } if (typeof element == "string") element = document.getElementById(element); return element; }, gsub: function(source, pattern, replacement) { var result = '', match; replacement = arguments.callee.prepareReplacement(replacement); while (source.length > 0) { if (match = source.match(pattern)) { result += source.slice(0, match.index); result += JsUnitTest.String.interpret(replacement(match)); source = source.slice(match.index + match[0].length); } else { result += source, source = ''; } } return result; }, scan: function(source, pattern, iterator) { this.gsub(source, pattern, iterator); return String(source); }, escapeHTML: function(data) { return data.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); }, arrayfromargs: function(args) { var myarray = new Array(); var i; for (i=0;i<args.length;i++) myarray[i] = args[i]; return myarray; }, hashToSortedArray: function(hash) { var results = []; for (key in hash) { results.push([key, hash[key]]); } return results.sort(); }, flattenArray: function(array) { var results = arguments[1] || []; for (var i=0; i < array.length; i++) { var object = array[i]; if (object != null && typeof object == "object" && 'splice' in object && 'join' in object) { this.flattenArray(object, results); } else { results.push(object); } }; return results; }, selectorMatch: function(expression, element) { var tokens = []; var patterns = { // combinators must be listed first // (and descendant needs to be last combinator) laterSibling: /^\s*~\s*/, child: /^\s*>\s*/, adjacent: /^\s*\+\s*/, descendant: /^\s/, // selectors follow tagName: /^\s*(\*|[\w\-]+)(\b|$)?/, id: /^#([\w\-\*]+)(\b|$)/, className: /^\.([\w\-\*]+)(\b|$)/, pseudo: /^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s|[:+~>]))/, attrPresence: /^\[((?:[\w]+:)?[\w]+)\]/, attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/ }; var assertions = { tagName: function(element, matches) { return matches[1].toUpperCase() == element.tagName.toUpperCase(); }, className: function(element, matches) { return Element.hasClassName(element, matches[1]); }, id: function(element, matches) { return element.id === matches[1]; }, attrPresence: function(element, matches) { return Element.hasAttribute(element, matches[1]); }, attr: function(element, matches) { var nodeValue = Element.readAttribute(element, matches[1]); return nodeValue && operators[matches[2]](nodeValue, matches[5] || matches[6]); } }; var e = this.expression, ps = patterns, as = assertions; var le, p, m; while (e && le !== e && (/\S/).test(e)) { le = e; for (var i in ps) { p = ps[i]; if (m = e.match(p)) { // use the Selector.assertions methods unless the selector // is too complex. if (as[i]) { tokens.push([i, Object.clone(m)]); e = e.replace(m[0], ''); } } } } var match = true, name, matches; for (var i = 0, token; token = tokens[i]; i++) { name = token[0], matches = token[1]; if (!assertions[name](element, matches)) { match = false; break; } } return match; }, toQueryParams: function(query, separator) { var query = query || window.location.search; var match = query.replace(/^\s+/, '').replace(/\s+$/, '').match(/([^?#]*)(#.*)?$/); if (!match) return { }; var hash = {}; var parts = match[1].split(separator || '&'); for (var i=0; i < parts.length; i++) { var pair = parts[i].split('='); if (pair[0]) { var key = decodeURIComponent(pair.shift()); var value = pair.length > 1 ? pair.join('=') : pair[0]; if (value != undefined) value = decodeURIComponent(value); if (key in hash) { var object = hash[key]; var isArray = object != null && typeof object == "object" && 'splice' in object && 'join' in object if (!isArray) hash[key] = [hash[key]]; hash[key].push(value); } else hash[key] = value; } }; return hash; }, String: { interpret: function(value) { return value == null ? '' : String(value); } } }; JsUnitTest.gsub.prepareReplacement = function(replacement) { if (typeof replacement == "function") return replacement; var template = new Template(replacement); return function(match) { return template.evaluate(match) }; }; JsUnitTest.Version = '0.7.3'; JsUnitTest.Template = function(template, pattern) { this.template = template; //template.toString(); this.pattern = pattern || JsUnitTest.Template.Pattern; }; JsUnitTest.Template.prototype.evaluate = function(object) { if (typeof object.toTemplateReplacements == "function") object = object.toTemplateReplacements(); return JsUnitTest.gsub(this.template, this.pattern, function(match) { if (object == null) return ''; var before = match[1] || ''; if (before == '\\') return match[2]; var ctx = object, expr = match[3]; var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; match = pattern.exec(expr); if (match == null) return before; while (match != null) { var comp = (match[1].indexOf('[]') === 0) ? match[2].gsub('\\\\]', ']') : match[1]; ctx = ctx[comp]; if (null == ctx || '' == match[3]) break; expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); match = pattern.exec(expr); } return before + JsUnitTest.String.interpret(ctx); }); } JsUnitTest.Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; JsUnitTest.Event = {}; // written by Dean Edwards, 2005 // with input from Tino Zijdel, Matthias Miller, Diego Perini // namespaced by Dr Nic Williams 2008 // http://dean.edwards.name/weblog/2005/10/add-event/ // http://dean.edwards.name/weblog/2005/10/add-event2/ JsUnitTest.Event.addEvent = function(element, type, handler) { if (element.addEventListener) { element.addEventListener(type, handler, false); } else { // assign each event handler a unique ID if (!handler.$$guid) handler.$$guid = JsUnitTest.Event.addEvent.guid++; // create a hash table of event types for the element if (!element.events) element.events = {}; // create a hash table of event handlers for each element/event pair var handlers = element.events[type]; if (!handlers) { handlers = element.events[type] = {}; // store the existing event handler (if there is one) if (element["on" + type]) { handlers[0] = element["on" + type]; } } // store the event handler in the hash table handlers[handler.$$guid] = handler; // assign a global event handler to do all the work element["on" + type] = this.handleEvent; } }; // a counter used to create unique IDs JsUnitTest.Event.addEvent.guid = 1; JsUnitTest.Event.removeEvent = function(element, type, handler) { if (element.removeEventListener) { element.removeEventListener(type, handler, false); } else { // delete the event handler from the hash table if (element.events && element.events[type]) { delete element.events[type][handler.$$guid]; } } }; JsUnitTest.Event.handleEvent = function(event) { var returnValue = true; // grab the event object (IE uses a global event object) event = event || JsUnitTest.Event.fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event); // get a reference to the hash table of event handlers var handlers = this.events[event.type]; // execute each event handler for (var i in handlers) { this.$$handleEvent = handlers[i]; if (this.$$handleEvent(event) === false) { returnValue = false; } } return returnValue; }; JsUnitTest.Event.fixEvent = function(event) { // add W3C standard event methods event.preventDefault = this.fixEvent.preventDefault; event.stopPropagation = this.fixEvent.stopPropagation; return event; }; JsUnitTest.Event.fixEvent.preventDefault = function() { this.returnValue = false; }; JsUnitTest.Event.fixEvent.stopPropagation = function() { this.cancelBubble = true; }; JsUnitTest.Unit.Logger = function(element) { this.element = JsUnitTest.$(element); if (this.element) this._createLogTable(); }; JsUnitTest.Unit.Logger.prototype.start = function(testName) { if (!this.element) return; var tbody = this.element.getElementsByTagName('tbody')[0]; var tr = document.createElement('tr'); var td; //testname td = document.createElement('td'); td.appendChild(document.createTextNode(testName)); tr.appendChild(td) tr.appendChild(document.createElement('td'));//status tr.appendChild(document.createElement('td'));//message tbody.appendChild(tr); }; JsUnitTest.Unit.Logger.prototype.setStatus = function(status) { var logline = this.getLastLogLine(); logline.className = status; var statusCell = logline.getElementsByTagName('td')[1]; statusCell.appendChild(document.createTextNode(status)); }; JsUnitTest.Unit.Logger.prototype.finish = function(status, summary) { if (!this.element) return; this.setStatus(status); this.message(summary); }; JsUnitTest.Unit.Logger.prototype.message = function(message) { if (!this.element) return; var cell = this.getMessageCell(); // cell.appendChild(document.createTextNode(this._toHTML(message))); cell.innerHTML = this._toHTML(message); }; JsUnitTest.Unit.Logger.prototype.summary = function(summary) { if (!this.element) return; var div = this.element.getElementsByTagName('div')[0]; div.innerHTML = this._toHTML(summary); }; JsUnitTest.Unit.Logger.prototype.getLastLogLine = function() { var tbody = this.element.getElementsByTagName('tbody')[0]; var loglines = tbody.getElementsByTagName('tr'); return loglines[loglines.length - 1]; }; JsUnitTest.Unit.Logger.prototype.getMessageCell = function() { var logline = this.getLastLogLine(); return logline.getElementsByTagName('td')[2]; }; JsUnitTest.Unit.Logger.prototype._createLogTable = function() { var html = '<div class="logsummary">running... | ||
Status | Test | Message |
---|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.