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