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

Commit

Permalink
feat(test): support rule in results
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove `url` from results

fix #1
  • Loading branch information
azu committed Feb 25, 2018
1 parent e3b2c7d commit 0e8683c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/TagFilter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// MIT © 2017 azu
import { ProofdictItem, Proofdict } from "./proofdict-tester";
import { ProofdictRule, Proofdict } from "./proofdict-tester";

const NOUN_TAG = "noun";

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

Expand All @@ -24,4 +24,4 @@ export function filterByTags(dictionary: Proofdict, whitelistTags: string[] = []
});
}
return dictionary;
}
}
22 changes: 12 additions & 10 deletions src/proofdict-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import {
} from "./proofdict-tester-util";
import { filterByTags, isNoun } from "./TagFilter";

export type Proofdict = ProofdictItem[]
export type Proofdict = ProofdictRule[]

export interface ProofdictItem {
export interface ProofdictRule {
id?: string;
description?: string;
expected: string;
patterns: string[];
description: string;
id: string;
specs: ProofdictSpec[];
tags: string[];
specs?: ProofdictSpec[];

[index: string]: any;
}

export interface ProofdictSpec {
Expand All @@ -22,8 +24,8 @@ export interface ProofdictSpec {
}

export interface ProofdictTesterResultDetail {
// details url
url: string;
// It is matched rule object
rule: ProofdictRule;
// additional description
description?: string;
// original match start index, not replaced start index
Expand Down Expand Up @@ -118,9 +120,9 @@ export class ProofdictTester {
const actual = currentString.slice(matchStartIndex, matchEndIndex);
const expected = diff.newText!;
const description = diff.rule && diff.rule.raw.description;
const url = diff.rule ? `https://proofdict.github.io/item/${diff.rule!.raw.id}` : "https://proofdict.github.io/";
const rule = diff.rule && diff.rule.raw;
results.push({
url,
rule,
matchStartIndex,
matchEndIndex,
actual,
Expand All @@ -136,4 +138,4 @@ export class ProofdictTester {
diffs: sortedDiffs
});
}
}
}
8 changes: 4 additions & 4 deletions test/TagFilter-test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// MIT © 2017 azu
import { Proofdict, ProofdictItem } from "../src/proofdict-tester";
import { Proofdict, ProofdictRule } 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) => {
return (item: ProofdictRule) => {
assert.ok(item.tags.indexOf(tag) !== -1, `item should have tag(${tag}). item: ${JSON.stringify(item)}`);
};
};
const shouldNotHaveTag = (tag: string) => {
return (item: ProofdictItem) => {
return (item: ProofdictRule) => {
assert.ok(item.tags.indexOf(tag) === -1, `item should not have tag(${tag}). item: ${JSON.stringify(item)}`);
};
};
Expand Down Expand Up @@ -62,4 +62,4 @@ describe("TagFilter", () => {
});
});
})
});
});
6 changes: 3 additions & 3 deletions test/proofdict-tester-fixtures-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// MIT © 2017 azu
import { ProofdictItem, ProofdictTester } from "../src/proofdict-tester";
import { ProofdictRule, ProofdictTester } from "../src/proofdict-tester";
import * as assert from "assert";

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

describe("ProofdictTester", () => {
describe("#replace", () => {
it("should return replace result", () => {
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const proofdict: ProofdictRule[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester({ dictionary: proofdict });
const text = "This is webkit desu.";
return tester.replace(text).then(result => {
Expand All @@ -15,13 +15,23 @@ describe("ProofdictTester", () => {
});
describe("#match", () => {
it("last noun pattern", () => {
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const proofdict: ProofdictRule[] = 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);
const [detail] = result.details;
assert.strictEqual(detail.url, "https://proofdict.github.io/item/01BQ92YZ6QR8RJKA5Y8W2F9NMY");
assert.deepStrictEqual(detail.rule, {
"id": "01BQ92YZ6QR8RJKA5Y8W2F9NMY",
"description": "Reference https://webkit.org/",
"expected": "WebKit",
"patterns": [
"/\\bwebkit\\b/i"
],
"tags": [
"noun"
]
});
assert.strictEqual(detail.actual, "webkit");
assert.strictEqual(detail.expected, "WebKit");
assert.strictEqual(detail.description, "Reference https://webkit.org/");
Expand All @@ -31,7 +41,7 @@ describe("ProofdictTester", () => {
});
});
it("first noun pattern", () => {
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const proofdict: ProofdictRule[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester({ dictionary: proofdict });
const text = "SourceMap is text.";
return tester.match(text).then(result => {
Expand All @@ -45,7 +55,7 @@ describe("ProofdictTester", () => {
});
});
it("non-noun pattern", () => {
const proofdict: ProofdictItem[] = require("./fixtures/proofdict.json");
const proofdict: ProofdictRule[] = require("./fixtures/proofdict.json");
const tester = new ProofdictTester({ dictionary: proofdict });
const text = "Workaound is typo.";
return tester.match(text).then(result => {
Expand All @@ -59,4 +69,4 @@ describe("ProofdictTester", () => {
});
});
});
});
});

0 comments on commit 0e8683c

Please sign in to comment.