Skip to content

Commit

Permalink
Chore: add core rule's tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Dec 17, 2016
1 parent b16a6c1 commit 848f29d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "test/fixtures/eslint"]
path = test/fixtures/eslint
url = https://github.com/eslint/eslint.git
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"clean": "rimraf .nyc_output coverage",
"coverage": "nyc report --reporter lcov && opener ./coverage/lcov-report/index.html",
"lint": "eslint index.js \"test/*.js\"",
"postinstall": "git submodule update --init && cd test/fixtures/eslint && npm install",
"postversion": "git push && git push --tags",
"pretest": "npm run lint",
"preversion": "npm test",
Expand Down
106 changes: 106 additions & 0 deletions test/core-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const path = require("path")
const fs = require("fs-extra")
const RuleTester = require("./fixtures/eslint/lib/testers/rule-tester")

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const RULES_ROOT = path.join(__dirname, "fixtures/eslint/tests/lib/rules")
const PARSER_PATH = path.resolve(__dirname, "../index.js")
const originalRun = RuleTester.prototype.run

/**
* Wrap the given code with a `<script>` tag.
*
* @param {string} code - The code to be wrapped.
* @returns {string} The wrapped code.
*/
function wrapCode(code) {
const eol = code.indexOf("\r\n") !== -1 ? "\r\n" : "\n"
return `<script>${eol}${code}${eol}</script>`
}

/**
* Modify the given test pattern to test with vue-eslint-parser.
*
* @param {string|object} pattern - The test pattern to be modified.
* @returns {object|null} The modified pattern.
*/
function modifyPattern(pattern) {
if (typeof pattern === "string") {
return {
code: wrapCode(pattern),
filename: "test.vue",
parser: PARSER_PATH,
}
}
if (pattern.parser != null || pattern.filename != null) {
return null
}

pattern.filename = "test.vue"
pattern.parser = PARSER_PATH
pattern.code = wrapCode(pattern.code)
if (pattern.output != null) {
pattern.output = wrapCode(pattern.output)
}
if (Array.isArray(pattern.errors)) {
for (const error of pattern.errors) {
if (typeof error === "object") {
if (error.line != null) {
error.line += 1
}
if (error.endLine != null) {
error.endLine += 1
}
}
}
}

return pattern
}

/**
* Run the given tests.
* This is used to replace `RuleTester.prototype.run`.
*
* @this {RuleTester}
* @param {string} ruleId - The rule ID.
* @param {object} impl - The rule implementation to be tested.
* @param {object} patterns - The test patterns.
* @returns {void}
*/
function overrideRun(ruleId, impl, patterns) {
return originalRun.call(this, ruleId, impl, {
valid: patterns.valid.map(modifyPattern).filter(Boolean),
invalid: patterns.invalid.map(modifyPattern).filter(Boolean),
})
}

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

RuleTester.prototype.run = overrideRun
try {
describe("Tests of ESLint core rules", () => {
for (const fileName of fs.readdirSync(RULES_ROOT)) {
require(path.join(RULES_ROOT, fileName))
}
})
}
finally {
RuleTester.prototype.run = originalRun
}
1 change: 1 addition & 0 deletions test/fixtures/eslint
Submodule eslint added at b4f88a

0 comments on commit 848f29d

Please sign in to comment.