mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 15:05:44 +01:00
fix unit test
This commit is contained in:
@@ -26,16 +26,20 @@ import { regExpPattern } from "./remarkCommitLinksParser";
|
|||||||
|
|
||||||
describe("Remark Commit Links RegEx Tests", () => {
|
describe("Remark Commit Links RegEx Tests", () => {
|
||||||
it("should match simple names", () => {
|
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", () => {
|
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", () => {
|
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", () => {
|
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();
|
expect(match).toBeTruthy();
|
||||||
if (match) {
|
if (match) {
|
||||||
expect(match[1]).toBe("namespace");
|
expect(match[1]).toBe("namespace");
|
||||||
@@ -44,15 +48,18 @@ describe("Remark Commit Links RegEx Tests", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
it("should match multiple links in text", () => {
|
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 text = "Prefix hitchhiker/heart-of-gold@42 some text hitchhiker/heart-of-gold@21 suffix";
|
||||||
const matches = [];
|
const matches = [];
|
||||||
|
|
||||||
let match = regExpPattern.exec(text);
|
let match = regExp.exec(text);
|
||||||
while (match !== null) {
|
while (match !== null) {
|
||||||
matches.push(match[0]);
|
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[0]).toBe("hitchhiker/heart-of-gold@42");
|
||||||
expect(matches[1]).toBe("hitchhiker/heart-of-gold@21");
|
expect(matches[1]).toBe("hitchhiker/heart-of-gold@21");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -31,19 +31,21 @@ import { TFunction } from "i18next";
|
|||||||
const namePartRegex = nameRegex.source.substring(1, nameRegex.source.length - 1);
|
const namePartRegex = nameRegex.source.substring(1, nameRegex.source.length - 1);
|
||||||
|
|
||||||
// Visible for testing
|
// 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[] {
|
function match(value: string): RegExpMatchArray[] {
|
||||||
|
const regExp = new RegExp(regExpPattern, "g");
|
||||||
const matches = [];
|
const matches = [];
|
||||||
let m = regExpPattern.exec(value);
|
let m = regExp.exec(value);
|
||||||
while (m) {
|
while (m) {
|
||||||
matches.push(m);
|
matches.push(m);
|
||||||
m = regExpPattern.exec(value);
|
m = regExp.exec(value);
|
||||||
}
|
}
|
||||||
return matches;
|
return matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createTransformer = (t: TFunction): MdastPlugin => {
|
export const createTransformer = (t: TFunction): MdastPlugin => {
|
||||||
|
|
||||||
return (tree: MarkdownAbstractSyntaxTree) => {
|
return (tree: MarkdownAbstractSyntaxTree) => {
|
||||||
visit(tree, "text", (node: MarkdownAbstractSyntaxTree, index: number, parent: MarkdownAbstractSyntaxTree) => {
|
visit(tree, "text", (node: MarkdownAbstractSyntaxTree, index: number, parent: MarkdownAbstractSyntaxTree) => {
|
||||||
if (parent.type === "link" || !node.value) {
|
if (parent.type === "link" || !node.value) {
|
||||||
|
|||||||
Reference in New Issue
Block a user