Skip to content

Commit 848f29d

Browse files
committed
Chore: add core rule's tests
1 parent b16a6c1 commit 848f29d

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "test/fixtures/eslint"]
2+
path = test/fixtures/eslint
3+
url = https://github.com/eslint/eslint.git

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"clean": "rimraf .nyc_output coverage",
1212
"coverage": "nyc report --reporter lcov && opener ./coverage/lcov-report/index.html",
1313
"lint": "eslint index.js \"test/*.js\"",
14+
"postinstall": "git submodule update --init && cd test/fixtures/eslint && npm install",
1415
"postversion": "git push && git push --tags",
1516
"pretest": "npm run lint",
1617
"preversion": "npm test",

test/core-rules.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @author Toru Nagashima <https://github.com/mysticatea>
3+
* @copyright 2016 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const path = require("path")
13+
const fs = require("fs-extra")
14+
const RuleTester = require("./fixtures/eslint/lib/testers/rule-tester")
15+
16+
//------------------------------------------------------------------------------
17+
// Helpers
18+
//------------------------------------------------------------------------------
19+
20+
const RULES_ROOT = path.join(__dirname, "fixtures/eslint/tests/lib/rules")
21+
const PARSER_PATH = path.resolve(__dirname, "../index.js")
22+
const originalRun = RuleTester.prototype.run
23+
24+
/**
25+
* Wrap the given code with a `<script>` tag.
26+
*
27+
* @param {string} code - The code to be wrapped.
28+
* @returns {string} The wrapped code.
29+
*/
30+
function wrapCode(code) {
31+
const eol = code.indexOf("\r\n") !== -1 ? "\r\n" : "\n"
32+
return `<script>${eol}${code}${eol}</script>`
33+
}
34+
35+
/**
36+
* Modify the given test pattern to test with vue-eslint-parser.
37+
*
38+
* @param {string|object} pattern - The test pattern to be modified.
39+
* @returns {object|null} The modified pattern.
40+
*/
41+
function modifyPattern(pattern) {
42+
if (typeof pattern === "string") {
43+
return {
44+
code: wrapCode(pattern),
45+
filename: "test.vue",
46+
parser: PARSER_PATH,
47+
}
48+
}
49+
if (pattern.parser != null || pattern.filename != null) {
50+
return null
51+
}
52+
53+
pattern.filename = "test.vue"
54+
pattern.parser = PARSER_PATH
55+
pattern.code = wrapCode(pattern.code)
56+
if (pattern.output != null) {
57+
pattern.output = wrapCode(pattern.output)
58+
}
59+
if (Array.isArray(pattern.errors)) {
60+
for (const error of pattern.errors) {
61+
if (typeof error === "object") {
62+
if (error.line != null) {
63+
error.line += 1
64+
}
65+
if (error.endLine != null) {
66+
error.endLine += 1
67+
}
68+
}
69+
}
70+
}
71+
72+
return pattern
73+
}
74+
75+
/**
76+
* Run the given tests.
77+
* This is used to replace `RuleTester.prototype.run`.
78+
*
79+
* @this {RuleTester}
80+
* @param {string} ruleId - The rule ID.
81+
* @param {object} impl - The rule implementation to be tested.
82+
* @param {object} patterns - The test patterns.
83+
* @returns {void}
84+
*/
85+
function overrideRun(ruleId, impl, patterns) {
86+
return originalRun.call(this, ruleId, impl, {
87+
valid: patterns.valid.map(modifyPattern).filter(Boolean),
88+
invalid: patterns.invalid.map(modifyPattern).filter(Boolean),
89+
})
90+
}
91+
92+
//------------------------------------------------------------------------------
93+
// Tests
94+
//------------------------------------------------------------------------------
95+
96+
RuleTester.prototype.run = overrideRun
97+
try {
98+
describe("Tests of ESLint core rules", () => {
99+
for (const fileName of fs.readdirSync(RULES_ROOT)) {
100+
require(path.join(RULES_ROOT, fileName))
101+
}
102+
})
103+
}
104+
finally {
105+
RuleTester.prototype.run = originalRun
106+
}

test/fixtures/eslint

Submodule eslint added at b4f88a9

0 commit comments

Comments
 (0)