Skip to content

test: add keywords test #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions ORGANIZATION.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# この書籍の内容について

## [jQuery](/ja/jQuery/README.md)
## [Introduction](ja/introduction/README.md)

この書籍の目的について書かれています。
なぜプラグインアーキテクチャを学ぶ必要があるのか、どのようにして学ぶことができるのかについて書かれています。

## [jQuery](ja/jQuery/README.md)

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

## [ESLint](/ja/ESLint/README.md)
## [ESLint](ja/ESLint/README.md)

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

## [Connect](/ja/connect/README.md)
## [Connect](ja/connect/README.md)

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

## [gulp](/ja/gulp/README.md)
## [gulp](ja/gulp/README.md)

**タスク自動化ツール** であるgulpのプラグインアーキテクチャについて解説しています。
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
"gitbook-plugin-japanese-support": "0.0.1",
"gitbook-plugin-richquotes": "0.0.5",
"gitbook-summary-to-path": "^1.0.1",
"jsdom": "^6.3.0",
"jsdom": "^7.0.1",
"mdast": "^2.1.0",
"mocha": "^2.2.5",
"nlcst-to-string": "^1.0.0",
"node-fetch": "^1.3.2",
"npm-run-all": "^1.2.8",
"power-assert": "^1.0.0",
Expand All @@ -58,7 +60,11 @@
"textlint-rule-no-mix-dearu-desumasu": "^1.1.0",
"textlint-rule-no-start-duplicated-conjunction": "^1.0.3",
"textlint-rule-prh": "^2.0.0",
"textlint-rule-spellcheck-tech-word": "^4.0.1"
"textlint-rule-spellcheck-tech-word": "^4.0.1",
"stemming-x-keywords": "^1.0.3",
"unist-util-is": "^1.0.0",
"unist-util-parents": "^0.1.1",
"unist-util-select": "^1.0.0"
},
"dependencies": {
"esprima": "^2.5.0",
Expand Down
95 changes: 95 additions & 0 deletions test/keywords-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// LICENSE : MIT
"use strict";
import {getKeywords} from "stemming-x-keywords";
import path from "path";
import fs from "fs";
import mdast from "mdast";
import parents from "unist-util-parents";
import select from "unist-util-select";
import isUnist from "unist-util-is";
import nlcstToString from "nlcst-to-string";

const rootDir = path.join(__dirname, "..");
const OrganizationText = fs.readFileSync(path.join(rootDir, "ORGANIZATION.md"), "utf-8");
function isNotContain(content, keywords) {
// 含んでないものだけを返す
return keywords.filter(keyword => {
return content.indexOf(keyword) === -1;
});
}
function findAllAfter(ast, node, type) {
let results = [];
let children = ast.children;
let index = 0, length = children.length;
let isFound = false;
while (++index < length) {
let child = children[index];
if (isUnist(node, child)) {
isFound = true;
} else if (isFound) {
if (isUnist(type, child)) {
results.push(child);
} else {
break;
}
}
}

return results;
}

function isAlreadyCheckKeyword(list, keyword) {
return list.indexOf(keyword) !== -1;
}

// P ASTからキーワードを抽出する
function getKeywordsOfParagraphsAsync(paragraphs) {
let _keywords = [];
let promiseList = paragraphs.map(p => {
let text = nlcstToString(p);
return getKeywords(text).then(keywords => {
_keywords = _keywords.concat(keywords);
});
});
return Promise.all(promiseList).then(()=> {
return _keywords;
});
}
function checkKeyword(text) {
let ast = mdast.parse(text);
let headerLinks = select(parents(ast), "heading link[href]");
let paragraphList = headerLinks.map(link => {
let filePath = path.resolve(rootDir, link.href);
let paragraphs = findAllAfter(ast, link.parent.node, "paragraph");
return getKeywordsOfParagraphsAsync(paragraphs).then(keywords => {
return {
filePath: filePath,
content: fs.readFileSync(filePath, "utf-8"),
keywords: keywords
};
});
});
return Promise.all(paragraphList).then(results => {
let confirmedKeywords = [];
return results.forEach(({filePath, content, keywords}) => {
let unusedKeywords = isNotContain(content, keywords);
let isChecked = isAlreadyCheckKeyword.bind(null, confirmedKeywords);
if (unusedKeywords.length === 0) {
// 使用済みのキーワードを登録
confirmedKeywords = confirmedKeywords.concat(keywords);
return;
}
if (unusedKeywords.every(isChecked)) {
console.log(unusedKeywords.join(",") + "はチェック済み");
return;
}
throw new Error(`"${unusedKeywords.join(",")}" are not used in ${filePath}`);
});
});
}
// キーワードが書くコンテンツに含まれているかをテストする
describe("keywords", function () {
it("Each chapter contain the keyword", function () {
return checkKeyword(OrganizationText);
});
});
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--timeout 100000
--recursive
--compilers js:espower-babel/guess