Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 142 lines (115 sloc) 3.31 kB
499fea9 [gsoc_spectest] moved do.t into spec, added tests, fudged for rakudo.…
Auzon authored
1 use v6;
2
3 use Test;
4
5 plan 23;
6
7 # L<S04/The do-once loop/"can't" put "statement modifier">
8 #?rakudo 3 todo 'do {} while/until/if is valid but should not be'
9 eval_dies_ok 'my $i = 1; do { $i++ } while $i < 5;',
10 "'do' can't take the 'while' modifier";
11
12 eval_dies_ok 'my $i = 1; do { $i++ } until $i > 4;',
13 "'do' can't take the 'until' modifier";
14
15 eval_dies_ok 'my $i = 1; do { $i++ } if $i;',
16 "'do' can't take the 'if' modifier";
17
18 eval_dies_ok 'my $i; do { $i++ } for 1..3;',
19 "'do' can't take the 'for' modifier";
20
21 eval_dies_ok 'my $i; do { $i++ } unless $i;',
22 "'do' can't take the 'unless' modifier";
23
24 eval_dies_ok 'my $i; do { $i++ } given $i;',
25 "'do' can't take the 'given' modifier";
26
27 # L<S04/The do-once loop/statement "prefixing with" do>
28 #?rakudo skip 'my($a, $b, $c) = "a" .. "c" not implemented'
29 {
30 my $x;
31 my ($a, $b, $c) = 'a' .. 'c';
32
33 $x = do if $a { $b } else { $c };
34 is $x, 'b', "prefixing 'if' statement with 'do' (then)";
35
36 $x = do if !$a { $b } else { $c };
37 is $x, 'c', "prefixing 'if' statement with 'do' (else)";
38
39 =begin comment
40 If the final statement is a conditional which does not execute
41 any branch, the return value is undef in item context and ()
42 in list context.
43 =end comment
44 $x = do if 0 { 1 } elsif 0 { 2 };
45 is $x, undef, 'when if does not execute any branch, return undef';
46 }
47
48 {
49 my $ret = do given 3 {
50 when 3 { 1 }
51 };
52 is($ret, 1, 'do STMT works');
53 }
54
55 {
56 my $ret = do { given 3 {
57 when 3 { 1 }
58 } };
59 is($ret, 1, 'do { STMT } works');
60 }
61
62 # L<S04/The do-once loop/"you may use" do "on an expression">
63 {
64 my $ret = do 42;
65 is($ret, 42, 'do EXPR should also work (single number)');
66
67 $ret = do 3 + 2;
68 is($ret, 5, 'do EXPR should also work (simple + expr)');
69
70 $ret = do do 5;
71 is($ret, 5, 'nested do (1)');
72
73 $ret = do {do 5};
74 is($ret, 5, 'nested do (2)');
75
76 # precedence decisions do not cross a do boundary
77 $ret = 2 * do 2 + 5;
78 is($ret, 14, 'do affects precendence correctly');
79 }
80
81 # L<S04/The do-once loop/"can take" "loop control statements">
82 #?rakudo skip 'next not implemented'
83 {
84 my $i;
85 do {
86 $i++;
87 next;
88 $i--;
89 };
90 is $i, 1, "'next' works in 'do' block";
91 }
92
93 #?rakudo skip 'last not implemented'
94 {
95 is eval('
96 my $i;
97 do {
98 $i++;
99 last;
100 $i--;
101 };
102 $i;
103 '), 1, "'last' works in 'do' block";
104 }
105
106 # IRC notes:
107 # <agentzh> audreyt: btw, can i use redo in the do-once loop?
108 # <audreyt> it can, and it will redo it
109 #?rakudo skip 'redo not implemented'
110 {
111 is eval('
112 my $i;
113 do {
114 $i++;
115 redo if $i < 3;
116 $i--;
117 };
118 $i;
119 '), 2, "'redo' works in 'do' block";
120 }
121
3152b01 [t] some updates to README, and fixed all but one smartlink in t/spec/
moritz authored
122 # L<S04/The do-once loop/"bare block is not a do-once">
499fea9 [gsoc_spectest] moved do.t into spec, added tests, fudged for rakudo.…
Auzon authored
123 {
124 eval_dies_ok 'my $i; { $i++; next; $i--; }',
125 "bare block can't take 'next'";
126
127 eval_dies_ok 'my $i; { $i++; last; $i--; }',
128 "bare block can't take 'last'";
129
130 eval_dies_ok 'my $i; { $i++; redo; $i--; }',
131 "bare block can't take 'last'";
132 }
133
134 # L<S04/Statement parsing/"final closing curly on a line"
135 # reverts to semicolon>
136 {
137 my $a = do {
138 1 + 2;
139 } # no trailing `;'
140 is $a, 3, "final `}' on a line reverted to `;'";
141 }
Something went wrong with that request. Please try again.