1 jasmine.TrivialReporter = function(doc) { 2 this.document = doc || document; 3 this.suiteDivs = {}; 4 }; 5 6 jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) { 7 var el = document.createElement(type); 8 9 for (var i = 2; i < arguments.length; i++) { 10 var child = arguments[i]; 11 12 if (typeof child === 'string') { 13 el.appendChild(document.createTextNode(child)); 14 } else { 15 el.appendChild(child); 16 } 17 } 18 19 for (var attr in attrs) { 20 if (attr == 'className') { 21 el.setAttribute('class', attrs[attr]); 22 } else { 23 el[attr] = attrs[attr]; 24 } 25 } 26 27 return el; 28 }; 29 30 jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) { 31 var suites = runner.getAllSuites(); 32 33 this.runnerDiv = this.createDom('div', { className: 'runner running' }, "Running..."); 34 this.document.body.appendChild(this.runnerDiv); 35 36 for (var i = 0; i < suites.length; i++) { 37 var suite = suites[i]; 38 var suiteDiv = this.createDom('div', { className: 'suite' }, 39 this.createDom('a', { className: 'runSpec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"), 40 suite.description); 41 this.suiteDivs[suite.getFullName()] = suiteDiv; 42 var parentDiv = this.document.body; 43 if (suite.parentSuite) { 44 parentDiv = this.suiteDivs[suite.parentSuite.getFullName()]; 45 } 46 parentDiv.appendChild(suiteDiv); 47 } 48 }; 49 50 jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) { 51 var results = runner.getResults(); 52 var className = (results.failedCount > 0) ? "runner failed" : "runner passed"; 53 this.runnerDiv.setAttribute("class", className); 54 var message = results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s"); 55 this.runnerDiv.replaceChild(this.document.createTextNode(message), this.runnerDiv.firstChild); 56 }; 57 58 jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) { 59 var results = suite.getResults(); 60 var status = results.passed() ? 'passed' : 'failed'; 61 if (results.totalCount == 0) { // todo: change this to check results.skipped 62 status = 'skipped'; 63 } 64 this.suiteDivs[suite.getFullName()].className += " " + status; 65 }; 66 67 jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) { 68 var results = spec.getResults(); 69 var status = results.passed() ? 'passed' : 'failed'; 70 if (results.skipped) { 71 status = 'skipped'; 72 } 73 var specDiv = this.createDom('div', { className: 'spec ' + status }, 74 this.createDom('a', { className: 'runSpec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"), 75 spec.getFullName()); 76 77 78 var resultItems = results.getItems(); 79 for (var i = 0; i < resultItems.length; i++) { 80 var result = resultItems[i]; 81 if (result.passed && !result.passed()) { 82 var resultMessageDiv = this.createDom('div', {className: 'resultMessage fail'}); 83 resultMessageDiv.innerHTML = result.message; // todo: lame; mend 84 specDiv.appendChild(resultMessageDiv); 85 specDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack)); 86 } 87 } 88 89 this.suiteDivs[spec.suite.getFullName()].appendChild(specDiv); 90 }; 91 92 jasmine.TrivialReporter.prototype.log = function() { 93 console.log.apply(console, arguments); 94 }; 95 96 jasmine.TrivialReporter.prototype.getLocation = function() { 97 return this.document.location; 98 }; 99 100 jasmine.TrivialReporter.prototype.specFilter = function(spec) { 101 var paramMap = {}; 102 var params = this.getLocation().search.substring(1).split('&'); 103 for (var i = 0; i < params.length; i++) { 104 var p = params[i].split('='); 105 paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]); 106 } 107 108 if (!paramMap["spec"]) return true; 109 return spec.getFullName().indexOf(paramMap["spec"]) == 0; 110 }; 111 112 //protect against console.log incidents 113 if (!("console" in window) || !("firebug" in console)) { 114 var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"]; 115 window.console = {}; 116 for (var i = 0, len = names.length; i < len; ++i) { 117 window.console[names[i]] = function() { 118 }; 119 } 120 } 121