webpack/test/compileBooleanMatcher.unitt...

94 lines
2.4 KiB
JavaScript
Raw Normal View History

"use strict";
const { itemsToRegexp } = require("../lib/util/compileBooleanMatcher");
describe("itemsToRegexp", () => {
const expectCompiled = (name, input, fn) => {
it(`should compile ${name}`, () => {
const items = typeof input === "string" ? input.split(",") : input;
const regexp = itemsToRegexp(items);
const r = new RegExp(`^${regexp}$`);
for (const item of items) {
expect(item).toMatch(r);
}
fn(expect(regexp));
});
};
expect.addSnapshotSerializer({
test() {
return true;
},
print(received) {
return received;
}
});
expectCompiled("basic", ["abc", "def", "123", "45", "6"], e =>
2024-07-31 12:23:44 +08:00
e.toMatchInlineSnapshot("(123|45|6|abc|def)")
);
expectCompiled("single chars", ["a", "b", "c", "1", "2", "3"], e =>
2024-07-31 12:23:44 +08:00
e.toMatchInlineSnapshot("[123abc]")
);
expectCompiled(
"prefixes",
["ab1", "ab2", "ab3", "ab4", "de5", "de6", "de7", "ef8", "ef9", "gh0"],
2024-07-31 12:23:44 +08:00
e => e.toMatchInlineSnapshot("(ab[1234]|de[567]|ef[89]|gh0)")
);
expectCompiled("short prefixes", "a,ab", e =>
2024-07-31 12:23:44 +08:00
e.toMatchInlineSnapshot("a(|b)")
);
expectCompiled(
"nested prefixes",
["a", "ab", "abc", "abcd", "abcde", "abcdef"],
2024-07-31 12:23:44 +08:00
e => e.toMatchInlineSnapshot("a(b(c(d(|e|ef)|)|)|)")
);
expectCompiled("suffixes", "a1,b1,c1,d1,e1,a2,b2,c2", e =>
2024-07-31 12:23:44 +08:00
e.toMatchInlineSnapshot("([abcde]1|[abc]2)")
);
expectCompiled(
"common prod",
"674,542,965,12,942,483,445,943,423,995,434,122,995,248,432,165,436,86,435,221",
e =>
e.toMatchInlineSnapshot(
2024-07-31 12:23:44 +08:00
"(1(2|22|65)|4(3[2456]|23|45|83)|9(42|43|65|95)|221|248|542|674|86)"
)
);
expectCompiled(
"long strings",
[
"./path/to/file.js",
"./path/to/file.mjs",
"./path/to/other-file.js",
"./path/to/directory/with/file.js",
"./path/to/directory/with/file.json",
"./path/to/directory/with/file.css",
"./path/to/directory/with/module.css",
"webpack/runtime/module"
],
e =>
e.toMatchInlineSnapshot(
2024-07-31 12:23:44 +08:00
"(\\.\\/path\\/to\\/(directory\\/with\\/(file\\.(js(|on)|css)|module\\.css)|file\\.(|m)js|other\\-file\\.js)|webpack\\/runtime\\/module)"
)
);
expectCompiled(
"prefix and suffix overlap",
[
"webpack_sharing_consume_default_react_react",
"webpack_sharing_consume_default_classnames_classnames-webpack_sharing_consume_default_react_react"
],
e =>
e.toMatchInlineSnapshot(
2024-07-31 12:23:44 +08:00
"webpack_sharing_consume_default_(|classnames_classnames\\-webpack_sharing_consume_default_)react_react"
)
);
});