doc.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. /**
  3. * @module Doc
  4. */
  5. /**
  6. * Module dependencies.
  7. */
  8. var Base = require('./base');
  9. var utils = require('../utils');
  10. var constants = require('../runner').constants;
  11. var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
  12. var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
  13. var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
  14. var EVENT_SUITE_END = constants.EVENT_SUITE_END;
  15. /**
  16. * Expose `Doc`.
  17. */
  18. exports = module.exports = Doc;
  19. /**
  20. * Initialize a new `Doc` reporter.
  21. *
  22. * @class
  23. * @memberof Mocha.reporters
  24. * @extends {Base}
  25. * @public
  26. * @param {Runner} runner
  27. */
  28. function Doc(runner) {
  29. Base.call(this, runner);
  30. var indents = 2;
  31. function indent() {
  32. return Array(indents).join(' ');
  33. }
  34. runner.on(EVENT_SUITE_BEGIN, function(suite) {
  35. if (suite.root) {
  36. return;
  37. }
  38. ++indents;
  39. console.log('%s<section class="suite">', indent());
  40. ++indents;
  41. console.log('%s<h1>%s</h1>', indent(), utils.escape(suite.title));
  42. console.log('%s<dl>', indent());
  43. });
  44. runner.on(EVENT_SUITE_END, function(suite) {
  45. if (suite.root) {
  46. return;
  47. }
  48. console.log('%s</dl>', indent());
  49. --indents;
  50. console.log('%s</section>', indent());
  51. --indents;
  52. });
  53. runner.on(EVENT_TEST_PASS, function(test) {
  54. console.log('%s <dt>%s</dt>', indent(), utils.escape(test.title));
  55. var code = utils.escape(utils.clean(test.body));
  56. console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);
  57. });
  58. runner.on(EVENT_TEST_FAIL, function(test, err) {
  59. console.log(
  60. '%s <dt class="error">%s</dt>',
  61. indent(),
  62. utils.escape(test.title)
  63. );
  64. var code = utils.escape(utils.clean(test.body));
  65. console.log(
  66. '%s <dd class="error"><pre><code>%s</code></pre></dd>',
  67. indent(),
  68. code
  69. );
  70. console.log('%s <dd class="error">%s</dd>', indent(), utils.escape(err));
  71. });
  72. }
  73. Doc.description = 'HTML documentation';