2021-05-19 06:18:23 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
2021-05-19 06:18:23 +02:00
|
|
|
|
|
|
|
const linkifiers = zrequire("linkifiers");
|
|
|
|
|
|
|
|
linkifiers.initialize([]);
|
2022-04-04 18:52:24 +02:00
|
|
|
|
|
|
|
function get_linkifier_regexes() {
|
|
|
|
return Array.from(linkifiers.get_linkifier_map().keys());
|
|
|
|
}
|
2021-05-19 06:18:23 +02:00
|
|
|
|
|
|
|
run_test("python_to_js_linkifier", () => {
|
|
|
|
// The only way to reach python_to_js_linkifier is indirectly, hence the call
|
|
|
|
// to update_linkifier_rules.
|
|
|
|
linkifiers.update_linkifier_rules([
|
|
|
|
{
|
|
|
|
pattern: "/a(?im)a/g",
|
|
|
|
url_format: "http://example1.example.com",
|
|
|
|
id: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: "/a(?L)a/g",
|
|
|
|
url_format: "http://example2.example.com",
|
|
|
|
id: 20,
|
|
|
|
},
|
|
|
|
]);
|
2022-04-04 18:52:24 +02:00
|
|
|
let actual_value = get_linkifier_regexes();
|
2021-05-19 06:18:23 +02:00
|
|
|
let expected_value = [/\/aa\/g(?!\w)/gim, /\/aa\/g(?!\w)/g];
|
|
|
|
assert.deepEqual(actual_value, expected_value);
|
|
|
|
// Test case with multiple replacements.
|
|
|
|
linkifiers.update_linkifier_rules([
|
|
|
|
{
|
|
|
|
pattern: "#cf(?P<contest>\\d+)(?P<problem>[A-Z][\\dA-Z]*)",
|
|
|
|
url_format: "http://example3.example.com",
|
|
|
|
id: 30,
|
|
|
|
},
|
|
|
|
]);
|
2022-04-04 18:52:24 +02:00
|
|
|
actual_value = get_linkifier_regexes();
|
2021-05-19 06:18:23 +02:00
|
|
|
expected_value = [/#cf(\d+)([A-Z][\dA-Z]*)(?!\w)/g];
|
|
|
|
assert.deepEqual(actual_value, expected_value);
|
|
|
|
// Test incorrect syntax.
|
|
|
|
blueslip.expect(
|
|
|
|
"error",
|
|
|
|
"python_to_js_linkifier: Invalid regular expression: /!@#@(!#&((!&(@#((?!\\w)/: Unterminated group",
|
|
|
|
);
|
|
|
|
linkifiers.update_linkifier_rules([
|
|
|
|
{
|
|
|
|
pattern: "!@#@(!#&((!&(@#(",
|
|
|
|
url_format: "http://example4.example.com",
|
|
|
|
id: 40,
|
|
|
|
},
|
|
|
|
]);
|
2022-04-04 18:52:24 +02:00
|
|
|
actual_value = get_linkifier_regexes();
|
2021-05-19 06:18:23 +02:00
|
|
|
expected_value = [];
|
|
|
|
assert.deepEqual(actual_value, expected_value);
|
|
|
|
});
|