Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 322 lines (263 sloc) 11.525 kB
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
1 use v6;
2 use Test;
3
5386c20 @jnthn Test for contextuals in <?{...}> assertions.
jnthn authored
4 plan 91;
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 {
22 is('aaaaa' ~~ /< a aa aaaa >/, 'aaaa', 'leading whitespace quotes words (space)');
23 is('aaaaa' ~~ /< a aa aaaa >/, 'aaaa', 'leading whitespace quotes words (tab)');
24
0cbc065 @lizmat s/eval_dies_ok/eval-dies-ok/g
lizmat authored
25 eval-dies-ok('"aaaa" ~~ /<a aa>/', '<...> without whitespace calls a function (not quote words)');
645eda3 @jnthn Test /< hello >/.
jnthn authored
26
27 is('hello' ~~ /< hello >/, 'hello', 'degenerate case of quote list');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
28 }
29
30 # A leading alphabetic character means it's a capturing grammatical assertion
31 {
32 is('moose' ~~ /<alpha>/, 'm', 'capturing grammatical assertion (1)');
33 is('1a2b3c' ~~ /<alpha>/, 'a', 'capturing grammatical assertion (2)');
34 }
35
4bda0c5 [t/spec] tests for RT #64464, regexes with dashes and hyphens
moritz authored
36 {
b09f2a9 @moritz fix and unfudge RT #64464 tests
moritz authored
37 my regex with-dash { '-' }
4bda0c5 [t/spec] tests for RT #64464, regexes with dashes and hyphens
moritz authored
38 ok '-' ~~ /<with-dash>/, 'can call regexes which dashes (positive)';
39 ok '|' !~~ /<with-dash>/, 'can call regexes which dashes (negative)';
40
b09f2a9 @moritz fix and unfudge RT #64464 tests
moritz authored
41 my regex with'hyphen { a }
4bda0c5 [t/spec] tests for RT #64464, regexes with dashes and hyphens
moritz authored
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
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
47 #?rakudo skip '<test()> NYI RT #124519'
9b324ea @coke fudge for niecza
coke authored
48 #?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
49 {
50 my $pass = 0;
51 my sub test (Int $a = 1) {$pass += $a}
52 '3.14' ~~ /3 <test()>/;
53 ok($pass, 'function call (no arguments)');
54 }
55
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
56 #?rakudo skip '<test()> NYI RT #124520'
9b324ea @coke fudge for niecza
coke authored
57 #?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
58 {
59 my $pass = 0;
60 my sub test (Int $a) {$pass += $a}
61 '3.14' ~~ /3 <test(2)>/;
62 ok($pass, 'function call (with arguments)');
63 }
64
65 # If the first character after the identifier is an =,
66 # then the identifier is taken as an alias for what follows
67 {
3887bcf [t/spec] as per r29067 <foo=bar> aliasing preserves the $<bar> captur…
moritz authored
68 ok 'foo' ~~ /<foo=alpha>/, 'basic <foo=bar> aliasing';
69 is $<foo>, 'f', 'alias works';
70 is $<alpha>, 'f', 'alias does not throw away original name';
71 }
72
1fee361 [t/spec] test for <foo=.bar> aliasing in regex
moritz authored
73 {
74 ok 'foo' ~~ /<foo=.alpha>/, 'basic <foo=.bar> aliasing';
75 is $<foo>, 'f', 'alias works';
76 ok !defined($<alpha>), 'alias does throw away original name';
77 }
78
3887bcf [t/spec] as per r29067 <foo=bar> aliasing preserves the $<bar> captur…
moritz authored
79 {
80 ok '123gb' ~~ / <foo=.alpha> /, '<foo=.bar>';
81 is $<foo>, 'g', '=. renaming worked';
2544bcf @jdhore Remove most remaining .notdef's.
jdhore authored
82 nok $<alpha>.defined, '=. removed the old capture name';
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
83 }
84
59e90f5 @FROGGS add tests for captures in assertions
FROGGS authored
85 {
86 ok 'foobar' ~~ / <foo=[bao]>+ /, '<foo=[bao]>';
87 is $<foo>, < o o b a >, '=[...] renaming worked';
88
89 ok 'foobar' ~~ / <bar=-[bao]>+ /, '<bar=-[bao]>';
90 is $<bar>, 'f', '=[...] renaming worked';
91 }
92
93 {
94 ok 'a.' ~~ / <foo=:Letter> /, '<foo=:Letter>';
95 is $<foo>, 'a', '=:UniProp renaming worked';
96
97 ok 'a.' ~~ / <bar=:!Letter> /, '<bar=:!Letter>';
98 is $<bar>, '.', '=:!UniProp renaming worked';
99
100 ok 'a.' ~~ / <baz=-:Letter> /, '<baz=-:Letter>';
101 is $<baz>, '.', '=-:UniProp renaming worked';
102 }
103
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
104 # If the first character after the identifier is whitespace, the subsequent
105 # text (following any whitespace) is passed as a regex
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
106 #?rakudo skip 'angle quotes in regexes RT #124521'
9b324ea @coke fudge for niecza
coke authored
107 #?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
108 {
109 my $is_regex = 0;
110 my sub test ($a) {$is_regex++ if $a ~~ Regex}
111
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
112 'whatever' ~~ /w < test hat >/;
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
113 ok($is_regex, 'text passed as a regex (1)');
114
115 $is_regex = 0;
116 'whatever' ~~ /w <test complicated . regex '<goes here>'>/;
117 ok($is_regex, 'more complicated text passed as a regex (2)');
118 }
119
120 # If the first character is a colon followed by whitespace the
121 # rest of the text is taken as a list of arguments to the method
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
122 #?rakudo skip 'colon arguments NYI RT #124522'
9b324ea @coke fudge for niecza
coke authored
123 #?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
124 {
125 my $called_ok = 0;
126 my sub test ($a, $b) {$called_ok++ if $a && $b}
127
128 'some text' ~~ /some <test: 3, 5>/;
129 ok($called_ok, 'method call syntax in <...>');
130 }
131
132 # No other characters are allowed after the initial identifier.
133 {
0cbc065 @lizmat s/eval_dies_ok/eval-dies-ok/g
lizmat authored
134 eval-dies-ok('"foo" ~~ /<test*>/', 'no other characters are allowed (*)');
135 eval-dies-ok('"foo" ~~ /<test|>/', 'no other characters are allowed (|)');
136 eval-dies-ok('"foo" ~~ /<test&>/', 'no other characters are allowed (&)');
137 eval-dies-ok('"foo" ~~ /<test:>/', 'no other characters are allowed (:)');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
138 }
139
3a41ccc Fix all currently broken smartlinks to S05
Coleoid authored
140 # L<S05/Extensible metasyntax (C<< <...> >>)/explicitly calls a method as a subrule>
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
141 {
b194db4 [t/spec]: Some rakudo fudge updates, not yet ready for spectest regr…
pmichaud authored
142 is('blorg' ~~ /<.alpha>/, 'b', 'leading . prevents capturing');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
143 }
144
145 # If the dot is not followed by an identifier, it is parsed as
146 # a "dotty" postfix of some type, such as an indirect method call
9b324ea @coke fudge for niecza
coke authored
147 #?niecza todo '<.$foo> syntax placeholder'
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
148 {
149 # placeholder test for <.$foo>
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
150 lives-ok({
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
151 my $method = 'WHAT';
152 'foo bar baz' ~~ /foo <.$method>/;
153 }, '<.$foo> syntax placeholder');
154 }
155
156 # A leading $ indicates an indirect subrule. The variable must contain
157 # either a Regex object, or a string to be compiled as the regex.
158 {
159 my $rule = rx/bar/;
160 my $str = 'qwe';
161 ok('bar' ~~ /<$rule>/, '<$whatever> subrule (Regex, 1)');
162 ok('qwer' ~~ /<$str>/, '<$whatever> subrule (String, 1)');
163
e53b228 @jnthn Correct misunderstanding of the spec in the tests. Unfudging for Rakudo.
jnthn authored
164 is('abar' ~~ /a<$rule>/, 'abar', '<$whatever> subrule (Regex, 2)');
165 is('qwer' ~~ /<$str>r/, 'qwer', '<$whatever> subrule (String, 2)');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
166 }
167
168 # A leading :: indicates a symbolic indirect subrule
169 {
170 my $name = 'alpha';
ec15161 @FROGGS unfuge/adjust test for indirect subrules
FROGGS authored
171 is('..abcdef--' ~~ /<::($name)>+/, 'abcdef', '<::($name)> symbolic indirect subrule');
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
172 }
173
174 # A leading @ matches like a bare array except that each element is
175 # treated as a subrule (string or Regex object) rather than as a literal
176 {
b482fed .** isn't a valid regex
lwall authored
177 my @first = <a b c .**4>;
00b635c [gsoc_spectest] added new angle-brackets.t file to test all <...> for…
Auzon authored
178 ok('dddd' ~~ /<@first>/, 'strings are treated as a subrule in <@foo>');
179
b482fed .** isn't a valid regex
lwall authored
180 my @second = rx/\.**2/, rx/'.**2'/;
181 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
182 }
183
28588a0 @FROGGS skip test for Perl 6 not for compilers
FROGGS authored
184 #?v6.0.0+ skip 'The use of a hash as an assertion is reserved.'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
185 {
6620326 @lizmat Fix %h = { a => 1 } confusion
lizmat authored
186 my %first = '<alpha>' => '', 'b' => '', 'c' => '';
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
187 ok('aeiou' ~~ /<%first>/, 'strings are treated as a subrule in <%foo>');
188
6620326 @lizmat Fix %h = { a => 1 } confusion
lizmat authored
189 my %second = rx/\.**2/ => '', rx/'.**2'/ => '';
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
190 ok('abc.**2def' ~~ /<%second>/, 'Regexes are left alone in <%foo> subrule');
191 }
192
193 # A leading { indicates code that produces a regex to be
194 # interpolated into the pattern at that point as a subrule:
195 {
196 ok('abcdef' ~~ /<{'<al' ~ 'pha>'}>/, 'code interpolation');
197 }
198
199 # A leading & interpolates the return value of a subroutine call as a regex.
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
200 #?rakudo skip '<&foo()> NYI RT #124523'
9b324ea @coke fudge for niecza
coke authored
201 #?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
202 {
203 my sub foo {return '<alpha>'}
204 ok('abcdef' ~~ /<&foo()>/, 'subroutine call interpolation');
205 }
206
207 # If it is a string, the compiled form is cached with the string so that
208 # it is not recompiled next time you use it unless the string changes.
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
209 #?rakudo skip '<$subrule> NYI RT #124524'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
210 {
211 my $counter = 0;
212 my $subrule = '{$counter++; \'<alpha>\'}';
213
214 'abc' ~~ /<$subrule>/;
215 is($counter, 1, 'code inside string was executed');
216
217 'def' ~~ /<$subrule>/;
9b324ea @coke fudge for niecza
coke authored
218 #?niecza todo "string value was cached"
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
219 is($counter, 1, 'string value was cached');
220 }
221
222 # A leading ?{ or !{ indicates a code assertion
223 {
9478f9a @jnthn Correct some tests that had wrong expectations on backtracking.
jnthn authored
224 ok('192' ~~ /(\d**3) <?{$0 < 256}>/, '<?{...}> works');
225 ok(!('992' ~~ /(\d**3) <?{$0 < 256}>/), '<?{...}> works');
226 ok(!('192' ~~ /(\d**3) <!{$0 < 256}>/), '<!{...}> works');
227 ok('992' ~~ /(\d**3) <!{$0 < 256}>/, '<!{...}> works');
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
228 }
229
5386c20 @jnthn Test for contextuals in <?{...}> assertions.
jnthn authored
230 {
231 my $*x = 0;
232 nok '' ~~ /<?{$*x}>/, 'Can use contextuals with <?{...}>';
233 }
234
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
235 # A leading [ indicates an enumerated character class
236 # A leading - indicates a complemented character class
237 # A leading + may also be supplied
238 # see charset.t
239
240 # The special assertion <.>
241 # see combchar.t
242
b95c468 [t/spec] fix smartlinks in angle-brackets.t
moritz authored
243 # 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
244 {
37ea2a8 [t/spec]: underscore matches <alpha> -- RT #65138.
pmichaud authored
245 ok('1./:"{}=-' ~~ /^[<!alpha> .]+$/, '<!alpha> matches non-letter characters');
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
246 ok(!('abcdef' ~~ /<!alpha>./), '<!alpha> does not match letter characters');
d5fdcaf [t/spec]: Fudge and fix some tests after autoviv changes.
pmichaud authored
247 is(+('.2 1' ~~ /<!before 2> \d/), 1, '<!before>');
248 is +$/.caps, 0, '<!before 2> does not capture';
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
249 }
250
251 # A leading ? indicates a positive zero-width assertion
252 {
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
253 is(~('123abc456def' ~~ /(.+? <?alpha>)/), '123', 'positive zero-width assertion');
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
254 }
255
256 # The <...>, <???>, and <!!!> special tokens have the same "not-defined-yet"
257 # meanings within regexes that the bare elipses have in ordinary code
9b324ea @coke fudge for niecza
coke authored
258 #?niecza skip 'Action method assertion:sym<???> not yet implemented'
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
259 {
0cbc065 @lizmat s/eval_dies_ok/eval-dies-ok/g
lizmat authored
260 eval-dies-ok('"foo" ~~ /<...>/', '<...> dies in regex match');
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
261 # XXX: Should be warns_ok, but we don't have that yet
9f47446 @lizmat s/lives_ok/lives-ok/g
lizmat authored
262 lives-ok({'foo' ~~ /<???>/}, '<???> lives in regex match');
fa508be @lizmat More S05 test unfudges, skip->todo and additional tests
lizmat authored
263 #?rakudo todo '!!! in regexes'
0cbc065 @lizmat s/eval_dies_ok/eval-dies-ok/g
lizmat authored
264 eval-dies-ok('"foo" ~~ /<!!!>/', '<!!!> dies in regex match');
27ad25f [gsoc_spectest] Added more angle bracket syntax tests. Almost done wi…
Auzon authored
265 }
266
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
267 # A leading * indicates that the following pattern allows a partial match.
268 # It always succeeds after matching as many characters as possible.
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
269 #?rakudo skip '<*literal> RT #124525'
9b324ea @coke fudge for niecza
coke authored
270 #?niecza skip 'Action method assertion:sym<*> not yet implemented'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
271 {
0e01a72 [t/spec] improved partial match tests, and fudged for rakudo
moritz authored
272 is('' ~~ /^ <*xyz> $ /, '', 'partial match (0)');
273 is('x' ~~ /^ <*xyz> $ /, 'x', 'partial match (1a)');
274 is('xz' ~~ /^ <*xyz> $ /, 'x', 'partial match (1b)');
275 is('yzx' ~~ /^ <*xyz> $ /, 'x', 'partial match (1c)');
276 is('xy' ~~ /^ <*xyz> $ /, 'xy', 'partial match (2a)');
277 is('xyx' ~~ /^ <*xyz> $ /, 'xy', 'partial match (2a)');
278 is('xyz' ~~ /^ <*xyz> $ /, 'xyz', 'partial match (3)');
279
280 is('abc' ~~ / ^ <*ab+c> $ /, 'abc', 'partial match with quantifier (1)');
281 is('abbbc' ~~ / ^ <*ab+c> $ /, 'abbbc', 'partial match with quantifier (2)');
282 is('ababc' ~~ / ^ <*'ab'+c> $ /, 'ababc', 'partial match with quantifier (3)');
283 is('aba' ~~ / ^ <*'ab'+c> $ /, 'ababc', 'partial match with quantifier (4)');
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
284 }
285
286 # A leading ~~ indicates a recursive call back into some or all of the
287 # current rule. An optional argument indicates which subpattern to re-use
9b324ea @coke fudge for niecza
coke authored
288 #?niecza skip 'Action method assertion:sym<~~>'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
289 {
cb86fc4 @jnthn Correct and unfudge a test. This tweak is needed since the call is re…
jnthn authored
290 ok('1.2.' ~~ /\d+\. <~~> | <?>/, 'recursive regex using whole pattern');
291 #?rakudo skip '<~~ ... >'
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
292 ok('foodbard' ~~ /(foo|bar) d <~~0>/, 'recursive regex with partial pattern');
293 }
294
295 # The following tokens include angles but are not required to balance
296
297 # A <( token indicates the start of a result capture,
298 # while the corresponding )> token indicates its endpoint
299 {
300 is('foo123bar' ~~ /foo <(\d+)> bar/, 123, '<(...)> pair');
301 is('foo456bar' ~~ /foo <(\d+ bar/, '456bar', '<( match');
302 is('foo789bar' ~~ /foo \d+)> bar/, 'foo789', ')> match');
39bcd72 @jnthn Correct a precedence fail in a test. Rakudo unfudge.
jnthn authored
303 ok(!('foo123' ~~ /foo <(\d+)> bar/), 'non-matching <(...)>');
baf17a4 @pmichaud Add some more <( and )> tests.
pmichaud authored
304
305 is('foo123bar' ~~ /foo <( bar || ....../, 'foo123', '<( in backtracking');
dc9f15f @colomon Lots of niecza fudges.
colomon authored
306 #?niecza todo
baf17a4 @pmichaud Add some more <( and )> tests.
pmichaud authored
307 is('foo123bar' ~~ /foo <( 123 <( bar/, 'bar', 'multiple <(');
308 is('foo123bar' ~~ /foo <( 123 [ <( xyz ]?/, '123', 'multiple <( backtracking');
dbc2768 [gsoc_spectest] finished up angle bracket syntax tests (added 21 tests)
Auzon authored
309 }
310
311 # A « or << token indicates a left word boundary.
312 # A » or >> token indicates a right word boundary.
313 {
314 is('abc' ~~ /<<abc/, 'abc', 'left word boundary (string beginning)');
315 is('!abc' ~~ /<<abc/, 'abc', 'left word boundary (\W character)');
316 is('abc' ~~ /abc>>/, 'abc', 'right word boundary (string end)');
317 is('abc!' ~~ /abc>>/, 'abc', 'right word boundary (\W character)');
318 is('!abc!' ~~ /<<abc>>/, 'abc', 'both word boundaries (\W character)');
319 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
320
321 # vim: ft=perl6
Something went wrong with that request. Please try again.