aboutsummaryrefslogtreecommitdiff
path: root/node_modules/mocha/lib/reporters/markdown.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/mocha/lib/reporters/markdown.js')
-rw-r--r--node_modules/mocha/lib/reporters/markdown.js97
1 files changed, 97 insertions, 0 deletions
diff --git a/node_modules/mocha/lib/reporters/markdown.js b/node_modules/mocha/lib/reporters/markdown.js
new file mode 100644
index 000000000..680c55d70
--- /dev/null
+++ b/node_modules/mocha/lib/reporters/markdown.js
@@ -0,0 +1,97 @@
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base');
+var utils = require('../utils');
+
+/**
+ * Constants
+ */
+
+var SUITE_PREFIX = '$';
+
+/**
+ * Expose `Markdown`.
+ */
+
+exports = module.exports = Markdown;
+
+/**
+ * Initialize a new `Markdown` reporter.
+ *
+ * @api public
+ * @param {Runner} runner
+ */
+function Markdown(runner) {
+ Base.call(this, runner);
+
+ var level = 0;
+ var buf = '';
+
+ function title(str) {
+ return Array(level).join('#') + ' ' + str;
+ }
+
+ function mapTOC(suite, obj) {
+ var ret = obj;
+ var key = SUITE_PREFIX + suite.title;
+
+ obj = obj[key] = obj[key] || { suite: suite };
+ suite.suites.forEach(function(suite) {
+ mapTOC(suite, obj);
+ });
+
+ return ret;
+ }
+
+ function stringifyTOC(obj, level) {
+ ++level;
+ var buf = '';
+ var link;
+ for (var key in obj) {
+ if (key === 'suite') {
+ continue;
+ }
+ if (key !== SUITE_PREFIX) {
+ link = ' - [' + key.substring(1) + ']';
+ link += '(#' + utils.slug(obj[key].suite.fullTitle()) + ')\n';
+ buf += Array(level).join(' ') + link;
+ }
+ buf += stringifyTOC(obj[key], level);
+ }
+ return buf;
+ }
+
+ function generateTOC(suite) {
+ var obj = mapTOC(suite, {});
+ return stringifyTOC(obj, 0);
+ }
+
+ generateTOC(runner.suite);
+
+ runner.on('suite', function(suite) {
+ ++level;
+ var slug = utils.slug(suite.fullTitle());
+ buf += '<a name="' + slug + '"></a>' + '\n';
+ buf += title(suite.title) + '\n';
+ });
+
+ runner.on('suite end', function() {
+ --level;
+ });
+
+ runner.on('pass', function(test) {
+ var code = utils.clean(test.body);
+ buf += test.title + '.\n';
+ buf += '\n```js\n';
+ buf += code + '\n';
+ buf += '```\n\n';
+ });
+
+ runner.on('end', function() {
+ process.stdout.write('# TOC\n');
+ process.stdout.write(generateTOC(runner.suite));
+ process.stdout.write(buf);
+ });
+}