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