/
test-util.ts
177 lines (170 loc) · 5.57 KB
/
test-util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// LICENSE : MIT
"use strict";
import * as assert from "assert";
import * as fs from "fs";
import { TextLintCore } from "textlint";
import { TextlintResult } from "@textlint/kernel";
/**
*
* @param {string} [text]
* @param {string} [inputPath]
* @returns {string}
*/
export function getTestText({ text, inputPath }: { text?: string; inputPath?: string }) {
if (typeof inputPath === "string") {
return fs.readFileSync(inputPath, "utf-8");
}
if (typeof text === "string") {
return text;
}
throw new Error("should define either one of { text } or { inputPath }");
}
export type InvalidPattern = {
textlint: TextLintCore;
inputPath?: string;
text?: string;
ext?: string;
errors: any[];
};
/**
* @param {TextLintCore} textlint
* @param {string} [text]
* @param {string} [ext]
* @param {string} [inputPath]
* @param {*[]} errors
*/
export function testInvalid({ textlint, inputPath, text, ext, errors }: InvalidPattern) {
const actualText = getTestText({ text, inputPath });
const lines = actualText.split(/\n/);
assert.strictEqual(
typeof actualText,
"string",
`invalid property should have text string
e.g.)
invalid : [
{
text: "example text",
errors: [{
message: "expected message"
}]
}
]
`
);
assert.ok(
Array.isArray(errors),
`invalid property should have array of expected error
e.g.)
invalid : [
{
text: "example text",
errors: [{
message: "expected message"
}]
}
]
`
);
const errorLength = errors.length;
let promise: Promise<TextlintResult>;
if (inputPath !== undefined) {
promise = textlint.lintFile(inputPath);
} else if (text !== undefined) {
promise = textlint.lintText(text, ext);
} else {
throw new Error("Should set `text` or `inputPath`");
}
return promise.then(lintResult => {
assert.strictEqual(
lintResult.messages.length,
errorLength,
`invalid: should have ${errorLength} errors but had ${lintResult.messages.length}:
===Text===:
${actualText}
==Result==:
${JSON.stringify(lintResult, null, 4)}`
);
errors.forEach((error, errorIndex) => {
const { ruleId, message, line, column, index } = error;
const resultMessageObject = lintResult.messages[errorIndex];
// check
assert.ok(
resultMessageObject.line >= 1,
`lint result's line number is ${resultMessageObject.line}, should be over than 1.`
);
assert.ok(
resultMessageObject.line <= lines.length,
`lint result's line number is line:${resultMessageObject.line}, but total line number of the text is ${
lines.length
}.
The result's line number should be less than ${lines.length}`
);
const columnText = lines[resultMessageObject.line - 1];
assert.ok(
resultMessageObject.column >= 1,
`lint result's column number is ${resultMessageObject.column}, should be over than 1.`
);
assert.ok(
resultMessageObject.column <= columnText.length + 1,
`lint result's column number is ${resultMessageObject.column},` +
`but the length of the text @ line:${resultMessageObject.line} is ${columnText.length + 1}.
The result's column number should be less than ${columnText.length + 1}`
);
if (ruleId !== undefined) {
const resultRuleId = resultMessageObject.ruleId;
assert.strictEqual(resultRuleId, ruleId, `"ruleId should be "${ruleId}"`);
}
if (message !== undefined) {
const resultMessage = resultMessageObject.message;
assert.strictEqual(resultMessage, message, `"message should be "${message}"`);
}
if (line !== undefined) {
const resultLine = resultMessageObject.line;
assert.strictEqual(resultLine, line, `line should be ${line}`);
}
if (column !== undefined) {
const resultColumn = resultMessageObject.column;
assert.strictEqual(resultColumn, column, `"column should be ${column}`);
}
if (index !== undefined) {
const resultIndex = resultMessageObject.index;
assert.strictEqual(resultIndex, index, `"index should be ${index}`);
}
});
});
}
export type ValidPattern = {
textlint: TextLintCore;
inputPath?: string;
text?: string;
ext?: string;
};
/**
* @param {TextLintCore} textlint
* @param {string} [inputPath]
* @param {string} [text]
* @param {string} [ext]
*/
export function testValid({ textlint, inputPath, text, ext }: ValidPattern) {
const actualText = getTestText({ text, inputPath });
assert.strictEqual(typeof actualText, "string", "valid should has string of text.");
let promise: Promise<TextlintResult>;
if (inputPath !== undefined) {
promise = textlint.lintFile(inputPath);
} else if (text !== undefined) {
promise = textlint.lintText(text, ext);
} else {
throw new Error("Should set `text` or `inputPath`");
}
return promise.then(results => {
assert.strictEqual(
results.messages.length,
0,
`valid: should have no errors but had Error results:
===Text===:
${actualText}
==Result==:
${JSON.stringify(results, null, 4)}`
);
});
}