deno.land / std@0.224.0 / regexp / escape_test.ts

escape_test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { escape } from "./escape.ts";import { assertEquals, assertMatch, assertNotMatch } from "../assert/mod.ts";
const ALL_ASCII = "\x00\x01\x02\x03\x04\x05\x06\x07\b\t\n\v\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F";const ALL_REGEXP_FLAGS = "gimsuy";
Deno.test("escape() works with examples", async (t) => { await t.step("`.` matches literal `.`", () => { const re = new RegExp(`^${escape(".")}$`, "u");
assertEquals("^\\.$", re.source); assertMatch(".", re); assertNotMatch("a", re); }); await t.step("`$` matches literal `$`", () => { const re = new RegExp(`^${escape("$")}$`);
assertMatch("$", re); assertNotMatch("", re); }); await t.step("`*` matches literal `*`", () => { const re = new RegExp(`^${escape("a*")}$`);
assertMatch("a*", re); assertNotMatch("", re); assertNotMatch("aaa", re); }); await t.step("escapes works correctly within character class", () => { const re = new RegExp(`^[${escape(".$*+[](){}|\\<>")}]$`);
assertMatch(".", re); assertMatch("$", re); assertMatch("*", re); assertMatch("+", re); assertMatch("[", re); assertMatch("]", re); assertMatch("(", re); assertMatch(")", re); assertMatch("{", re); assertMatch("}", re); assertMatch("|", re); assertMatch("\\", re); assertMatch("<", re); assertMatch(">", re);
assertNotMatch("a", re); });});Deno.test("escape() works with all ASCII", async (t) => { await t.step("interpolates without erroring", async (t) => { await t.step("outside character class", () => { for (const char of ALL_ASCII) { for (const flag of ALL_REGEXP_FLAGS) { new RegExp(escape(char), flag); } } }); await t.step("within character class", () => { for (const char of ALL_ASCII) { for (const flag of ALL_REGEXP_FLAGS) { new RegExp(`[${escape(char)}]`, flag); } } }); await t.step("matches self", () => { for (const char of ALL_ASCII) { for (const flag of ALL_REGEXP_FLAGS) { assertMatch(char, new RegExp(`^${escape(char)}$`, flag)); } } }); await t.step("doesn't match any other chars", () => { for (const char of ALL_ASCII) { for (const flag of ALL_REGEXP_FLAGS) { if (flag === "i") continue;
for (const char2 of ALL_ASCII) { if (char2 === char) continue;
assertNotMatch( char2, new RegExp(`^${escape(char)}$`, flag), ); } } } }); });});
std

Version Info

Tagged at
3 weeks ago