Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 305 lines (253 sloc) 11.159 kB
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
1 use v6;
2 use Test;
3
d2482f4 replace plan *; with number of tests S03-S06
mberends authored
4 plan 76;
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 {
b09f2a9 @moritz fix and unfudge RT #64464 tests
moritz authored
36 my regex with-dash { '-' }
4bda0c5 [t/spec] tests for RT #64464, regexes with dashes and hyphens
moritz authored
37 ok '-' ~~ /<with-dash>/, 'can call regexes which dashes (positive)';
38 ok '|' !~~ /<with-dash>/, 'can call regexes which dashes (negative)';
39
b09f2a9 @moritz fix and unfudge RT #64464 tests
moritz authored
40 my regex with'hyphen { a }
4bda0c5 [t/spec] tests for RT #64464, regexes with dashes and hyphens
moritz authored
41 ok 'a' ~~ /<with'hyphen>/, 'can call regex with hypen (positive)';
42 ok 'b' !~~ /<with'hyphen>/, 'can call regex with hypen (negative)';
43 }
44
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
45 # 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
46 #?rakudo skip '<test()> not implemented'
9b324ea @coke fudge for niecza
coke authored
47 #?niecza skip 'Unable to resolve method test in class Cursor'
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'
9b324ea @coke fudge for niecza
coke authored
56 #?niecza skip 'Unable to resolve method test in class Cursor'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
57 {
58 my $pass = 0;
59 my sub test (Int $a) {$pass += $a}
60 '3.14' ~~ /3 <test(2)>/;
61 ok($pass, 'function call (with arguments)');
62 }
63
64 # If the first character after the identifier is an =,
65 # then the identifier is taken as an alias for what follows
66 {
3887bcf [t/spec] as per r29067 <foo=bar> aliasing preserves the $<bar> captur…
moritz authored
67 ok 'foo' ~~ /<foo=alpha>/, 'basic <foo=bar> aliasing';
68 is $<foo>, 'f', 'alias works';
69 is $<alpha>, 'f', 'alias does not throw away original name';
70 }
71
1fee361 [t/spec] test for <foo=.bar> aliasing in regex
moritz authored
72 {
73 ok 'foo' ~~ /<foo=.alpha>/, 'basic <foo=.bar> aliasing';
74 is $<foo>, 'f', 'alias works';
75 ok !defined($<alpha>), 'alias does throw away original name';
76 }
77
3887bcf [t/spec] as per r29067 <foo=bar> aliasing preserves the $<bar> captur…
moritz authored
78 {
79 ok '123gb' ~~ / <foo=.alpha> /, '<foo=.bar>';
80 is $<foo>, 'g', '=. renaming worked';
2544bcf @jdhore Remove most remaining .notdef's.
jdhore authored
81 nok $<alpha>.defined, '=. removed the old capture name';
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
82 }
83
84 # If the first character after the identifier is whitespace, the subsequent
85 # text (following any whitespace) is passed as a regex
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
86 #?rakudo skip 'angle quotes in regexes'
9b324ea @coke fudge for niecza
coke authored
87 #?niecza skip 'Unable to resolve method test in class Cursor'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
88 {
89 my $is_regex = 0;
90 my sub test ($a) {$is_regex++ if $a ~~ Regex}
91
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
92 'whatever' ~~ /w < test hat >/;
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
93 ok($is_regex, 'text passed as a regex (1)');
94
95 $is_regex = 0;
96 'whatever' ~~ /w <test complicated . regex '<goes here>'>/;
97 ok($is_regex, 'more complicated text passed as a regex (2)');
98 }
99
100 # If the first character is a colon followed by whitespace the
101 # 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
102 #?rakudo skip 'colon arguments not implemented'
9b324ea @coke fudge for niecza
coke authored
103 #?niecza skip 'Unable to resolve method test in class Cursor'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
104 {
105 my $called_ok = 0;
106 my sub test ($a, $b) {$called_ok++ if $a && $b}
107
108 'some text' ~~ /some <test: 3, 5>/;
109 ok($called_ok, 'method call syntax in <...>');
110 }
111
112 # No other characters are allowed after the initial identifier.
113 {
114 eval_dies_ok('"foo" ~~ /<test*>/', 'no other characters are allowed (*)');
115 eval_dies_ok('"foo" ~~ /<test|>/', 'no other characters are allowed (|)');
116 eval_dies_ok('"foo" ~~ /<test&>/', 'no other characters are allowed (&)');
117 eval_dies_ok('"foo" ~~ /<test:>/', 'no other characters are allowed (:)');
118 }
119
b95c468 [t/spec] fix smartlinks in angle-brackets.t
moritz authored
120 # 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
121 # XXX "section ``Extensible metasyntax (<...>)'' not found in S05." - how to
122 # reference that section?
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
123 {
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
124 is('blorg' ~~ /<.alpha>/, 'b', 'leading . prevents capturing');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
125 }
126
127 # If the dot is not followed by an identifier, it is parsed as
128 # a "dotty" postfix of some type, such as an indirect method call
9b324ea @coke fudge for niecza
coke authored
129 #?niecza todo '<.$foo> syntax placeholder'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
130 {
131 # placeholder test for <.$foo>
132 lives_ok({
133 my $method = 'WHAT';
134 'foo bar baz' ~~ /foo <.$method>/;
135 }, '<.$foo> syntax placeholder');
136 }
137
138 # A leading $ indicates an indirect subrule. The variable must contain
139 # either a Regex object, or a string to be compiled as the regex.
140 {
141 my $rule = rx/bar/;
142 my $str = 'qwe';
143 ok('bar' ~~ /<$rule>/, '<$whatever> subrule (Regex, 1)');
144 ok('qwer' ~~ /<$str>/, '<$whatever> subrule (String, 1)');
145
9b324ea @coke fudge for niecza
coke authored
146 #?niecza todo 'the assertion is not captured'
e53b228 @jnthn Correct misunderstanding of the spec in the tests. Unfudging for Rakudo.
jnthn authored
147 is('abar' ~~ /a<$rule>/, 'abar', '<$whatever> subrule (Regex, 2)');
9b324ea @coke fudge for niecza
coke authored
148 #?niecza todo 'the assertion is not captured'
e53b228 @jnthn Correct misunderstanding of the spec in the tests. Unfudging for Rakudo.
jnthn authored
149 is('qwer' ~~ /<$str>r/, 'qwer', '<$whatever> subrule (String, 2)');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
150 }
151
152 # A leading :: indicates a symbolic indirect subrule
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
153 #?rakudo skip 'indirect subrule call not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
154 {
155 my $name = 'alpha';
b482fed .** isn't a valid regex
lwall authored
156 ok('abcdef' ~~ /<::($name)>/, '<::($name)> symbolic indirect subrule');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
157 }
158
159 # A leading @ matches like a bare array except that each element is
160 # treated as a subrule (string or Regex object) rather than as a literal
02c1b47 @coke rakudo fudging
coke authored
161 #?rakudo todo 'nom regression'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
162 {
b482fed .** isn't a valid regex
lwall authored
163 my @first = <a b c .**4>;
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
164 ok('dddd' ~~ /<@first>/, 'strings are treated as a subrule in <@foo>');
165
b482fed .** isn't a valid regex
lwall authored
166 my @second = rx/\.**2/, rx/'.**2'/;
167 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
168 }
169
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
170 # A leading % matches like a bare hash except that
171 # a string value is always treated as a subrule
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
172 #?rakudo skip '<%hash> not implemented'
9b324ea @coke fudge for niecza
coke authored
173 #?niecza skip 'Sigil % is not allowed for regex assertions'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
174 {
175 my %first = {'<alpha>' => '', 'b' => '', 'c' => ''};
176 ok('aeiou' ~~ /<%first>/, 'strings are treated as a subrule in <%foo>');
177
178 my %second = {rx/\.**2/ => '', rx/'.**2'/ => ''};
179 ok('abc.**2def' ~~ /<%second>/, 'Regexes are left alone in <%foo> subrule');
180 }
181
182 # A leading { indicates code that produces a regex to be
183 # interpolated into the pattern at that point as a subrule:
184 {
185 ok('abcdef' ~~ /<{'<al' ~ 'pha>'}>/, 'code interpolation');
186 }
187
188 # 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
189 #?rakudo skip '<&foo()> not implemented'
9b324ea @coke fudge for niecza
coke authored
190 #?niecza skip 'Anonymous submatch returned a Str instead of a Cursor, violating the submatch protocol'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
191 {
192 my sub foo {return '<alpha>'}
193 ok('abcdef' ~~ /<&foo()>/, 'subroutine call interpolation');
194 }
195
196 # If it is a string, the compiled form is cached with the string so that
197 # 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
198 #?rakudo skip '<$subrule> not implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
199 {
200 my $counter = 0;
201 my $subrule = '{$counter++; \'<alpha>\'}';
202
203 'abc' ~~ /<$subrule>/;
204 is($counter, 1, 'code inside string was executed');
205
206 'def' ~~ /<$subrule>/;
9b324ea @coke fudge for niecza
coke authored
207 #?niecza todo "string value was cached"
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
208 is($counter, 1, 'string value was cached');
209 }
210
211 # A leading ?{ or !{ indicates a code assertion
212 {
213 ok('192' ~~ /(\d**1..3) <?{$0 < 256}>/, '<?{...}> works');
9b324ea @coke fudge for niecza
coke authored
214 #?niecza todo '<?{...}>'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
215 ok(!('992' ~~ /(\d**1..3) <?{$0 < 256}>/), '<?{...}> works');
216 ok(!('192' ~~ /(\d**1..3) <!{$0 < 256}>/), '<!{...}> works');
217 ok('992' ~~ /(\d**1..3) <!{$0 < 256}>/, '<!{...}> works');
218 }
219
220 # A leading [ indicates an enumerated character class
221 # A leading - indicates a complemented character class
222 # A leading + may also be supplied
223 # see charset.t
224
225 # The special assertion <.>
226 # see combchar.t
227
b95c468 [t/spec] fix smartlinks in angle-brackets.t
moritz authored
228 # 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
229 {
37ea2a8 [t/spec]: underscore matches <alpha> -- RT #65138.
pmichaud authored
230 ok('1./:"{}=-' ~~ /^[<!alpha> .]+$/, '<!alpha> matches non-letter characters');
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
231 ok(!('abcdef' ~~ /<!alpha>./), '<!alpha> does not match letter characters');
d5fdcaf [t/spec]: Fudge and fix some tests after autoviv changes.
pmichaud authored
232 is(+('.2 1' ~~ /<!before 2> \d/), 1, '<!before>');
233 is +$/.caps, 0, '<!before 2> does not capture';
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
234 }
235
236 # A leading ? indicates a positive zero-width assertion
237 {
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
238 is(~('123abc456def' ~~ /(.+? <?alpha>)/), '123', 'positive zero-width assertion');
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
239 }
240
241 # The <...>, <???>, and <!!!> special tokens have the same "not-defined-yet"
242 # meanings within regexes that the bare elipses have in ordinary code
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
243 #?rakudo skip '..., !!! and ??? in regexes'
9b324ea @coke fudge for niecza
coke authored
244 #?niecza skip 'Action method assertion:sym<???> not yet implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
245 {
246 eval_dies_ok('"foo" ~~ /<...>/', '<...> dies in regex match');
247 # XXX: Should be warns_ok, but we don't have that yet
248 lives_ok({'foo' ~~ /<???>/}, '<???> lives in regex match');
249 eval_dies_ok('"foo" ~~ /<!!!>/', '<!!!> dies in regex match');
250 }
251
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
252 # A leading * indicates that the following pattern allows a partial match.
253 # It always succeeds after matching as many characters as possible.
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
254 #?rakudo skip '<*literal>'
9b324ea @coke fudge for niecza
coke authored
255 #?niecza skip 'Action method assertion:sym<*> not yet implemented'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
256 {
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
257 is('' ~~ /^ <*xyz> $ /, '', 'partial match (0)');
258 is('x' ~~ /^ <*xyz> $ /, 'x', 'partial match (1a)');
259 is('xz' ~~ /^ <*xyz> $ /, 'x', 'partial match (1b)');
260 is('yzx' ~~ /^ <*xyz> $ /, 'x', 'partial match (1c)');
261 is('xy' ~~ /^ <*xyz> $ /, 'xy', 'partial match (2a)');
262 is('xyx' ~~ /^ <*xyz> $ /, 'xy', 'partial match (2a)');
263 is('xyz' ~~ /^ <*xyz> $ /, 'xyz', 'partial match (3)');
264
265 is('abc' ~~ / ^ <*ab+c> $ /, 'abc', 'partial match with quantifier (1)');
266 is('abbbc' ~~ / ^ <*ab+c> $ /, 'abbbc', 'partial match with quantifier (2)');
267 is('ababc' ~~ / ^ <*'ab'+c> $ /, 'ababc', 'partial match with quantifier (3)');
268 is('aba' ~~ / ^ <*'ab'+c> $ /, 'ababc', 'partial match with quantifier (4)');
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
269 }
270
271 # A leading ~~ indicates a recursive call back into some or all of the
272 # current rule. An optional argument indicates which subpattern to re-use
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
273 #?rakudo skip '<~~ ... >'
9b324ea @coke fudge for niecza
coke authored
274 #?niecza skip 'Action method assertion:sym<~~>'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
275 {
276 ok('1.2.' ~~ /\d+\. <~~>/, 'recursive regex using whole pattern');
277 ok('foodbard' ~~ /(foo|bar) d <~~0>/, 'recursive regex with partial pattern');
278 }
279
280 # The following tokens include angles but are not required to balance
281
282 # A <( token indicates the start of a result capture,
283 # while the corresponding )> token indicates its endpoint
284 {
285 is('foo123bar' ~~ /foo <(\d+)> bar/, 123, '<(...)> pair');
286 is('foo456bar' ~~ /foo <(\d+ bar/, '456bar', '<( match');
287 is('foo789bar' ~~ /foo \d+)> bar/, 'foo789', ')> match');
9b324ea @coke fudge for niecza
coke authored
288 #?niecza todo 'non-matching <(...)>'
39bcd72 @jnthn Correct a precedence fail in a test. Rakudo unfudge.
jnthn authored
289 ok(!('foo123' ~~ /foo <(\d+)> bar/), 'non-matching <(...)>');
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
290 }
291
292 # A « or << token indicates a left word boundary.
293 # A » or >> token indicates a right word boundary.
294 {
295 is('abc' ~~ /<<abc/, 'abc', 'left word boundary (string beginning)');
296 is('!abc' ~~ /<<abc/, 'abc', 'left word boundary (\W character)');
297 is('abc' ~~ /abc>>/, 'abc', 'right word boundary (string end)');
298 is('abc!' ~~ /abc>>/, 'abc', 'right word boundary (\W character)');
299 is('!abc!' ~~ /<<abc>>/, 'abc', 'both word boundaries (\W character)');
300 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
301
e76dd4c @moritz s/done_testing/done/ as per recent S24 changes
moritz authored
302 done();
3887bcf [t/spec] as per r29067 <foo=bar> aliasing preserves the $<bar> captur…
moritz authored
303
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
304 # vim: ft=perl6
Something went wrong with that request. Please try again.