jasmine-html.js 6.6 KB

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