Skip to content

Commit f3445f8

Browse files
committed
test(keywords): キーワード自体をmarkdownから取得
キーワードが含まれてるから
1 parent b46e86e commit f3445f8

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"jsdom": "^6.3.0",
5151
"mdast": "^2.1.0",
5252
"mocha": "^2.2.5",
53+
"nlcst-to-string": "^1.0.0",
5354
"node-fetch": "^1.3.2",
5455
"npm-run-all": "^1.2.8",
5556
"power-assert": "^1.0.0",
@@ -60,6 +61,8 @@
6061
"textlint-rule-no-start-duplicated-conjunction": "^1.0.3",
6162
"textlint-rule-prh": "^1.0.1",
6263
"textlint-rule-spellcheck-tech-word": "^4.0.1",
64+
"unist-util-find-all-after": "^1.0.0",
65+
"unist-util-parents": "^0.1.1",
6366
"unist-util-select": "^1.0.0"
6467
},
6568
"dependencies": {
@@ -68,6 +71,7 @@
6871
"gulp": "^3.9.0",
6972
"gulp-util": "^3.0.6",
7073
"jquery": "^2.1.4",
71-
"stemming-x-keywords": "^1.0.3"
74+
"stemming-x-keywords": "^1.0.3",
75+
"unist-util-is": "^1.0.0"
7276
}
7377
}

test/keywords-test.js

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
// LICENSE : MIT
22
"use strict";
33
import {getFilePathListAsync} from "gitbook-summary-to-path";
4+
let getKeywords = require("stemming-x-keywords").getKeywords;
45
import path from "path";
56
import fs from "fs";
67
import mdast from "mdast";
7-
var select = require('unist-util-select');
8+
import parents from "unist-util-parents";
9+
import select from "unist-util-select";
10+
import isUnist from "unist-util-is";
11+
import nlcstToString from "nlcst-to-string";
12+
13+
814
const rootDir = path.join(__dirname, "..");
915
const keywordInfo = require("./keywords.json");
10-
const Org = fs.readFileSync(__dirname + "/../ORGANIZATION.md","utf-8");
11-
function getKeywords(matchPath) {
16+
const Org = fs.readFileSync(__dirname + "/../ORGANIZATION.md", "utf-8");
17+
function getSummary() {
18+
if (getSummary.cache) {
19+
return Promise.resolve(getSummary.cache);
20+
}
1221
return getFilePathListAsync(__dirname + "/../SUMMARY.md").then(fileList => {
13-
let filteredList = fileList.filter(filePath => {
14-
return filePath.indexOf(matchPath) !== -1;
15-
});
16-
return filteredList[0];
17-
}).then(filePath => {
18-
let relativePath = path.relative(rootDir, filePath);
19-
let content = fs.readFileSync(filePath, "utf-8");
20-
return {
21-
filePath,
22-
content,
23-
keywords: keywordInfo[relativePath]
24-
};
22+
getSummary.cache = fileList;
23+
return fileList;
2524
});
2625
}
2726

@@ -31,23 +30,61 @@ function isNotContain(content, keywords) {
3130
return content.indexOf(keyword) === -1;
3231
});
3332
}
33+
function findAllAfter(ast, node, type) {
34+
let results = [];
35+
let children = ast.children;
36+
let index = 0, length = children.length;
37+
let isFound = false;
38+
while (++index < length) {
39+
let child = children[index];
40+
if (isUnist(node, child)) {
41+
isFound = true;
42+
} else if (isFound) {
43+
if (isUnist(type, child)) {
44+
results.push(child);
45+
} else {
46+
break;
47+
}
48+
}
49+
}
50+
51+
return results;
52+
}
3453
// キーワードが書くコンテンツに含まれているかをテストする
3554
describe("keywords", function () {
36-
it("Each chapter contain the keyword", function (done) {
55+
it("Each chapter contain the keyword", function () {
3756
let ast = mdast.parse(Org);
38-
let headingPrs = select(ast, "heading, heading + paragraph");
39-
let promises = Object.keys(keywordInfo).map(key => {
40-
return getKeywords(key).then(({filePath, content, keywords}) => {
57+
let headerLinks = select(parents(ast), "heading link[href]");
58+
let filePathList = headerLinks.map(link => {
59+
return path.resolve(rootDir, link.href);
60+
});
61+
let paragraphList = headerLinks.map(link => {
62+
let filePath = path.resolve(rootDir, link.href);
63+
let paragraphs = findAllAfter(ast, link.parent.node, "paragraph");
64+
let results = {
65+
filePath: filePath,
66+
content: fs.readFileSync(filePath, "utf-8"),
67+
keywords: []
68+
};
69+
let keywords = paragraphs.map(p => {
70+
let text = nlcstToString(p);
71+
return getKeywords(text).then(a => {
72+
results.keywords = results.keywords.concat(a)
73+
});
74+
});
75+
return Promise.all(keywords).then(()=> {
76+
return results;
77+
});
78+
});
79+
let promises = Promise.all(paragraphList).then(results => {
80+
return results.forEach(({filePath, content, keywords}) => {
4181
let unusedKeywords = isNotContain(content, keywords);
4282
if (unusedKeywords.length === 0) {
43-
return Promise.resolve();
44-
} else {
45-
return Promise.reject(new Error(`"${unusedKeywords.join(",")}" are not used in ${filePath}`));
83+
return;
4684
}
85+
throw new Error(`"${unusedKeywords.join(",")}" are not used in ${filePath}`);
4786
});
4887
});
49-
Promise.all(promises).then(() => {
50-
done();
51-
}, done);
88+
return promises;
5289
});
5390
});

test/mocha.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
--timeout 5000
12
--recursive
23
--compilers js:espower-babel/guess

0 commit comments

Comments
 (0)