Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 186 lines (160 sloc) 4.579 kb
ab5204d [t] moved tests for let and temp to spec/
moritz authored
1 use v6;
2
3 use Test;
4
5 plan 31;
6
7 # L<S04/The Relationship of Blocks and Declarations/function has been renamed>
8 {
9 my $a = 42;
10 {
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and oth...
sorear authored
11 is((temp $a = 23; $a), 23, "temp() changed the variable (1)");
ab5204d [t] moved tests for let and temp to spec/
moritz authored
12 }
13 is $a, 42, "temp() restored the variable (1)";
14 }
15
16 # Test that temp() restores the variable at scope exit, not at subroutine
17 # entry.
18 {
19 my $a = 42;
20 my $get_a = { $a };
21 {
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and oth...
sorear authored
22 is((temp $a = 23; $a), 23, "temp() changed the variable (2-1)");
ab5204d [t] moved tests for let and temp to spec/
moritz authored
23 is $get_a(), 23, "temp() changed the variable (2-2)";
24 }
25 is $a, 42, "temp() restored the variable (2)";
26 }
27
28 # temp() shouldn't change the variable containers
29 {
30 my $a = 42;
31 my $get_a = { $a };
32 {
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and oth...
sorear authored
33 ok((temp $a = 23; $a =:= $get_a()), "temp() shouldn't change the variable containers");
ab5204d [t] moved tests for let and temp to spec/
moritz authored
34 }
35 }
36
37 {
38 our $pkgvar = 42;
39 {
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and oth...
sorear authored
40 is((temp $pkgvar = 'not 42'; $pkgvar), 'not 42', "temp() changed the package variable (3-1)");
ab5204d [t] moved tests for let and temp to spec/
moritz authored
41 }
42 is $pkgvar, 42, "temp() restored the package variable (3-2)";
43 }
44
45 # Test that temp() restores variable even when not exited regularly (using a
46 # (possibly implicit) call to return()), but when left because of an exception.
47 {
48 my $a = 42;
49 try {
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and oth...
sorear authored
50 is((temp $a = 23; $a), 23, "temp() changed the variable in a try block");
ab5204d [t] moved tests for let and temp to spec/
moritz authored
51 die 57;
52 };
53 is $a, 42, "temp() restored the variable, the block was exited using an exception";
54 }
55
56 eval('
57 {
58 my @array = (0, 1, 2);
59 {
60 temp @array[1] = 42;
61 is @array[1], 42, "temp() changed our array element";
62 }
63 is @array[1], 1, "temp() restored our array element";
64 }
65 "1 - delete this line when the parsefail eval() is removed";
8b5a87e @moritz reverse order of skip() arguments all over the test suite
moritz authored
66 ') or skip("parsefail: temp \@array[1]", 2);
ab5204d [t] moved tests for let and temp to spec/
moritz authored
67
68 {
69 my %hash = (:a(1), :b(2), :c(3));
70 {
71 temp %hash<b> = 42;
72 is %hash<b>, 42, "temp() changed our hash element";
73 }
74 is %hash<b>, 2, "temp() restored our array element";
75 }
76
77 {
78 my $struct = [
79 "doesnt_matter",
80 {
81 doesnt_matter => "doesnt_matter",
82 key => [
83 "doesnt_matter",
84 42,
85 ],
86 },
87 ];
88
89 {
90 temp $struct[1]<key>[1] = 23;
91 is $struct[1]<key>[1], 23, "temp() changed our nested arrayref/hashref element";
92 }
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and oth...
sorear authored
93 is $struct[1]<key>[1], 42, "temp() restored our nested arrayref/hashref element";
ab5204d [t] moved tests for let and temp to spec/
moritz authored
94 }
95
96 # Block TEMP{}
97 # L<S06/Temporization/You can also modify the behaviour of temporized code structures>
98 # (Test is more or less directly from S06.)
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and oth...
sorear authored
99 #?niecza 2 skip 'spec clarification needed'
4614839 @jnthn Fudge temp.t for Rakudo; don't have TEMP phasers yet.
jnthn authored
100 #?rakudo 2 skip 'TEMP phasers NYI'
ab5204d [t] moved tests for let and temp to spec/
moritz authored
101 {
102 my $next = 0;
103
104 # Here is the real implementation of &advance.
105 sub advance() {
106 my $curr = $next++;
107 TEMP {{ $next = $curr }} # TEMP block returns the closure { $next = $curr }
108 return $curr;
109 };
110
111 # and later...
112
113 is advance(), 0, "TEMP{} block (1)";
114 is advance(), 1, "TEMP{} block (2)";
115 is advance(), 2, "TEMP{} block (3)";
116 is $next, 3, "TEMP{} block (4)";
117
8c5363f [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
118 #?pugs 4 todo 'feature'
119 flunk "TEMP{} block (5)";
120 flunk "TEMP{} block (6)";
121 flunk "TEMP{} block (7)";
122 flunk "TEMP{} block (8)";
ab5204d [t] moved tests for let and temp to spec/
moritz authored
123
124 # Following does parse, but isn't executed (don't know why).
125 # If the "{" on the following line is changed to "if 1 {", it is executed,
126 # too, but then it dies complaining about not finding a matching temp()
127 # function. So, for now, we just comment the following block and add
128 # unconditional flunk()s.
62fa996 [t/spec] embedded comments and unspaces now have a backtick
moritz authored
129 # {
8c5363f [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
130 # #?pugs 4 todo 'feature'
131 # is temp(advance()), 3, "TEMP{} block (5)";
132 # is $next, 4, "TEMP{} block (6)";
133 # is temp(advance()), 4, "TEMP{} block (7)";
134 # is temp(advance()), 5, "TEMP{} block (8)";
62fa996 [t/spec] embedded comments and unspaces now have a backtick
moritz authored
135 # } # $next = 3
ab5204d [t] moved tests for let and temp to spec/
moritz authored
136
137 is $next, 3, "TEMP{} block (9)";
138 is advance(), 3, "TEMP{} block (10)";
139 is $next, 4, "TEMP{} block (11)";
140 }
141
142 # Following are OO tests, but I think they fit better in var/temp.t than in
143 # oo/.
144 # L<S06/Temporization/temp invokes its argument's .TEMP method.>
145 {
146 my $was_in_own_temp_handler = 0;
147
148 class WierdTemp is Int {
149 method TEMP {
150 $was_in_own_temp_handler++;
151 return { $was_in_own_temp_handler++ };
152 }
153 }
154
155 my $a = WierdTemp.new();
156 ok defined($a), "instantiating a WierdTemp worked";
157 is $was_in_own_temp_handler, 0, ".TEMP method wasn't yet executed";
158
159 {
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and oth...
sorear authored
160 is((temp $a; $was_in_own_temp_handler), 1, ".TEMP method was executed on temporization");
ab5204d [t] moved tests for let and temp to spec/
moritz authored
161 }
162 is $was_in_own_temp_handler, 2, ".TEMP method was executed on restoration";
163 }
164
38cc5b7 @diakopter test recursive nested and multiple temps; rakudo will need these fudged ...
diakopter authored
165 {
166 my $depth = 0;
167 my $c = 1;
168 sub a {
169 ++temp $c;
170 a() if ++$depth < 3;
171 }
172 a();
173 is $c, 1, 'recursive nested temps are restored properly';
174 }
175
176 {
177 my $a=1;
178 {
179 temp $a=2;
180 temp $a=3;
181 }
182 is $a, 1, 'multiple temps in the same scope are restored properly';
183 }
ab5204d [t] moved tests for let and temp to spec/
moritz authored
184
58e9558 [t/spec] fix some problems found by STD.pm, TimToady++
moritz authored
185 # vim: ft=perl6
Something went wrong with that request. Please try again.