Skip to content

Commit cffa9c7

Browse files
committed
Merge pull request #83 from azu/keywords-testing
test: add keywords test
2 parents 9504f14 + 40a86dd commit cffa9c7

File tree

4 files changed

+113
-6
lines changed

4 files changed

+113
-6
lines changed

ORGANIZATION.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
# この書籍の内容について
22

3-
## [jQuery](/ja/jQuery/README.md)
3+
## [Introduction](ja/introduction/README.md)
4+
5+
この書籍の目的について書かれています。
6+
なぜプラグインアーキテクチャを学ぶ必要があるのか、どのようにして学ぶことができるのかについて書かれています。
7+
8+
## [jQuery](ja/jQuery/README.md)
49

510
jQueryのプラグインについて解説しています。
611
`<script>`タグをベースとしたプラグインアーキテクチャについて解説しています。
712

8-
## [ESLint](/ja/ESLint/README.md)
13+
## [ESLint](ja/ESLint/README.md)
914

1015
ESLintのルールを拡張する仕組みについて解説しています。
1116
ESLintではJavaScriptのコードをパースして作成されたASTを元にコードのLintを行います。
1217
実際にESLintのルールを解釈できる小さな実装を作りながらプラグインの仕組みについて学びます。
1318

14-
## [Connect](/ja/connect/README.md)
19+
## [Connect](ja/connect/README.md)
1520

1621
Connectの **middleware** と呼ばれるプラグインアーキテクチャについて解説しています。
1722
Node.js以外においても_Rack_などHTTPサーバーでよく見られるプラグインを使った階層構造について学びます。
1823

19-
## [gulp](/ja/gulp/README.md)
24+
## [gulp](ja/gulp/README.md)
2025

2126
**タスク自動化ツール** であるgulpのプラグインアーキテクチャについて解説しています。

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
"gitbook-plugin-japanese-support": "0.0.1",
4848
"gitbook-plugin-richquotes": "0.0.5",
4949
"gitbook-summary-to-path": "^1.0.1",
50-
"jsdom": "^6.3.0",
50+
"jsdom": "^7.0.1",
51+
"mdast": "^2.1.0",
5152
"mocha": "^2.2.5",
53+
"nlcst-to-string": "^1.0.0",
5254
"node-fetch": "^1.3.2",
5355
"npm-run-all": "^1.2.8",
5456
"power-assert": "^1.0.0",
@@ -58,7 +60,11 @@
5860
"textlint-rule-no-mix-dearu-desumasu": "^1.1.0",
5961
"textlint-rule-no-start-duplicated-conjunction": "^1.0.3",
6062
"textlint-rule-prh": "^2.0.0",
61-
"textlint-rule-spellcheck-tech-word": "^4.0.1"
63+
"textlint-rule-spellcheck-tech-word": "^4.0.1",
64+
"stemming-x-keywords": "^1.0.3",
65+
"unist-util-is": "^1.0.0",
66+
"unist-util-parents": "^0.1.1",
67+
"unist-util-select": "^1.0.0"
6268
},
6369
"dependencies": {
6470
"esprima": "^2.5.0",

test/keywords-test.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import {getKeywords} from "stemming-x-keywords";
4+
import path from "path";
5+
import fs from "fs";
6+
import mdast from "mdast";
7+
import parents from "unist-util-parents";
8+
import select from "unist-util-select";
9+
import isUnist from "unist-util-is";
10+
import nlcstToString from "nlcst-to-string";
11+
12+
const rootDir = path.join(__dirname, "..");
13+
const OrganizationText = fs.readFileSync(path.join(rootDir, "ORGANIZATION.md"), "utf-8");
14+
function isNotContain(content, keywords) {
15+
// 含んでないものだけを返す
16+
return keywords.filter(keyword => {
17+
return content.indexOf(keyword) === -1;
18+
});
19+
}
20+
function findAllAfter(ast, node, type) {
21+
let results = [];
22+
let children = ast.children;
23+
let index = 0, length = children.length;
24+
let isFound = false;
25+
while (++index < length) {
26+
let child = children[index];
27+
if (isUnist(node, child)) {
28+
isFound = true;
29+
} else if (isFound) {
30+
if (isUnist(type, child)) {
31+
results.push(child);
32+
} else {
33+
break;
34+
}
35+
}
36+
}
37+
38+
return results;
39+
}
40+
41+
function isAlreadyCheckKeyword(list, keyword) {
42+
return list.indexOf(keyword) !== -1;
43+
}
44+
45+
// P ASTからキーワードを抽出する
46+
function getKeywordsOfParagraphsAsync(paragraphs) {
47+
let _keywords = [];
48+
let promiseList = paragraphs.map(p => {
49+
let text = nlcstToString(p);
50+
return getKeywords(text).then(keywords => {
51+
_keywords = _keywords.concat(keywords);
52+
});
53+
});
54+
return Promise.all(promiseList).then(()=> {
55+
return _keywords;
56+
});
57+
}
58+
function checkKeyword(text) {
59+
let ast = mdast.parse(text);
60+
let headerLinks = select(parents(ast), "heading link[href]");
61+
let paragraphList = headerLinks.map(link => {
62+
let filePath = path.resolve(rootDir, link.href);
63+
let paragraphs = findAllAfter(ast, link.parent.node, "paragraph");
64+
return getKeywordsOfParagraphsAsync(paragraphs).then(keywords => {
65+
return {
66+
filePath: filePath,
67+
content: fs.readFileSync(filePath, "utf-8"),
68+
keywords: keywords
69+
};
70+
});
71+
});
72+
return Promise.all(paragraphList).then(results => {
73+
let confirmedKeywords = [];
74+
return results.forEach(({filePath, content, keywords}) => {
75+
let unusedKeywords = isNotContain(content, keywords);
76+
let isChecked = isAlreadyCheckKeyword.bind(null, confirmedKeywords);
77+
if (unusedKeywords.length === 0) {
78+
// 使用済みのキーワードを登録
79+
confirmedKeywords = confirmedKeywords.concat(keywords);
80+
return;
81+
}
82+
if (unusedKeywords.every(isChecked)) {
83+
console.log(unusedKeywords.join(",") + "はチェック済み");
84+
return;
85+
}
86+
throw new Error(`"${unusedKeywords.join(",")}" are not used in ${filePath}`);
87+
});
88+
});
89+
}
90+
// キーワードが書くコンテンツに含まれているかをテストする
91+
describe("keywords", function () {
92+
it("Each chapter contain the keyword", function () {
93+
return checkKeyword(OrganizationText);
94+
});
95+
});

test/mocha.opts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
--timeout 100000
12
--recursive
23
--compilers js:espower-babel/guess

0 commit comments

Comments
 (0)