jasmine-html.js 6.8 KB

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