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       if (child) { 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.suites();
 32 
 33   this.runnerDiv = this.createDom('div', { className: 'runner running' },
 34       this.createDom('a', { className: 'runSpec', href: '?' }, "run all"),
 35       this.runnerMessageSpan = this.createDom('span', {}, "Running..."));
 36   this.document.body.appendChild(this.runnerDiv);
 37 
 38   for (var i = 0; i < suites.length; i++) {
 39     var suite = suites[i];
 40     var suiteDiv = this.createDom('div', { className: 'suite' },
 41         this.createDom('a', { className: 'runSpec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"),
 42         suite.description);
 43     this.suiteDivs[suite.getFullName()] = suiteDiv;
 44     var parentDiv = this.document.body;
 45     if (suite.parentSuite) {
 46       parentDiv = this.suiteDivs[suite.parentSuite.getFullName()];
 47     }
 48     parentDiv.appendChild(suiteDiv);
 49   }
 50 
 51   this.startedAt = new Date();
 52 };
 53 
 54 jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) {
 55   var results = runner.results();
 56   var className = (results.failedCount > 0) ? "runner failed" : "runner passed";
 57   this.runnerDiv.setAttribute("class", className);
 58   var message = results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s");
 59   message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s";
 60   this.runnerMessageSpan.replaceChild(this.document.createTextNode(message), this.runnerMessageSpan.firstChild);
 61 };
 62 
 63 jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) {
 64   var results = suite.results();
 65   var status = results.passed() ? 'passed' : 'failed';
 66   if (results.totalCount == 0) { // todo: change this to check results.skipped
 67     status = 'skipped';
 68   }
 69   this.suiteDivs[suite.getFullName()].className += " " + status;
 70 };
 71 
 72 jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
 73   var results = spec.results();
 74   var status = results.passed() ? 'passed' : 'failed';
 75   if (results.skipped) {
 76     status = 'skipped';
 77   }
 78   var specDiv = this.createDom('div', { className: 'spec '  + status },
 79       this.createDom('a', { className: 'runSpec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"),
 80       spec.getFullName());
 81 
 82 
 83   var resultItems = results.getItems();
 84   for (var i = 0; i < resultItems.length; i++) {
 85     var result = resultItems[i];
 86     if (result.passed && !result.passed()) {
 87       var resultMessageDiv = this.createDom('div', {className: 'resultMessage fail'});
 88       resultMessageDiv.innerHTML = result.message; // todo: lame; mend
 89       specDiv.appendChild(resultMessageDiv);
 90       specDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
 91     }
 92   }
 93 
 94   this.suiteDivs[spec.suite.getFullName()].appendChild(specDiv);
 95 };
 96 
 97 jasmine.TrivialReporter.prototype.log = function() {
 98   console.log.apply(console, arguments);
 99 };
100 
101 jasmine.TrivialReporter.prototype.getLocation = function() {
102   return this.document.location;
103 };
104 
105 jasmine.TrivialReporter.prototype.specFilter = function(spec) {
106   var paramMap = {};
107   var params = this.getLocation().search.substring(1).split('&');
108   for (var i = 0; i < params.length; i++) {
109     var p = params[i].split('=');
110     paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
111   }
112 
113   if (!paramMap["spec"]) return true;
114   return spec.getFullName().indexOf(paramMap["spec"]) == 0;
115 };
116 
117 //protect against console.log incidents
118 if (!("console" in window) || !("firebug" in console)) {
119   var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
120   window.console = {};
121   for (var i = 0, len = names.length; i < len; ++i) {
122     window.console[names[i]] = function() {
123     };
124   }
125 }
126