Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 302 lines (250 sloc) 11.016 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
e53b228 @jnthn Correct misunderstanding of the spec in the tests. Unfudging for Rakudo.
jnthn authored
146 is('abar' ~~ /a<$rule>/, 'abar', '<$whatever> subrule (Regex, 2)');
147 is('qwer' ~~ /<$str>r/, 'qwer', '<$whatever> subrule (String, 2)');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
148 }
149
150 # A leading :: indicates a symbolic indirect subrule
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
151 #?rakudo skip 'indirect subrule call not implemented'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
152 {
153 my $name = 'alpha';
b482fed .** isn't a valid regex
lwall authored
154 ok('abcdef' ~~ /<::($name)>/, '<::($name)> symbolic indirect subrule');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
155 }
156
157 # A leading @ matches like a bare array except that each element is
158 # treated as a subrule (string or Regex object) rather than as a literal
02c1b47 @coke rakudo fudging
coke authored
159 #?rakudo todo 'nom regression'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
160 {
b482fed .** isn't a valid regex
lwall authored
161 my @first = <a b c .**4>;
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
162 ok('dddd' ~~ /<@first>/, 'strings are treated as a subrule in <@foo>');
163
b482fed .** isn't a valid regex
lwall authored
164 my @second = rx/\.**2/, rx/'.**2'/;
165 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
166 }
167
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
168 # A leading % matches like a bare hash except that
169 # a string value is always treated as a subrule
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
170 #?rakudo skip '<%hash> not implemented'
9b324ea @coke fudge for niecza
coke authored
171 #?niecza skip 'Sigil % is not allowed for regex assertions'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
172 {
173 my %first = {'<alpha>' => '', 'b' => '', 'c' => ''};
174 ok('aeiou' ~~ /<%first>/, 'strings are treated as a subrule in <%foo>');
175
176 my %second = {rx/\.**2/ => '', rx/'.**2'/ => ''};
177 ok('abc.**2def' ~~ /<%second>/, 'Regexes are left alone in <%foo> subrule');
178 }
179
180 # A leading { indicates code that produces a regex to be
181 # interpolated into the pattern at that point as a subrule:
182 {
183 ok('abcdef' ~~ /<{'<al' ~ 'pha>'}>/, 'code interpolation');
184 }
185
186 # 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
187 #?rakudo skip '<&foo()> not implemented'
9b324ea @coke fudge for niecza
coke authored
188 #?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
189 {
190 my sub foo {return '<alpha>'}
191 ok('abcdef' ~~ /<&foo()>/, 'subroutine call interpolation');
192 }
193
194 # If it is a string, the compiled form is cached with the string so that
195 # 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
196 #?rakudo skip '<$subrule> not implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
197 {
198 my $counter = 0;
199 my $subrule = '{$counter++; \'<alpha>\'}';
200
201 'abc' ~~ /<$subrule>/;
202 is($counter, 1, 'code inside string was executed');
203
204 'def' ~~ /<$subrule>/;
9b324ea @coke fudge for niecza
coke authored
205 #?niecza todo "string value was cached"
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
206 is($counter, 1, 'string value was cached');
207 }
208
209 # A leading ?{ or !{ indicates a code assertion
210 {
9478f9a @jnthn Correct some tests that had wrong expectations on backtracking.
jnthn authored
211 ok('192' ~~ /(\d**3) <?{$0 < 256}>/, '<?{...}> works');
9b324ea @coke fudge for niecza
coke authored
212 #?niecza todo '<?{...}>'
9478f9a @jnthn Correct some tests that had wrong expectations on backtracking.
jnthn authored
213 ok(!('992' ~~ /(\d**3) <?{$0 < 256}>/), '<?{...}> works');
214 ok(!('192' ~~ /(\d**3) <!{$0 < 256}>/), '<!{...}> works');
215 ok('992' ~~ /(\d**3) <!{$0 < 256}>/, '<!{...}> works');
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
216 }
217
218 # A leading [ indicates an enumerated character class
219 # A leading - indicates a complemented character class
220 # A leading + may also be supplied
221 # see charset.t
222
223 # The special assertion <.>
224 # see combchar.t
225
b95c468 [t/spec] fix smartlinks in angle-brackets.t
moritz authored
226 # 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
227 {
37ea2a8 [t/spec]: underscore matches <alpha> -- RT #65138.
pmichaud authored
228 ok('1./:"{}=-' ~~ /^[<!alpha> .]+$/, '<!alpha> matches non-letter characters');
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
229 ok(!('abcdef' ~~ /<!alpha>./), '<!alpha> does not match letter characters');
d5fdcaf [t/spec]: Fudge and fix some tests after autoviv changes.
pmichaud authored
230 is(+('.2 1' ~~ /<!before 2> \d/), 1, '<!before>');
231 is +$/.caps, 0, '<!before 2> does not capture';
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
232 }
233
234 # A leading ? indicates a positive zero-width assertion
235 {
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
236 is(~('123abc456def' ~~ /(.+? <?alpha>)/), '123', 'positive zero-width assertion');
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
237 }
238
239 # The <...>, <???>, and <!!!> special tokens have the same "not-defined-yet"
240 # meanings within regexes that the bare elipses have in ordinary code
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
241 #?rakudo skip '..., !!! and ??? in regexes'
9b324ea @coke fudge for niecza
coke authored
242 #?niecza skip 'Action method assertion:sym<???> not yet implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
243 {
244 eval_dies_ok('"foo" ~~ /<...>/', '<...> dies in regex match');
245 # XXX: Should be warns_ok, but we don't have that yet
246 lives_ok({'foo' ~~ /<???>/}, '<???> lives in regex match');
247 eval_dies_ok('"foo" ~~ /<!!!>/', '<!!!> dies in regex match');
248 }
249
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
250 # A leading * indicates that the following pattern allows a partial match.
251 # It always succeeds after matching as many characters as possible.
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
252 #?rakudo skip '<*literal>'
9b324ea @coke fudge for niecza
coke authored
253 #?niecza skip 'Action method assertion:sym<*> not yet implemented'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
254 {
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
255 is('' ~~ /^ <*xyz> $ /, '', 'partial match (0)');
256 is('x' ~~ /^ <*xyz> $ /, 'x', 'partial match (1a)');
257 is('xz' ~~ /^ <*xyz> $ /, 'x', 'partial match (1b)');
258 is('yzx' ~~ /^ <*xyz> $ /, 'x', 'partial match (1c)');
259 is('xy' ~~ /^ <*xyz> $ /, 'xy', 'partial match (2a)');
260 is('xyx' ~~ /^ <*xyz> $ /, 'xy', 'partial match (2a)');
261 is('xyz' ~~ /^ <*xyz> $ /, 'xyz', 'partial match (3)');
262
263 is('abc' ~~ / ^ <*ab+c> $ /, 'abc', 'partial match with quantifier (1)');
264 is('abbbc' ~~ / ^ <*ab+c> $ /, 'abbbc', 'partial match with quantifier (2)');
265 is('ababc' ~~ / ^ <*'ab'+c> $ /, 'ababc', 'partial match with quantifier (3)');
266 is('aba' ~~ / ^ <*'ab'+c> $ /, 'ababc', 'partial match with quantifier (4)');
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
267 }
268
269 # A leading ~~ indicates a recursive call back into some or all of the
270 # current rule. An optional argument indicates which subpattern to re-use
9b324ea @coke fudge for niecza
coke authored
271 #?niecza skip 'Action method assertion:sym<~~>'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
272 {
cb86fc4 @jnthn Correct and unfudge a test. This tweak is needed since the call is re…
jnthn authored
273 ok('1.2.' ~~ /\d+\. <~~> | <?>/, 'recursive regex using whole pattern');
274 #?rakudo skip '<~~ ... >'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
275 ok('foodbard' ~~ /(foo|bar) d <~~0>/, 'recursive regex with partial pattern');
276 }
277
278 # The following tokens include angles but are not required to balance
279
280 # A <( token indicates the start of a result capture,
281 # while the corresponding )> token indicates its endpoint
282 {
283 is('foo123bar' ~~ /foo <(\d+)> bar/, 123, '<(...)> pair');
284 is('foo456bar' ~~ /foo <(\d+ bar/, '456bar', '<( match');
285 is('foo789bar' ~~ /foo \d+)> bar/, 'foo789', ')> match');
39bcd72 @jnthn Correct a precedence fail in a test. Rakudo unfudge.
jnthn authored
286 ok(!('foo123' ~~ /foo <(\d+)> bar/), 'non-matching <(...)>');
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
287 }
288
289 # A « or << token indicates a left word boundary.
290 # A » or >> token indicates a right word boundary.
291 {
292 is('abc' ~~ /<<abc/, 'abc', 'left word boundary (string beginning)');
293 is('!abc' ~~ /<<abc/, 'abc', 'left word boundary (\W character)');
294 is('abc' ~~ /abc>>/, 'abc', 'right word boundary (string end)');
295 is('abc!' ~~ /abc>>/, 'abc', 'right word boundary (\W character)');
296 is('!abc!' ~~ /<<abc>>/, 'abc', 'both word boundaries (\W character)');
297 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
298
e76dd4c @moritz s/done_testing/done/ as per recent S24 changes
moritz authored
299 done();
3887bcf [t/spec] as per r29067 <foo=bar> aliasing preserves the $<bar> captur…
moritz authored
300
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
301 # vim: ft=perl6
Something went wrong with that request. Please try again.