fix unit test

This commit is contained in:
Eduard Heimbuch
2020-06-25 13:24:48 +02:00
parent 149b6cbc2a
commit c18719e573
2 changed files with 18 additions and 9 deletions

View File

@@ -26,16 +26,20 @@ import { regExpPattern } from "./remarkCommitLinksParser";
describe("Remark Commit Links RegEx Tests", () => {
it("should match simple names", () => {
expect("namespace/name@1a5s4w8a".match(regExpPattern)).toBeTruthy();
const regExp = new RegExp(regExpPattern, "g");
expect("namespace/name@1a5s4w8a".match(regExp)).toBeTruthy();
});
it("should match complex names", () => {
expect("hitchhiker/heart-of-gold@c7237cb60689046990dc9dc2a388a517adb3e2b2".match(regExpPattern)).toBeTruthy();
const regExp = new RegExp(regExpPattern, "g");
expect("hitchhiker/heart-of-gold@c7237cb60689046990dc9dc2a388a517adb3e2b2".match(regExp)).toBeTruthy();
});
it("should replace match", () => {
expect("Prefix namespace/name@42 suffix".replace(regExpPattern, "replaced")).toBe("Prefix replaced suffix");
const regExp = new RegExp(regExpPattern, "g");
expect("Prefix namespace/name@42 suffix".replace(regExp, "replaced")).toBe("Prefix replaced suffix");
});
it("should match groups", () => {
const match = regExpPattern.exec("namespace/name@42");
const regExp = new RegExp(regExpPattern, "g");
const match = regExp.exec("namespace/name@42");
expect(match).toBeTruthy();
if (match) {
expect(match[1]).toBe("namespace");
@@ -44,15 +48,18 @@ describe("Remark Commit Links RegEx Tests", () => {
}
});
it("should match multiple links in text", () => {
const regExp = new RegExp(regExpPattern, "g");
const text = "Prefix hitchhiker/heart-of-gold@42 some text hitchhiker/heart-of-gold@21 suffix";
const matches = [];
let match = regExpPattern.exec(text);
let match = regExp.exec(text);
while (match !== null) {
matches.push(match[0]);
match = regExpPattern.exec(text);
match = regExp.exec(text);
}
console.log(matches)
expect(matches[0]).toBe("hitchhiker/heart-of-gold@42");
expect(matches[1]).toBe("hitchhiker/heart-of-gold@21");
});

View File

@@ -31,19 +31,21 @@ import { TFunction } from "i18next";
const namePartRegex = nameRegex.source.substring(1, nameRegex.source.length - 1);
// Visible for testing
export const regExpPattern = new RegExp(`(${namePartRegex})\\/(${namePartRegex})@([\\w\\d]+)`, "g");
export const regExpPattern = `(${namePartRegex})\\/(${namePartRegex})@([\\w\\d]+)`;
function match(value: string): RegExpMatchArray[] {
const regExp = new RegExp(regExpPattern, "g");
const matches = [];
let m = regExpPattern.exec(value);
let m = regExp.exec(value);
while (m) {
matches.push(m);
m = regExpPattern.exec(value);
m = regExp.exec(value);
}
return matches;
}
export const createTransformer = (t: TFunction): MdastPlugin => {
return (tree: MarkdownAbstractSyntaxTree) => {
visit(tree, "text", (node: MarkdownAbstractSyntaxTree, index: number, parent: MarkdownAbstractSyntaxTree) => {
if (parent.type === "link" || !node.value) {