summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax/21_regex_literals
blob: 44f00793e701d061df7bc11d65e8bad1d6f4ed25 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Regex literals are enclosed in forward slashes and may contain zero
or more trailing flag characters. Interpretation of escape sequences
within regular expression literals is subject of the underlying
regular expression engine.

-- Expect stdout --
[ "/Hello world/", "/test/gis", "/test/g", "/test1 / test2/", "/1\n\\.\u0007\\bc☀\\\\/" ]
-- End --

-- Testcase --
{%
	print([
		/Hello world/,				// A very simple expression
		/test/gsi,					// Regular expression flags
		/test/gg,					// Repeated flags
		/test1 \/ test2/,			// Escaped forward slash
		/\x31\n\.\a\b\c\u2600\\/	// Ensure that escape sequences are passed as-is
	], "\n");
%}
-- End --


Testing regular expression type.

-- Expect stdout --
regexp
-- End --

-- Testcase --
{{ type(/foo/) }}
-- End --


Testing invalid flag characters.

-- Expect stderr --
Syntax error: Unexpected token
Expecting ';'
In line 2, byte 8:

 `    /test/x`
            ^-- Near here


-- End --

-- Testcase --
{%
	/test/x
%}
-- End --


Testing unclosed regular expression.

-- Expect stderr --
Syntax error: Unterminated string
In line 2, byte 2:

 `    /foo \/`
      ^-- Near here


-- End --

-- Testcase --
{%
	/foo \/
%}
-- End --


Testing regex compilation errors.

-- Expect stderr --
Catched syntax error
In line 7, byte 30:

 `            die("Catched syntax error");`
  Near here ----------------------------^


-- End --

-- Testcase --
{%
	try {
		/foo (/
	}
	catch (e) {
		if (e.type == "Syntax error")
			die("Catched syntax error");
	}
%}
-- End --


Testing that slashes within character classes are not treated as regex
literal delimitters.

-- Expect stdout --
[
	"/[/]/",
	"/[[./.]/]/",
	"/[[:alpha:]/]/",
	"/[[=/=]/]/"
]
-- End --

-- Testcase --
{%
	printf("%.J\n", [
		/[/]/,
		/[[./.]/]/,
		/[[:alpha:]/]/,
		/[[=/=]/]/
	]);
%}
-- End --


Testing that regex extension macros are substituted only outside of
bracket set expressions.

-- Expect stdout --
[
	"/ \\b \\B [\b B] /",
	"/ \\< \\> [< >] /",
	"/ [[:digit:]] [^[:digit:]] [d D] /",
	"/ [[:space:]] [^[:space:]] [s S] /",
	"/ [[:alnum:]_] [^[:alnum:]_] [w W] /"
]
-- End --

-- Testcase --
{%
	printf("%.J\n", [
		/ \b \B [\b \B] /,   // \b outside brackets is a word boundary,
		                     // \b within brackets is backspace
		/ \< \> [\< \>] /,
		/ \d \D [\d \D] /,
		/ \s \S [\s \S] /,
		/ \w \W [\w \W] /
	]);
%}
-- End --