Nenhuma descrição

jasmine-html.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. Copyright (c) 2008-2014 Pivotal Labs
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. jasmineRequire.html = function(j$) {
  21. j$.ResultsNode = jasmineRequire.ResultsNode();
  22. j$.HtmlReporter = jasmineRequire.HtmlReporter(j$);
  23. j$.QueryString = jasmineRequire.QueryString();
  24. j$.HtmlSpecFilter = jasmineRequire.HtmlSpecFilter();
  25. };
  26. jasmineRequire.HtmlReporter = function(j$) {
  27. var noopTimer = {
  28. start: function() {},
  29. elapsed: function() { return 0; }
  30. };
  31. function HtmlReporter(options) {
  32. var env = options.env || {},
  33. getContainer = options.getContainer,
  34. createElement = options.createElement,
  35. createTextNode = options.createTextNode,
  36. onRaiseExceptionsClick = options.onRaiseExceptionsClick || function() {},
  37. timer = options.timer || noopTimer,
  38. results = [],
  39. specsExecuted = 0,
  40. failureCount = 0,
  41. pendingSpecCount = 0,
  42. htmlReporterMain,
  43. symbols;
  44. this.initialize = function() {
  45. clearPrior();
  46. htmlReporterMain = createDom('div', {className: 'jasmine_html-reporter'},
  47. createDom('div', {className: 'banner'},
  48. createDom('a', {className: 'title', href: 'http://jasmine.github.io/', target: '_blank'}),
  49. createDom('span', {className: 'version'}, j$.version)
  50. ),
  51. createDom('ul', {className: 'symbol-summary'}),
  52. createDom('div', {className: 'alert'}),
  53. createDom('div', {className: 'results'},
  54. createDom('div', {className: 'failures'})
  55. )
  56. );
  57. getContainer().appendChild(htmlReporterMain);
  58. symbols = find('.symbol-summary');
  59. };
  60. var totalSpecsDefined;
  61. this.jasmineStarted = function(options) {
  62. totalSpecsDefined = options.totalSpecsDefined || 0;
  63. timer.start();
  64. };
  65. var summary = createDom('div', {className: 'summary'});
  66. var topResults = new j$.ResultsNode({}, '', null),
  67. currentParent = topResults;
  68. this.suiteStarted = function(result) {
  69. currentParent.addChild(result, 'suite');
  70. currentParent = currentParent.last();
  71. };
  72. this.suiteDone = function(result) {
  73. if (currentParent == topResults) {
  74. return;
  75. }
  76. currentParent = currentParent.parent;
  77. };
  78. this.specStarted = function(result) {
  79. currentParent.addChild(result, 'spec');
  80. };
  81. var failures = [];
  82. this.specDone = function(result) {
  83. if(noExpectations(result) && console && console.error) {
  84. console.error('Spec \'' + result.fullName + '\' has no expectations.');
  85. }
  86. if (result.status != 'disabled') {
  87. specsExecuted++;
  88. }
  89. symbols.appendChild(createDom('li', {
  90. className: noExpectations(result) ? 'empty' : result.status,
  91. id: 'spec_' + result.id,
  92. title: result.fullName
  93. }
  94. ));
  95. if (result.status == 'failed') {
  96. failureCount++;
  97. var failure =
  98. createDom('div', {className: 'spec-detail failed'},
  99. createDom('div', {className: 'description'},
  100. createDom('a', {title: result.fullName, href: specHref(result)}, result.fullName)
  101. ),
  102. createDom('div', {className: 'messages'})
  103. );
  104. var messages = failure.childNodes[1];
  105. for (var i = 0; i < result.failedExpectations.length; i++) {
  106. var expectation = result.failedExpectations[i];
  107. messages.appendChild(createDom('div', {className: 'result-message'}, expectation.message));
  108. messages.appendChild(createDom('div', {className: 'stack-trace'}, expectation.stack));
  109. }
  110. failures.push(failure);
  111. }
  112. if (result.status == 'pending') {
  113. pendingSpecCount++;
  114. }
  115. };
  116. this.jasmineDone = function() {
  117. var banner = find('.banner');
  118. banner.appendChild(createDom('span', {className: 'duration'}, 'finished in ' + timer.elapsed() / 1000 + 's'));
  119. var alert = find('.alert');
  120. alert.appendChild(createDom('span', { className: 'exceptions' },
  121. createDom('label', { className: 'label', 'for': 'raise-exceptions' }, 'raise exceptions'),
  122. createDom('input', {
  123. className: 'raise',
  124. id: 'raise-exceptions',
  125. type: 'checkbox'
  126. })
  127. ));
  128. var checkbox = find('#raise-exceptions');
  129. checkbox.checked = !env.catchingExceptions();
  130. checkbox.onclick = onRaiseExceptionsClick;
  131. if (specsExecuted < totalSpecsDefined) {
  132. var skippedMessage = 'Ran ' + specsExecuted + ' of ' + totalSpecsDefined + ' specs - run all';
  133. alert.appendChild(
  134. createDom('span', {className: 'bar skipped'},
  135. createDom('a', {href: '?', title: 'Run all specs'}, skippedMessage)
  136. )
  137. );
  138. }
  139. var statusBarMessage = '';
  140. var statusBarClassName = 'bar ';
  141. if (totalSpecsDefined > 0) {
  142. statusBarMessage += pluralize('spec', specsExecuted) + ', ' + pluralize('failure', failureCount);
  143. if (pendingSpecCount) { statusBarMessage += ', ' + pluralize('pending spec', pendingSpecCount); }
  144. statusBarClassName += (failureCount > 0) ? 'failed' : 'passed';
  145. } else {
  146. statusBarClassName += 'skipped';
  147. statusBarMessage += 'No specs found';
  148. }
  149. alert.appendChild(createDom('span', {className: statusBarClassName}, statusBarMessage));
  150. var results = find('.results');
  151. results.appendChild(summary);
  152. summaryList(topResults, summary);
  153. function summaryList(resultsTree, domParent) {
  154. var specListNode;
  155. for (var i = 0; i < resultsTree.children.length; i++) {
  156. var resultNode = resultsTree.children[i];
  157. if (resultNode.type == 'suite') {
  158. var suiteListNode = createDom('ul', {className: 'suite', id: 'suite-' + resultNode.result.id},
  159. createDom('li', {className: 'suite-detail'},
  160. createDom('a', {href: specHref(resultNode.result)}, resultNode.result.description)
  161. )
  162. );
  163. summaryList(resultNode, suiteListNode);
  164. domParent.appendChild(suiteListNode);
  165. }
  166. if (resultNode.type == 'spec') {
  167. if (domParent.getAttribute('class') != 'specs') {
  168. specListNode = createDom('ul', {className: 'specs'});
  169. domParent.appendChild(specListNode);
  170. }
  171. var specDescription = resultNode.result.description;
  172. if(noExpectations(resultNode.result)) {
  173. specDescription = 'SPEC HAS NO EXPECTATIONS ' + specDescription;
  174. }
  175. specListNode.appendChild(
  176. createDom('li', {
  177. className: resultNode.result.status,
  178. id: 'spec-' + resultNode.result.id
  179. },
  180. createDom('a', {href: specHref(resultNode.result)}, specDescription)
  181. )
  182. );
  183. }
  184. }
  185. }
  186. if (failures.length) {
  187. alert.appendChild(
  188. createDom('span', {className: 'menu bar spec-list'},
  189. createDom('span', {}, 'Spec List | '),
  190. createDom('a', {className: 'failures-menu', href: '#'}, 'Failures')));
  191. alert.appendChild(
  192. createDom('span', {className: 'menu bar failure-list'},
  193. createDom('a', {className: 'spec-list-menu', href: '#'}, 'Spec List'),
  194. createDom('span', {}, ' | Failures ')));
  195. find('.failures-menu').onclick = function() {
  196. setMenuModeTo('failure-list');
  197. };
  198. find('.spec-list-menu').onclick = function() {
  199. setMenuModeTo('spec-list');
  200. };
  201. setMenuModeTo('failure-list');
  202. var failureNode = find('.failures');
  203. for (var i = 0; i < failures.length; i++) {
  204. failureNode.appendChild(failures[i]);
  205. }
  206. }
  207. };
  208. return this;
  209. function find(selector) {
  210. return getContainer().querySelector('.jasmine_html-reporter ' + selector);
  211. }
  212. function clearPrior() {
  213. // return the reporter
  214. var oldReporter = find('');
  215. if(oldReporter) {
  216. getContainer().removeChild(oldReporter);
  217. }
  218. }
  219. function createDom(type, attrs, childrenVarArgs) {
  220. var el = createElement(type);
  221. for (var i = 2; i < arguments.length; i++) {
  222. var child = arguments[i];
  223. if (typeof child === 'string') {
  224. el.appendChild(createTextNode(child));
  225. } else {
  226. if (child) {
  227. el.appendChild(child);
  228. }
  229. }
  230. }
  231. for (var attr in attrs) {
  232. if (attr == 'className') {
  233. el[attr] = attrs[attr];
  234. } else {
  235. el.setAttribute(attr, attrs[attr]);
  236. }
  237. }
  238. return el;
  239. }
  240. function pluralize(singular, count) {
  241. var word = (count == 1 ? singular : singular + 's');
  242. return '' + count + ' ' + word;
  243. }
  244. function specHref(result) {
  245. return '?spec=' + encodeURIComponent(result.fullName);
  246. }
  247. function setMenuModeTo(mode) {
  248. htmlReporterMain.setAttribute('class', 'jasmine_html-reporter ' + mode);
  249. }
  250. function noExpectations(result) {
  251. return (result.failedExpectations.length + result.passedExpectations.length) === 0 &&
  252. result.status === 'passed';
  253. }
  254. }
  255. return HtmlReporter;
  256. };
  257. jasmineRequire.HtmlSpecFilter = function() {
  258. function HtmlSpecFilter(options) {
  259. var filterString = options && options.filterString() && options.filterString().replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  260. var filterPattern = new RegExp(filterString);
  261. this.matches = function(specName) {
  262. return filterPattern.test(specName);
  263. };
  264. }
  265. return HtmlSpecFilter;
  266. };
  267. jasmineRequire.ResultsNode = function() {
  268. function ResultsNode(result, type, parent) {
  269. this.result = result;
  270. this.type = type;
  271. this.parent = parent;
  272. this.children = [];
  273. this.addChild = function(result, type) {
  274. this.children.push(new ResultsNode(result, type, this));
  275. };
  276. this.last = function() {
  277. return this.children[this.children.length - 1];
  278. };
  279. }
  280. return ResultsNode;
  281. };
  282. jasmineRequire.QueryString = function() {
  283. function QueryString(options) {
  284. this.setParam = function(key, value) {
  285. var paramMap = queryStringToParamMap();
  286. paramMap[key] = value;
  287. options.getWindowLocation().search = toQueryString(paramMap);
  288. };
  289. this.getParam = function(key) {
  290. return queryStringToParamMap()[key];
  291. };
  292. return this;
  293. function toQueryString(paramMap) {
  294. var qStrPairs = [];
  295. for (var prop in paramMap) {
  296. qStrPairs.push(encodeURIComponent(prop) + '=' + encodeURIComponent(paramMap[prop]));
  297. }
  298. return '?' + qStrPairs.join('&');
  299. }
  300. function queryStringToParamMap() {
  301. var paramStr = options.getWindowLocation().search.substring(1),
  302. params = [],
  303. paramMap = {};
  304. if (paramStr.length > 0) {
  305. params = paramStr.split('&');
  306. for (var i = 0; i < params.length; i++) {
  307. var p = params[i].split('=');
  308. var value = decodeURIComponent(p[1]);
  309. if (value === 'true' || value === 'false') {
  310. value = JSON.parse(value);
  311. }
  312. paramMap[decodeURIComponent(p[0])] = value;
  313. }
  314. }
  315. return paramMap;
  316. }
  317. }
  318. return QueryString;
  319. };