From 747e3c31b9637e41c2de3f7110192f2b5c9d998e Mon Sep 17 00:00:00 2001 From: David Anson Date: Tue, 17 Mar 2015 22:34:47 -0700 Subject: [PATCH] Add tests for docs and styles, add missing tag. --- .eslintrc | 2 +- README.md | 1 + lib/rules.js | 2 +- test/markdownlint-test.js | 106 +++++++++++++++++++++++++++++++++++++- 4 files changed, 107 insertions(+), 4 deletions(-) diff --git a/.eslintrc b/.eslintrc index f4b0502a..b14692cd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -126,7 +126,7 @@ "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "max-depth": [2, 4], "max-len": [2, 80, 2], - "max-nested-callbacks": [2, 3], + "max-nested-callbacks": [2, 5], "max-params": [2, 4], "max-statements": [0, 10], "new-cap": 2, diff --git a/README.md b/README.md index b9b8d60e..fbe3c0e0 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ See [Rules.md](doc/Rules.md) for more details. * **blockquote** - MD027, MD028 * **bullet** - MD004, MD005, MD006, MD007, MD032 * **code** - MD014, MD031 +* **hard_tab** - MD010 * **headers** - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026 * **indentation** - MD005, MD006, MD007, MD027 diff --git a/lib/rules.js b/lib/rules.js index 6447320a..abe48bf9 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -247,7 +247,7 @@ module.exports = [ { "name": "MD010", "desc": "Hard tabs", - "tags": [ "whitespace" ], + "tags": [ "whitespace", "hard_tab" ], "func": function MD010(params, errors) { params.lines.forEach(function forLine(line, lineIndex) { if (/\t/.test(line)) { diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index 529408d2..064f271d 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -2,9 +2,11 @@ var fs = require("fs"); var path = require("path"); +var md = require("markdown-it")(); var Q = require("q"); var markdownlint = require("../lib/markdownlint"); var shared = require("../lib/shared"); +var rules = require("../lib/rules"); function createTestForFile(file) { return function testForFile(test) { @@ -64,10 +66,10 @@ function createTestForFile(file) { } /* eslint-disable no-sync, for synchronous test method creation */ -fs.readdirSync(__dirname).forEach(function forFile(file) { +fs.readdirSync("./test").forEach(function forFile(file) { /* eslint-enable no-sync */ if (file.match(/\.md$/)) { - module.exports[file] = createTestForFile(path.join(__dirname, file)); + module.exports[file] = createTestForFile(path.join("./test", file)); } }); @@ -280,6 +282,17 @@ module.exports.enableTag = function enableTag(test) { }); }; +module.exports.styleFiles = function styleFiles(test) { + test.expect(4); + fs.readdir("./style", function readdir(err, files) { + test.ifError(err); + files.forEach(function forFile(file) { + test.ok(require(path.join("../style", file)), "Unable to load/parse."); + }); + test.done(); + }); +}; + module.exports.styleAll = function styleAll(test) { test.expect(2); var options = { @@ -408,3 +421,92 @@ module.exports.badFile = function badFile(test) { test.done(); }); }; + +module.exports.readme = function readme(test) { + test.expect(143); + fs.readFile("README.md", shared.utf8Encoding, + function readFile(err, contents) { + test.ifError(err); + var rulesLeft = rules.slice(); + var seenRules = false; + var inRules = false; + var seenTags = false; + var inTags = false; + var docTags = []; + var usedTags = []; + md.parse(contents, {}).forEach(function forToken(token) { + if (token.type === "bullet_list_open") { + if (!seenRules) { + seenRules = true; + inRules = true; + } else if (!seenTags) { + seenTags = true; + inTags = true; + } + } else if (token.type === "bullet_list_close") { + inRules = false; + inTags = false; + } else if (token.type === "inline") { + if (inRules) { + var rule = rulesLeft.shift(); + var expected = "**" + rule.name + "** - " + rule.desc; + test.equal(token.content, expected, "Rule mismatch."); + } else if (inTags) { + var parts = token.content.replace(/\*\*/g, "").split(/ - |, |,\n/); + var tag = parts.shift(); + docTags.push(tag); + parts.forEach(function forPart(part) { + var found = rules.some(function forRule(r) { + if (r.name === part) { + test.ok(r.tags.indexOf(tag) !== -1, "Missing tag."); + r.tags.forEach(function forTag(t) { + if (usedTags.indexOf(t) === -1) { + usedTags.push(t); + } + }); + return true; + } + return false; + }); + test.ok(found, "Unknown rule." + part); + }); + } + } + }); + test.ok(!rulesLeft.length, "Missing rule."); + test.deepEqual(docTags, usedTags.sort(), "Tag mismatch."); + test.done(); + }); +}; + +module.exports.doc = function doc(test) { + test.expect(87); + fs.readFile("doc/Rules.md", shared.utf8Encoding, + function readFile(err, contents) { + test.ifError(err); + var rulesLeft = rules.slice(); + var inHeading = false; + var rule = null; + md.parse(contents, {}).forEach(function forToken(token) { + if ((token.type === "heading_open") && (token.hLevel === 2)) { + inHeading = true; + } else if (token.type === "heading_close") { + inHeading = false; + } else if (token.type === "inline") { + if (inHeading) { + test.ok(!rule, "Missing tags."); + rule = rulesLeft.shift(); + var expected = rule.name + " - " + rule.desc; + test.equal(token.content, expected, "Rule mismatch."); + } else if (/^Tags: /.test(token.content)) { + var tags = token.content.split(/, |: | /).slice(1); + test.deepEqual(tags, rule.tags, "Tag mismatch."); + rule = null; + } + } + }); + test.ok(!rulesLeft.length, "Missing rule."); + test.ok(!rule, "Missing tags."); + test.done(); + }); +};