testValidPattern(name: string, param: TextlintRuleModule | TestConfig, valid: TesterValid) { const text = typeof valid === "object" ? valid.text : valid; const inputPath = typeof valid === "object" ? valid.inputPath : undefined; const ext = typeof valid === "object" && valid.ext !== undefined ? valid.ext : ".md"; const textlint = new TextLintCore(); if (isTestConfig(param)) { const testRuleSet = createTestRuleSet(param.rules); textlint.setupRules(testRuleSet.rules, testRuleSet.rulesOptions); if (param.plugins !== undefined) { const testPluginSet = createTestPluginSet(param.plugins); textlint.setupPlugins(testPluginSet.plugins, testPluginSet.pluginOptions); } } else { const options = typeof valid === "object" ? valid.options : // just enable true; textlint.setupRules( { [name]: param }, { [name]: options } ); } it(inputPath || text, () => { return testValid({ textlint, inputPath, text, ext }); }); }
testInvalidPattern(name: string, param: TextlintRuleModule | TestConfig, invalid: TesterInvalid) { const errors = invalid.errors; const inputPath = invalid.inputPath; const text = invalid.text; const ext = invalid.ext !== undefined ? invalid.ext : ".md"; const textlint = new TextLintCore(); if (isTestConfig(param)) { const testRuleSet = createTestRuleSet(param.rules); textlint.setupRules(testRuleSet.rules, testRuleSet.rulesOptions); if (Array.isArray(param.plugins)) { const testPluginSet = createTestPluginSet(param.plugins); textlint.setupPlugins(testPluginSet.plugins, testPluginSet.pluginOptions); } } else { const options = invalid.options; textlint.setupRules( { [name]: param }, { [name]: options } ); } it(inputPath || text, () => { return testInvalid({ textlint, inputPath, text, ext, errors }); }); // --fix if (invalid.hasOwnProperty("output")) { it(`Fixer: ${inputPath || text}`, () => { if (isTestConfig(param)) { param.rules.forEach(rule => { assertHasFixer(rule.rule, rule.ruleId); }); } else { assertHasFixer(param, name); } let promise: Promise<TextlintFixResult>; if (inputPath !== undefined) { promise = textlint.fixFile(inputPath); } else if (text !== undefined) { promise = textlint.fixText(text, ext); } else { throw new Error("Should set `text` or `inputPath`"); } return promise.then(result => { const output = invalid.output; assert.strictEqual(result.output, output); }); }); } }
it(`should ${caseName.split("-").join(" ")} throw assertion Error`, () => { const fixtureRule = path.join(fixturesDir, caseName); const textlint = new TextLintCore(); textlint.setupRules({ "broken-rule": require(fixtureRule) }); return testInvalid({ textlint, text: "text", ext: ".md", errors: [{ message: "Found TODO: '- [ ] string'", line: 1, column: 3 }] }) .then(() => { throw new Error("WRONG"); }) .catch(error => { assert(error.code === "ERR_ASSERTION" || error.name === "AssertionError"); }); });
it(`Fixer: ${inputPath || text}`, () => { if (isTestConfig(param)) { param.rules.forEach(rule => { assertHasFixer(rule.rule, rule.ruleId); }); } else { assertHasFixer(param, name); } let promise: Promise<TextlintFixResult>; if (inputPath !== undefined) { promise = textlint.fixFile(inputPath); } else if (text !== undefined) { promise = textlint.fixText(text, ext); } else { throw new Error("Should set `text` or `inputPath`"); } return promise.then(result => { const output = invalid.output; assert.strictEqual(result.output, output); }); });