Commit e5c561c2 authored by Roy Revelt's avatar Roy Revelt

test: split test file into separate files

parent c2af7a42
......@@ -33,6 +33,7 @@
"4.0.1": 1212172.3050236916,
"4.0.2": 296775.91504485044,
"4.0.3": 264187.7953102763,
"lastPublished": 264187.7953102763,
"lastRan": 264187.7953102763
"4.0.7": 702537.76943317,
"lastPublished": 702537.76943317,
"lastRan": 702537.76943317
}
\ No newline at end of file
import tap from "tap";
import {
// matchLeftIncl,
matchRightIncl,
// matchLeft,
matchRight,
} from "../dist/string-match-left-right.esm";
// 13. Ad-hoc
// -----------------------------------------------------------------------------
tap.test(
`01 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, tests set #01`,
(t) => {
t.equal(matchRight('<a class="something"> text', 19, ">"), ">", "01");
t.end();
}
);
tap.test(
`02 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, tests set #01`,
(t) => {
t.equal(
matchRight('<a class="something"> text', 19, ">", {
cb: (char) => typeof char === "string" && char.trim() === "",
}),
">",
"02"
);
t.end();
}
);
tap.test(
`03 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, tests set #01`,
(t) => {
t.equal(
matchRightIncl('<a class="something"> text', 20, "> t"),
"> t",
"03"
);
t.end();
}
);
tap.test(
`04 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, tests set #01`,
(t) => {
t.equal(matchRight('<a class="something"> text', 19, "> t"), "> t", "04");
t.end();
}
);
tap.test(
`05 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, tests set #01`,
(t) => {
t.equal(
matchRight("ab cdef", 1, "cde", { trimBeforeMatching: true }),
"cde",
"05"
);
t.end();
}
);
tap.test(
`06 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, tests set #01`,
(t) => {
t.equal(
matchRight('<a class="something"> text', 19, ">", {
cb: (char) => char === " ",
}),
">",
"06"
);
t.end();
}
);
tap.test(
`07 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, tests set #01`,
(t) => {
t.equal(
matchRight("ab cdef", 1, "cde", {
cb: (char) => char === "f",
trimBeforeMatching: true,
}),
"cde",
"07"
);
t.end();
}
);
tap.test(
`08 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, tests set #01`,
(t) => {
matchRight("ab cdef", 1, "cd", {
trimBeforeMatching: true,
cb: (char, theRemainderOfTheString, index) => {
t.equal(char, "e");
t.equal(theRemainderOfTheString, "ef");
t.equal(index, 10);
},
});
t.end();
}
);
tap.test(`09 - ${`\u001b[${35}m${"ADHOC"}\u001b[${39}m`}, set #02`, (t) => {
t.equal(
matchRight("a<!DOCTYPE html>b", 1, ["!--", "doctype", "xml", "cdata"], {
i: true,
trimCharsBeforeMatching: ["?", "!", "[", " ", "-"],
}),
"doctype",
"09"
);
t.end();
});
import tap from "tap";
import {
matchLeftIncl,
matchRightIncl,
// matchLeft,
// matchRight,
} from "../dist/string-match-left-right.esm";
// input arg validation
// -----------------------------------------------------------------------------
tap.test("01 - throws", (t) => {
// no third arg
t.throws(() => {
matchLeftIncl("zzz", 1);
}, /THROW_ID_08/);
t.throws(() => {
matchRightIncl("zzz", 1);
}, /THROW_ID_08/);
// third arg being wrong
t.throws(() => {
matchRightIncl("zzz", 1, 1);
}, /THROW_ID_05/);
t.throws(() => {
matchRightIncl("zzz", "aaa", 1);
}, /THROW_ID_03/);
t.throws(() => {
matchRightIncl("zzz", "aaa", "");
}, /THROW_ID_03/);
t.throws(() => {
matchRightIncl("zzz", "aaa", [""]);
}, /THROW_ID_03/);
t.throws(() => {
matchRightIncl("zzz", "aaa", ["", ""]);
}, /THROW_ID_03/);
// no second arg
t.throws(() => {
matchLeftIncl("zzz", null, ["aaa"]);
}, /THROW_ID_03/);
t.throws(() => {
matchRightIncl("zzz", null, ["aaa"]);
}, /THROW_ID_03/);
t.throws(() => {
matchRightIncl("zzz", null, []);
}, /THROW_ID_03/);
t.throws(() => {
matchRightIncl("zzz", null, "");
}, /THROW_ID_03/);
// second arg completely missing onwards
t.throws(() => {
matchLeftIncl("zzz");
}, /THROW_ID_03/);
t.throws(() => {
matchRightIncl("zzz");
}, /THROW_ID_03/);
// fourth arg not a plain object
t.throws(() => {
matchRightIncl("zzz", 1, ["aaa"], true);
}, /THROW_ID_06/);
// opts.trimBeforeMatching wrong type
t.throws(() => {
matchRightIncl("zzz", 1, ["aaa"], {
trimBeforeMatching: "z",
});
}, /THROW_ID_09/);
t.throws(() => {
matchRightIncl("zzz", 1, ["aaa"], {
trimBeforeMatching: [],
});
}, /trimCharsBeforeMatching/);
t.end();
});
This diff is collapsed.
This diff is collapsed.
import tap from "tap";
import {
// matchLeftIncl,
// matchRightIncl,
matchLeft,
// matchRight,
} from "../dist/string-match-left-right.esm";
// matchLeft()
// -----------------------------------------------------------------------------
tap.test(
`01 - ${`\u001b[${31}m${"matchLeft()"}\u001b[${39}m`} on a simple string`,
(t) => {
t.equal(matchLeft("abc", 2, "b"), "b", "01.01");
t.equal(
matchLeft("abcdefghi", 3, ["abc"]),
"abc",
"01.02 - one elem to match"
);
t.equal(
matchLeft("abcdefghi", 3, ["c", "bc"]),
"c", // first one matched returned, although both did match
"01.03 - multiple to match"
);
t.equal(matchLeft("abcdefghi", 3, ["aaa", "bc"]), "bc", "01.04");
t.equal(matchLeft("abcdefghi", 3, ["aaa", "zzz"]), false, "01.05");
t.equal(matchLeft("abcdefghi", 99, ["aaa", "zzz"]), false, "01.06");
t.equal(matchLeft("abc", 2, "zab"), false, "01.07");
t.end();
}
);
tap.test(
`02 - ${`\u001b[${31}m${"matchLeft()"}\u001b[${39}m`} case insensitive`,
(t) => {
t.equal(matchLeft("abc", 2, "B"), false, "02.01 - control");
t.equal(matchLeft("abc", 2, "B", { i: true }), "B", "02.02 - opts.i");
t.end();
}
);
tap.test(
`03 - ${`\u001b[${31}m${"matchLeft()"}\u001b[${39}m`} opts.maxMismatches === 1, three-char set`,
(t) => {
//
// first char, "d" is dot
t.equal(
matchLeft("_abc.efghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: false, // that's "d"
lastMustMatch: false, // that's "b"
}),
"bcd",
"03.01"
);
t.equal(
matchLeft("_abc.efghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: true, // that's "d"
lastMustMatch: false, // that's "b"
}),
false, // because "d" was not matched
"03.02"
);
t.equal(
matchLeft("_abc.efghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: false, // that's "d"
lastMustMatch: true, // that's "b"
}),
"bcd",
"03.03"
);
t.equal(
matchLeft("_abc.efghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: true, // that's "d"
lastMustMatch: true, // that's "b"
}),
false,
"03.04"
);
//
// second char, "c" is dot
t.equal(
matchLeft("_ab.defghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: false, // that's "d"
lastMustMatch: false, // that's "b"
}),
"bcd",
"03.05"
);
t.equal(
matchLeft("_ab.defghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: true, // that's "d"
lastMustMatch: false, // that's "b"
}),
"bcd",
"03.06"
);
t.equal(
matchLeft("_ab.defghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: false, // that's "d"
lastMustMatch: true, // that's "b"
}),
"bcd",
"03.07"
);
t.equal(
matchLeft("_ab.defghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: true, // that's "d"
lastMustMatch: true, // that's "b"
}),
"bcd",
"03.08"
);
//
// third char, "b" is dot
t.equal(
matchLeft("_a.cdefghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: false, // that's "d"
lastMustMatch: true, // that's "b"
}),
false,
"03.09"
);
t.equal(
matchLeft("_a.cdefghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: true, // that's "d"
lastMustMatch: true, // that's "b"
}),
false,
"03.10"
);
t.equal(
matchLeft("_a.cdefghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: false, // that's "d"
lastMustMatch: false, // that's "b"
}),
"bcd",
"03.11"
);
t.equal(
matchLeft("_a.cdefghi", 5, ["bcd"], {
maxMismatches: 1,
firstMustMatch: true, // that's "d"
lastMustMatch: false, // that's "b"
}),
"bcd",
"03.12"
);
t.end();
}
);
tap.test(
`04 - ${`\u001b[${31}m${"matchLeft()"}\u001b[${39}m`} opts.maxMismatches === 1, two-char set`,
(t) => {
// lastMustMatch=false
t.equal(
matchLeft("_abc.efghi", 5, ["zc"], {
maxMismatches: 1,
firstMustMatch: true,
lastMustMatch: false,
}),
false,
"04.01"
);
t.equal(
matchLeft("_abc.efghi", 4, ["zc"], {
maxMismatches: 1,
firstMustMatch: true,
lastMustMatch: false,
}),
"zc",
"04.02"
);
t.equal(
matchLeft("_abc.efghi", 5, ["cz"], {
maxMismatches: 1,
firstMustMatch: true,
lastMustMatch: false,
}),
false,
"04.03"
);
t.equal(
matchLeft("_abc.efghi", 5, ["cz"], {
maxMismatches: 1,
firstMustMatch: false,
lastMustMatch: false,
}),
"cz",
"04.04"
);
// lastMustMatch=true
t.equal(
matchLeft("_abc.efghi", 5, ["zc"], {
maxMismatches: 1,
firstMustMatch: true,
lastMustMatch: true,
}),
false,
"04.05"
);
t.equal(
matchLeft("_abc.efghi", 4, ["zc"], {
maxMismatches: 1,
firstMustMatch: true,
lastMustMatch: true,
}),
false,
"04.06"
);
t.equal(
matchLeft("_abc.efghi", 5, ["cz"], {
maxMismatches: 1,
firstMustMatch: true,
lastMustMatch: true,
}),
false,
"04.07"
);
t.equal(
matchLeft("_abc.efghi", 5, ["cz"], {
maxMismatches: 1,
firstMustMatch: false,
lastMustMatch: true,
}),
"cz",
"04.08"
);
t.end();
}
);
tap.test(
`05 - ${`\u001b[${31}m${"matchLeft()"}\u001b[${39}m`} opts.maxMismatches === 1, pin the maxMismatches cut-off point, protruding value`,
(t) => {
//
// firstMustMatch = true
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 1,
firstMustMatch: true,
}),
false,
"05.01"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 2,
firstMustMatch: true,
}),
false,
"05.02"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 3,
firstMustMatch: true,
}),
false,
"05.03"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 4,
firstMustMatch: true,
}),
false,
"05.04"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 5,
firstMustMatch: true,
}),
".....d",
"05.05"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 99,
firstMustMatch: true,
}),
".....d",
"05.06"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 0,
firstMustMatch: true,
}),
false,
"05.07"
);
//
// firstMustMatch = false
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 1,
firstMustMatch: false,
}),
false,
"05.08"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 2,
firstMustMatch: false,
}),
false,
"05.09"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 3,
firstMustMatch: false,
}),
false,
"05.10"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 4,
firstMustMatch: false,
}),
false,
"05.11"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 5,
firstMustMatch: false,
}),
".....d",
"05.12"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 99,
firstMustMatch: false,
}),
".....d",
"05.13"
);
t.equal(
matchLeft("_abcdef", 5, [".....d"], {
maxMismatches: 0,
firstMustMatch: false,
}),
false,
"05.14"
);
t.end();
}
);
tap.test(
`06 - ${`\u001b[${31}m${"matchLeft()"}\u001b[${39}m`} opts.maxMismatches === 1, pin the maxMismatches cut-off point, non-protruding value`,
(t) => {
t.equal(
matchLeft("_abcdef", 5, ["....d"], {
maxMismatches: 1,
firstMustMatch: true,
}),
false,
"06.01"
);
t.equal(
matchLeft("_abcdef", 5, ["....d"], {
maxMismatches: 2,
firstMustMatch: true,
}),
false,
"06.02"
);
t.equal(
matchLeft("_abcdef", 5, ["....d"], {
maxMismatches: 3,
firstMustMatch: true,
}),
false,
"06.03"
);
t.equal(
matchLeft("_abcdef", 5, ["....d"], {
maxMismatches: 4,
firstMustMatch: true,
}),
"....d",
"06.04"
);
t.equal(
matchLeft("_abcdef", 5, ["....d"], {
maxMismatches: 5,
firstMustMatch: true,
}),
"....d",
"06.05"
);
t.equal(
matchLeft("_abcdef", 5, ["....d"], {
maxMismatches: 99,
firstMustMatch: true,
}),
"....d",
"06.06"
);
t.equal(
matchLeft("_abcdef", 5, ["....d"], {
maxMismatches: 0,
firstMustMatch: true,
}),
false,
"06.07"
);
t.end();
}
);
import tap from "tap";
import {
matchLeftIncl,
// matchRightIncl,
matchLeft,
// matchRight,
} from "../dist/string-match-left-right.esm";
// matchLeftIncl()
// -----------------------------------------------------------------------------
tap.test(
`01 - ${`\u001b[${33}m${"matchLeftIncl()"}\u001b[${39}m`} on a simple string`,
(t) => {
t.equal(matchLeftIncl("abc", 2, "c"), "c", "01.01 - pointless, but still");
t.equal(
matchLeftIncl("zabcdefghi", 4, ["bcd"]),
"bcd",