Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 283 lines (234 sloc) 10.249 kB
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
1 use v6;
2 use Test;
3
4bda0c5 [t/spec] tests for RT #64464, regexes with dashes and hyphens
moritz authored
4 plan 68;
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
5
6 =begin pod
7
8 This file attempts to cover all the possible variants in regexes that use the
9 <...> syntax. They are listed in the same order as they are defined in S05.
10
11 Other files may have more comprehensive tests for a specific form (such as the
12 character classes), and those are referenced at the correct spot.
13
14 =end pod
15
b95c468 [t/spec] fix smartlinks in angle-brackets.t
moritz authored
16 # L<S05/Extensible metasyntax (C<< <...> >>)/>
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
17
18 # tests for the simpler parts of <...> syntax in regexes
19
20 # the first character is whitespace
21 {
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
22 #?rakudo 2 skip '< list > not implemented in regex'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
23 is('aaaaa' ~~ /< a aa aaaa >/, 'aaaa', 'leading whitespace quotes words (space)');
24 is('aaaaa' ~~ /< a aa aaaa >/, 'aaaa', 'leading whitespace quotes words (tab)');
25
26 eval_dies_ok('"aaaa" ~~ /<a aa>/', '<...> without whitespace calls a function (not quote words)');
27 }
28
29 # A leading alphabetic character means it's a capturing grammatical assertion
30 {
31 is('moose' ~~ /<alpha>/, 'm', 'capturing grammatical assertion (1)');
32 is('1a2b3c' ~~ /<alpha>/, 'a', 'capturing grammatical assertion (2)');
33 }
34
4bda0c5 [t/spec] tests for RT #64464, regexes with dashes and hyphens
moritz authored
35 #?rakudo skip 'RT #64464'
36 {
37 regex with-dash { '-' }
38 ok '-' ~~ /<with-dash>/, 'can call regexes which dashes (positive)';
39 ok '|' !~~ /<with-dash>/, 'can call regexes which dashes (negative)';
40
41 regex with'hyphen { a }
42 ok 'a' ~~ /<with'hyphen>/, 'can call regex with hypen (positive)';
43 ok 'b' !~~ /<with'hyphen>/, 'can call regex with hypen (negative)';
44 }
45
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
46 # so if the first character is a left parenthesis, it really is a call
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
47 #?rakudo skip '<test()> not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
48 {
49 my $pass = 0;
50 my sub test (Int $a = 1) {$pass += $a}
51 '3.14' ~~ /3 <test()>/;
52 ok($pass, 'function call (no arguments)');
53 }
54
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
55 #?rakudo skip '<test()> not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
56 {
57 my $pass = 0;
58 my sub test (Int $a) {$pass += $a}
59 '3.14' ~~ /3 <test(2)>/;
60 ok($pass, 'function call (with arguments)');
61 }
62
63 # If the first character after the identifier is an =,
64 # then the identifier is taken as an alias for what follows
65 {
66 # placeholder test for <foo=bar>
67 lives_ok({'foo' ~~ /<foo=alpha>/}, 'placeholder test for <foo=bar>');
68 }
69
70 # If the first character after the identifier is whitespace, the subsequent
71 # text (following any whitespace) is passed as a regex
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
72 #?rakudo skip 'angle quotes in regexes'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
73 {
74 my $is_regex = 0;
75 my sub test ($a) {$is_regex++ if $a ~~ Regex}
76
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
77 'whatever' ~~ /w < test hat >/;
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
78 ok($is_regex, 'text passed as a regex (1)');
79
80 $is_regex = 0;
81 'whatever' ~~ /w <test complicated . regex '<goes here>'>/;
82 ok($is_regex, 'more complicated text passed as a regex (2)');
83 }
84
85 # If the first character is a colon followed by whitespace the
86 # rest of the text is taken as a list of arguments to the method
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
87 #?rakudo skip 'colon arguments not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
88 {
89 my $called_ok = 0;
90 my sub test ($a, $b) {$called_ok++ if $a && $b}
91
92 'some text' ~~ /some <test: 3, 5>/;
93 ok($called_ok, 'method call syntax in <...>');
94 }
95
96 # No other characters are allowed after the initial identifier.
97 {
98 eval_dies_ok('"foo" ~~ /<test*>/', 'no other characters are allowed (*)');
99 eval_dies_ok('"foo" ~~ /<test|>/', 'no other characters are allowed (|)');
100 eval_dies_ok('"foo" ~~ /<test&>/', 'no other characters are allowed (&)');
101 eval_dies_ok('"foo" ~~ /<test:>/', 'no other characters are allowed (:)');
102 }
103
b95c468 [t/spec] fix smartlinks in angle-brackets.t
moritz authored
104 # L<S05/Extensible metasyntax (C<< <...> >>)/A leading . causes a named assertion not to capture what it matches>
3152b01 [t] some updates to README, and fixed all but one smartlink in t/spec/
moritz authored
105 # XXX "section ``Extensible metasyntax (<...>)'' not found in S05." - how to
106 # reference that section?
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
107 {
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
108 is('blorg' ~~ /<.alpha>/, 'b', 'leading . prevents capturing');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
109 }
110
111 # If the dot is not followed by an identifier, it is parsed as
112 # a "dotty" postfix of some type, such as an indirect method call
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
113 #?rakudo skip 'indirect subrule call not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
114 {
115 # placeholder test for <.$foo>
116 lives_ok({
117 my $method = 'WHAT';
118 'foo bar baz' ~~ /foo <.$method>/;
119 }, '<.$foo> syntax placeholder');
120 }
121
122 # A leading $ indicates an indirect subrule. The variable must contain
123 # either a Regex object, or a string to be compiled as the regex.
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
124 #?rakudo skip 'indirect subrule call not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
125 {
126 my $rule = rx/bar/;
127 my $str = 'qwe';
128 ok('bar' ~~ /<$rule>/, '<$whatever> subrule (Regex, 1)');
129 ok('qwer' ~~ /<$str>/, '<$whatever> subrule (String, 1)');
130
131 # The assertion is not captured.
132 is('abar' ~~ /a<$rule>/, 'a', '<$whatever> subrule (Regex, 2)');
133 is('qwer' ~~ /<$str>r/, 'r', '<$whatever> subrule (String, 2)');
134 }
135
136 # A leading :: indicates a symbolic indirect subrule
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
137 #?rakudo skip 'indirect subrule call not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
138 {
139 my $name = 'alpha';
b482fed .** isn't a valid regex
lwall authored
140 ok('abcdef' ~~ /<::($name)>/, '<::($name)> symbolic indirect subrule');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
141 }
142
143 # A leading @ matches like a bare array except that each element is
144 # treated as a subrule (string or Regex object) rather than as a literal
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
145 #?rakudo skip '<@array> not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
146 {
b482fed .** isn't a valid regex
lwall authored
147 my @first = <a b c .**4>;
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
148 ok('dddd' ~~ /<@first>/, 'strings are treated as a subrule in <@foo>');
149
b482fed .** isn't a valid regex
lwall authored
150 my @second = rx/\.**2/, rx/'.**2'/;
151 ok('abc.**2def' ~~ /<@second>/, 'Regexes are left alone in <@foo> subrule');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
152 }
153
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
154 # A leading % matches like a bare hash except that
155 # a string value is always treated as a subrule
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
156 #?rakudo skip '<%hash> not implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
157 {
158 my %first = {'<alpha>' => '', 'b' => '', 'c' => ''};
159 ok('aeiou' ~~ /<%first>/, 'strings are treated as a subrule in <%foo>');
160
161 my %second = {rx/\.**2/ => '', rx/'.**2'/ => ''};
162 ok('abc.**2def' ~~ /<%second>/, 'Regexes are left alone in <%foo> subrule');
163 }
164
165 # A leading { indicates code that produces a regex to be
166 # interpolated into the pattern at that point as a subrule:
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
167 #?rakudo skip '<{code}> not implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
168 {
169 ok('abcdef' ~~ /<{'<al' ~ 'pha>'}>/, 'code interpolation');
170 }
171
172 # A leading & interpolates the return value of a subroutine call as a regex.
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
173 #?rakudo skip '<&foo()> not implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
174 {
175 my sub foo {return '<alpha>'}
176 ok('abcdef' ~~ /<&foo()>/, 'subroutine call interpolation');
177 }
178
179 # If it is a string, the compiled form is cached with the string so that
180 # it is not recompiled next time you use it unless the string changes.
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
181 #?rakudo skip '<$subrule> not implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
182 {
183 my $counter = 0;
184 my $subrule = '{$counter++; \'<alpha>\'}';
185
186 'abc' ~~ /<$subrule>/;
187 is($counter, 1, 'code inside string was executed');
188
189 'def' ~~ /<$subrule>/;
190 is($counter, 1, 'string value was cached');
191 }
192
193 # A leading ?{ or !{ indicates a code assertion
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
194 #?rakudo skip '<?{...}> and <!{...}> not implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
195 {
196 ok('192' ~~ /(\d**1..3) <?{$0 < 256}>/, '<?{...}> works');
197 ok(!('992' ~~ /(\d**1..3) <?{$0 < 256}>/), '<?{...}> works');
198 ok(!('192' ~~ /(\d**1..3) <!{$0 < 256}>/), '<!{...}> works');
199 ok('992' ~~ /(\d**1..3) <!{$0 < 256}>/, '<!{...}> works');
200 }
201
202 # A leading [ indicates an enumerated character class
203 # A leading - indicates a complemented character class
204 # A leading + may also be supplied
205 # see charset.t
206
207 # The special assertion <.>
208 # see combchar.t
209
b95c468 [t/spec] fix smartlinks in angle-brackets.t
moritz authored
210 # L<S05/Extensible metasyntax (C<< <...> >>)/A leading ! indicates a negated meaning (always a zero-width assertion)>
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
211 {
37ea2a8 [t/spec]: underscore matches <alpha> -- RT #65138.
pmichaud authored
212 ok('1./:"{}=-' ~~ /^[<!alpha> .]+$/, '<!alpha> matches non-letter characters');
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
213 ok(!('abcdef' ~~ /<!alpha>./), '<!alpha> does not match letter characters');
214 #?rakudo todo '<!before>'
215 is(+('.2 1' ~~ /<!before \.> \d/), 1, '<!before>');
216 is +$/.keys, 0, '<!before \\.> does not capture';
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
217 }
218
219 # A leading ? indicates a positive zero-width assertion
220 {
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
221 is(~('123abc456def' ~~ /(.+? <?alpha>)/), '123', 'positive zero-width assertion');
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
222 }
223
224 # The <...>, <???>, and <!!!> special tokens have the same "not-defined-yet"
225 # meanings within regexes that the bare elipses have in ordinary code
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
226 #?rakudo skip '..., !!! and ??? in regexes'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
227 {
228 eval_dies_ok('"foo" ~~ /<...>/', '<...> dies in regex match');
229 # XXX: Should be warns_ok, but we don't have that yet
230 lives_ok({'foo' ~~ /<???>/}, '<???> lives in regex match');
231 eval_dies_ok('"foo" ~~ /<!!!>/', '<!!!> dies in regex match');
232 }
233
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
234 # A leading * indicates that the following pattern allows a partial match.
235 # It always succeeds after matching as many characters as possible.
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
236 #?rakudo skip '<*literal>'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
237 {
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
238 is('' ~~ /^ <*xyz> $ /, '', 'partial match (0)');
239 is('x' ~~ /^ <*xyz> $ /, 'x', 'partial match (1a)');
240 is('xz' ~~ /^ <*xyz> $ /, 'x', 'partial match (1b)');
241 is('yzx' ~~ /^ <*xyz> $ /, 'x', 'partial match (1c)');
242 is('xy' ~~ /^ <*xyz> $ /, 'xy', 'partial match (2a)');
243 is('xyx' ~~ /^ <*xyz> $ /, 'xy', 'partial match (2a)');
244 is('xyz' ~~ /^ <*xyz> $ /, 'xyz', 'partial match (3)');
245
246 is('abc' ~~ / ^ <*ab+c> $ /, 'abc', 'partial match with quantifier (1)');
247 is('abbbc' ~~ / ^ <*ab+c> $ /, 'abbbc', 'partial match with quantifier (2)');
248 is('ababc' ~~ / ^ <*'ab'+c> $ /, 'ababc', 'partial match with quantifier (3)');
249 is('aba' ~~ / ^ <*'ab'+c> $ /, 'ababc', 'partial match with quantifier (4)');
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
250 }
251
252 # A leading ~~ indicates a recursive call back into some or all of the
253 # current rule. An optional argument indicates which subpattern to re-use
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
254 #?rakudo skip '<~~ ... >'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
255 {
256 ok('1.2.' ~~ /\d+\. <~~>/, 'recursive regex using whole pattern');
257 ok('foodbard' ~~ /(foo|bar) d <~~0>/, 'recursive regex with partial pattern');
258 }
259
260 # The following tokens include angles but are not required to balance
261
262 # A <( token indicates the start of a result capture,
263 # while the corresponding )> token indicates its endpoint
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
264 #?rakudo skip '<( and )>'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
265 {
266 is('foo123bar' ~~ /foo <(\d+)> bar/, 123, '<(...)> pair');
267 is('foo456bar' ~~ /foo <(\d+ bar/, '456bar', '<( match');
268 is('foo789bar' ~~ /foo \d+)> bar/, 'foo789', ')> match');
269 ok(!('foo123') ~~ /foo <(\d+)> bar/, 'non-matching <(...)>');
270 }
271
272 # A « or << token indicates a left word boundary.
273 # A » or >> token indicates a right word boundary.
274 {
275 is('abc' ~~ /<<abc/, 'abc', 'left word boundary (string beginning)');
276 is('!abc' ~~ /<<abc/, 'abc', 'left word boundary (\W character)');
277 is('abc' ~~ /abc>>/, 'abc', 'right word boundary (string end)');
278 is('abc!' ~~ /abc>>/, 'abc', 'right word boundary (\W character)');
279 is('!abc!' ~~ /<<abc>>/, 'abc', 'both word boundaries (\W character)');
280 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
281
282 # vim: ft=perl6
Something went wrong with that request. Please try again.