Skip to content
This repository has been archived by the owner on Mar 23, 2018. It is now read-only.

Commit

Permalink
feat(proofdict-tester): support whitelist and blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 20, 2017
1 parent 4d588dd commit 2c2c78d
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 21 deletions.
27 changes: 27 additions & 0 deletions src/TagFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// MIT © 2017 azu
import { ProofdictItem, Proofdict } from "./proofdict-tester";

const NOUN_TAG = "noun";

/**
* Does the `dict` has "noun" tag?
* @param {ProofdictItem} dict
* @returns {boolean}
*/
export function isNoun(dict: ProofdictItem): boolean {
return dict.tags.indexOf(NOUN_TAG) !== -1;
}

export function filterByTags(dictionary: Proofdict, whitelistTags: string[] = [], blacklistTags: string[] = []) {
if (whitelistTags.length > 0) {
return dictionary.filter(item => {
return whitelistTags.every(whitelistTags => item.tags.indexOf(whitelistTags) !== -1);
});
}
if (blacklistTags.length > 0) {
return dictionary.filter(item => {
return !item.tags.some(tag => blacklistTags.indexOf(tag) !== -1);
});
}
return dictionary;
}
28 changes: 19 additions & 9 deletions src/proofdict-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { Diff, Engine } from "prh";
import {
wrapHyphenWordBoundary, wrapWordBoundaryToString
} from "./proofdict-tester-util";
import { filterByTags, isNoun } from "./TagFilter";

export interface Proofdict {
export type Proofdict = ProofdictItem[]

export interface ProofdictItem {
expected: string;
patterns: string[];
description: string;
Expand Down Expand Up @@ -40,20 +43,28 @@ export interface ProofdictTesterResult {
diffs?: Diff[];
}

const NOUN_TAG = "noun";
export interface ProofdictTesterOptions {
dictionary: Proofdict;
// Filter dictionary by whitelist or blacklist
// Default: Enable all terms of the dictionary.
// When set both options, this rule prefer whitelist to blacklist
whitelistTags?: string[];
blacklistTags?: string[];
}

export class ProofdictTester {
private prhEngine: Engine;
private proofdict: Proofdict;

constructor(private proofdictData: Proofdict[]) {
this.proofdictData = proofdictData;
constructor(options: ProofdictTesterOptions) {
this.proofdict = options.dictionary;
const filteredProofdict = filterByTags(this.proofdict, options.whitelistTags, options.blacklistTags);
this.prhEngine = new Engine({
version: 1,
rules: proofdictData.map(dict => {
const isNoun = dict.tags.indexOf(NOUN_TAG) !== -1;
rules: filteredProofdict.map(dict => {
return {
expected: dict.expected,
patterns: isNoun ? dict.patterns.map(pattern => {
patterns: isNoun(dict) ? dict.patterns.map(pattern => {
return wrapWordBoundaryToString(pattern);
}) : dict.patterns,
tags: dict.tags,
Expand Down Expand Up @@ -82,8 +93,7 @@ export class ProofdictTester {
}
// Extension: "noun"
// Automatically add word boundary to the patterns
const tags: string[] = diff.rule!.raw.tags;
if (tags.indexOf(NOUN_TAG) !== -1) {
if (isNoun(diff.rule!.raw)) {
const expectPatterns = wrapHyphenWordBoundary(diff.pattern);
const isExpected = expectPatterns.some(expectPattern => {
return expectPattern.test(currentString);
Expand Down
65 changes: 65 additions & 0 deletions test/TagFilter-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// MIT © 2017 azu
import { Proofdict, ProofdictItem } from "../src/proofdict-tester";
import { filterByTags } from "../src/TagFilter";
import * as assert from "assert";

const proofdict: Proofdict = require("./fixtures/proofdict.json");
const shouldHaveTag = (tag: string) => {
return (item: ProofdictItem) => {
assert.ok(item.tags.indexOf(tag) !== -1, `item should have tag(${tag}). item: ${JSON.stringify(item)}`);
};
};
const shouldNotHaveTag = (tag: string) => {
return (item: ProofdictItem) => {
assert.ok(item.tags.indexOf(tag) === -1, `item should not have tag(${tag}). item: ${JSON.stringify(item)}`);
};
};
describe("TagFilter", () => {
describe("#filterByTags", () => {
it("whitelist options filter items", () => {
const whitelistTags = ["noun"];
const items = filterByTags(proofdict, whitelistTags);
items.forEach(item => {
shouldHaveTag("noun")(item);
});
});
it("whitelist options filter items by multiple tags", () => {
const whitelistTags = ["noun", "JavaScript"];
const items = filterByTags(proofdict, whitelistTags);
items.forEach(item => {
shouldHaveTag("noun")(item);
shouldHaveTag("JavaScript")(item);
});
});
it("blacklist options reject items", () => {
const blacklistTags = ["noun"];
const items = filterByTags(proofdict, [], blacklistTags);
items.forEach(item => {
shouldNotHaveTag("noun")(item);
});
});
it("blacklist options reject items by multiple tags", () => {
const blacklistTags = ["noun", "JavaScript"];
const items = filterByTags(proofdict, [], blacklistTags);
items.forEach(item => {
shouldNotHaveTag("noun")(item);
shouldNotHaveTag("JavaScript")(item);
});
});
it("whitelist options filter items", () => {
const whitelistTags = ["noun"];
const items = filterByTags(proofdict, whitelistTags);
items.forEach(item => {
shouldHaveTag("noun")(item);
});
});
it("whitelist/blacklist options, prefer to use whitelist", () => {
const whitelist = ["noun"];
const blacklistTags = ["noun"];
const items = filterByTags(proofdict, whitelist, blacklistTags);
items.forEach(item => {
shouldHaveTag("noun")(item);
});
});
})
});
6 changes: 3 additions & 3 deletions test/proofdict-tester-fixtures-test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// MIT © 2017 azu
import { Proofdict, ProofdictTester } from "../src/proofdict-tester";
import { ProofdictItem, ProofdictTester } from "../src/proofdict-tester";
import * as assert from "assert";

describe("ProofdictTester fixtures", () => {
const proofdict: Proofdict[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester(proofdict);
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester({ dictionary: proofdict });
proofdict
.filter(dict => dict.specs.length > 0)
.forEach(dict => {
Expand Down
18 changes: 9 additions & 9 deletions test/proofdict-tester-test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// MIT © 2017 azu
import { Proofdict, ProofdictTester } from "../src/proofdict-tester";
import { ProofdictItem, ProofdictTester } from "../src/proofdict-tester";
import * as assert from "assert";

describe("ProofdictTester", () => {
describe("#replace", () => {
it("should return replace result", () => {
const proofdict: Proofdict[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester(proofdict);
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester({ dictionary: proofdict });
const text = "This is webkit desu.";
return tester.replace(text).then(result => {
assert.strictEqual(result, "This is WebKit desu.")
Expand All @@ -15,8 +15,8 @@ describe("ProofdictTester", () => {
});
describe("#match", () => {
it("last noun pattern", () => {
const proofdict: Proofdict[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester(proofdict);
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester({ dictionary: proofdict });
const text = "This is webkit";
return tester.match(text).then(result => {
assert.strictEqual(result.details.length, 1);
Expand All @@ -30,8 +30,8 @@ describe("ProofdictTester", () => {
});
});
it("first noun pattern", () => {
const proofdict: Proofdict[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester(proofdict);
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester({ dictionary: proofdict });
const text = "SourceMap is text.";
return tester.match(text).then(result => {
assert.strictEqual(result.details.length, 1);
Expand All @@ -44,8 +44,8 @@ describe("ProofdictTester", () => {
});
});
it("non-noun pattern", () => {
const proofdict: Proofdict[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester(proofdict);
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester({ dictionary: proofdict });
const text = "Workaound is typo.";
return tester.match(text).then(result => {
assert.strictEqual(result.details.length, 1);
Expand Down

0 comments on commit 2c2c78d

Please sign in to comment.