-
-
Notifications
You must be signed in to change notification settings - Fork 575
/
Copy pathdoctest.js
50 lines (49 loc) · 1.9 KB
/
doctest.js
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
const { test } = require("@power-doctest/tester");
const { parse } = require("@power-doctest/asciidoctor");
const globby = require("globby");
const fs = require("fs");
const path = require("path");
// = の段落が順番でないため do not support nested sections が発生する問題を回避する
// ダミー文字列に書き換える
const replaceDummyHeader = (content) => {
return content.split("\n").map(line => {
return line.replace(/^(=+)/g, (all, match) => {
return "♪".repeat(match.length);
});
}).join("\n");
};
describe("doctest:adoc", function () {
const sourceDir = path.join(__dirname, "..", "Ch5_AsyncFunction");
const files = globby.sync([
`${sourceDir}/**/*.adoc`,
`!**/node_modules{,/**}`,
]);
files.forEach(filePath => {
const normalizeFilePath = filePath.replace(sourceDir, "");
describe(`${normalizeFilePath}`, function () {
const content = fs.readFileSync(filePath, "utf-8");
const parsedCodes = parse({
filePath,
content: replaceDummyHeader(content)
});
// try to eval
const dirName = path.dirname(filePath).split(path.sep).pop();
parsedCodes.forEach((parsedCode, index) => {
const codeValue = parsedCode.code;
const testCaseName = codeValue.slice(0, 32).replace(/[\r\n]/g, "_");
it(dirName + ": " + testCaseName, function () {
return test(parsedCode).catch(error => {
const filePathLineColumn = `${error.fileName}:${error.lineNumber}:${error.columnNumber}`;
console.error(`Asciidoc Doctest is failed
at ${filePathLineColumn}
----------
${codeValue}
----------
`);
return Promise.reject(error);
});
});
});
});
});
});