Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 143 lines (116 sloc) 3.41 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 #?rakudo 1 todo 'rakudo does not return undef when do if does not return a value'
46 is $x, undef, 'when if does not execute any branch, return undef';
47 }
48
49 {
50 my $ret = do given 3 {
51 when 3 { 1 }
52 };
53 is($ret, 1, 'do STMT works');
54 }
55
56 {
57 my $ret = do { given 3 {
58 when 3 { 1 }
59 } };
60 is($ret, 1, 'do { STMT } works');
61 }
62
63 # L<S04/The do-once loop/"you may use" do "on an expression">
64 {
65 my $ret = do 42;
66 is($ret, 42, 'do EXPR should also work (single number)');
67
68 $ret = do 3 + 2;
69 is($ret, 5, 'do EXPR should also work (simple + expr)');
70
71 $ret = do do 5;
72 is($ret, 5, 'nested do (1)');
73
74 $ret = do {do 5};
75 is($ret, 5, 'nested do (2)');
76
77 # precedence decisions do not cross a do boundary
78 $ret = 2 * do 2 + 5;
79 is($ret, 14, 'do affects precendence correctly');
80 }
81
82 # L<S04/The do-once loop/"can take" "loop control statements">
83 #?rakudo skip 'next not implemented'
84 {
85 my $i;
86 do {
87 $i++;
88 next;
89 $i--;
90 };
91 is $i, 1, "'next' works in 'do' block";
92 }
93
94 #?rakudo skip 'last not implemented'
95 {
96 is eval('
97 my $i;
98 do {
99 $i++;
100 last;
101 $i--;
102 };
103 $i;
104 '), 1, "'last' works in 'do' block";
105 }
106
107 # IRC notes:
108 # <agentzh> audreyt: btw, can i use redo in the do-once loop?
109 # <audreyt> it can, and it will redo it
110 #?rakudo skip 'redo not implemented'
111 {
112 is eval('
113 my $i;
114 do {
115 $i++;
116 redo if $i < 3;
117 $i--;
118 };
119 $i;
120 '), 2, "'redo' works in 'do' block";
121 }
122
123 # L<S04/The do-once loop/"bare block" "no longer a do-once loop">
124 {
125 eval_dies_ok 'my $i; { $i++; next; $i--; }',
126 "bare block can't take 'next'";
127
128 eval_dies_ok 'my $i; { $i++; last; $i--; }',
129 "bare block can't take 'last'";
130
131 eval_dies_ok 'my $i; { $i++; redo; $i--; }',
132 "bare block can't take 'last'";
133 }
134
135 # L<S04/Statement parsing/"final closing curly on a line"
136 # reverts to semicolon>
137 {
138 my $a = do {
139 1 + 2;
140 } # no trailing `;'
141 is $a, 3, "final `}' on a line reverted to `;'";
142 }
Something went wrong with that request. Please try again.