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[attr] = attrs[attr];
 22     } else {
 23       el.setAttribute(attr, attrs[attr]);
 24     }
 25   }
 26 
 27   return el;
 28 };
 29 
 30 jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) {
 31   var showPassed, showSkipped;
 32 
 33   this.outerDiv = this.createDom('div', { className: 'jasmine_reporter' },
 34       this.createDom('div', { className: 'banner' },
 35         this.createDom('div', { className: 'logo' },
 36             "Jasmine",
 37             this.createDom('span', { className: 'version' }, runner.env.versionString())),
 38         this.createDom('div', { className: 'options' },
 39             "Show ",
 40             showPassed = this.createDom('input', { id: "__jasmine_TrivialReporter_showPassed__", type: 'checkbox' }),
 41             this.createDom('label', { "for": "__jasmine_TrivialReporter_showPassed__" }, " passed "),
 42             showSkipped = this.createDom('input', { id: "__jasmine_TrivialReporter_showSkipped__", type: 'checkbox' }),
 43             this.createDom('label', { "for": "__jasmine_TrivialReporter_showSkipped__" }, " skipped")
 44             )
 45           ),
 46 
 47       this.runnerDiv = this.createDom('div', { className: 'runner running' },
 48           this.createDom('a', { className: 'run_spec', href: '?' }, "run all"),
 49           this.runnerMessageSpan = this.createDom('span', {}, "Running..."),
 50           this.finishedAtSpan = this.createDom('span', { className: 'finished-at' }, ""))
 51       );
 52 
 53   this.document.body.appendChild(this.outerDiv);
 54 
 55   var suites = runner.suites();
 56   for (var i = 0; i < suites.length; i++) {
 57     var suite = suites[i];
 58     var suiteDiv = this.createDom('div', { className: 'suite' },
 59         this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"),
 60         this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, suite.description));
 61     this.suiteDivs[suite.id] = suiteDiv;
 62     var parentDiv = this.outerDiv;
 63     if (suite.parentSuite) {
 64       parentDiv = this.suiteDivs[suite.parentSuite.id];
 65     }
 66     parentDiv.appendChild(suiteDiv);
 67   }
 68 
 69   this.startedAt = new Date();
 70 
 71   var self = this;
 72   showPassed.onchange = function(evt) {
 73     if (evt.target.checked) {
 74       self.outerDiv.className += ' show-passed';
 75     } else {
 76       self.outerDiv.className = self.outerDiv.className.replace(/ show-passed/, '');
 77     }
 78   };
 79 
 80   showSkipped.onchange = function(evt) {
 81     if (evt.target.checked) {
 82       self.outerDiv.className += ' show-skipped';
 83     } else {
 84       self.outerDiv.className = self.outerDiv.className.replace(/ show-skipped/, '');
 85     }
 86   };
 87 };
 88 
 89 jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) {
 90   var results = runner.results();
 91   var className = (results.failedCount > 0) ? "runner failed" : "runner passed";
 92   this.runnerDiv.setAttribute("class", className);
 93   //do it twice for IE
 94   this.runnerDiv.setAttribute("className", className);
 95   var specs = runner.specs();
 96   var specCount = 0;
 97   for (var i = 0; i < specs.length; i++) {
 98     if (this.specFilter(specs[i])) {
 99       specCount++;
100     }
101   }
102   var message = "" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s");
103   message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s";
104   this.runnerMessageSpan.replaceChild(this.createDom('a', { className: 'description', href: '?'}, message), this.runnerMessageSpan.firstChild);
105 
106   this.finishedAtSpan.appendChild(document.createTextNode("Finished at " + new Date().toString()));
107 };
108 
109 jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) {
110   var results = suite.results();
111   var status = results.passed() ? 'passed' : 'failed';
112   if (results.totalCount == 0) { // todo: change this to check results.skipped
113     status = 'skipped';
114   }
115   this.suiteDivs[suite.id].className += " " + status;
116 };
117 
118 jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
119   var results = spec.results();
120   var status = results.passed() ? 'passed' : 'failed';
121   if (results.skipped) {
122     status = 'skipped';
123   }
124   var specDiv = this.createDom('div', { className: 'spec '  + status },
125       this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"),
126       this.createDom('a', {
127         className: 'description',
128         href: '?spec=' + encodeURIComponent(spec.getFullName()),
129         title: spec.getFullName()
130       }, spec.description));
131 
132 
133   var resultItems = results.getItems();
134   var messagesDiv = this.createDom('div', { className: 'messages' });
135   for (var i = 0; i < resultItems.length; i++) {
136     var result = resultItems[i];
137 
138     if (result.type == 'log') {
139       messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString()));
140     } else if (result.type == 'expect' && result.passed && !result.passed()) {
141       messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message));
142 
143       if (result.trace.stack) {
144         messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
145       }
146     }
147   }
148 
149   if (messagesDiv.childNodes.length > 0) {
150     specDiv.appendChild(messagesDiv);
151   }
152 
153   this.suiteDivs[spec.suite.id].appendChild(specDiv);
154 };
155 
156 jasmine.TrivialReporter.prototype.log = function() {
157   var console = jasmine.getGlobal().console;
158   if (console && console.log) console.log.apply(console, arguments);
159 };
160 
161 jasmine.TrivialReporter.prototype.getLocation = function() {
162   return this.document.location;
163 };
164 
165 jasmine.TrivialReporter.prototype.specFilter = function(spec) {
166   var paramMap = {};
167   var params = this.getLocation().search.substring(1).split('&');
168   for (var i = 0; i < params.length; i++) {
169     var p = params[i].split('=');
170     paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
171   }
172 
173   if (!paramMap["spec"]) return true;
174   return spec.getFullName().indexOf(paramMap["spec"]) == 0;
175 };
176