Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 167 lines (142 sloc) 4.323 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 …
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 …
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 …
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 …
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 …
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 …
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 …
sorear authored
99 #?niecza 2 skip 'spec clarification needed'
ab5204d [t] moved tests for let and temp to spec/
moritz authored
100 {
101 my $next = 0;
102
103 # Here is the real implementation of &advance.
104 sub advance() {
105 my $curr = $next++;
106 TEMP {{ $next = $curr }} # TEMP block returns the closure { $next = $curr }
107 return $curr;
108 };
109
110 # and later...
111
112 is advance(), 0, "TEMP{} block (1)";
113 is advance(), 1, "TEMP{} block (2)";
114 is advance(), 2, "TEMP{} block (3)";
115 is $next, 3, "TEMP{} block (4)";
116
8c5363f [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
117 #?pugs 4 todo 'feature'
118 flunk "TEMP{} block (5)";
119 flunk "TEMP{} block (6)";
120 flunk "TEMP{} block (7)";
121 flunk "TEMP{} block (8)";
ab5204d [t] moved tests for let and temp to spec/
moritz authored
122
123 # Following does parse, but isn't executed (don't know why).
124 # If the "{" on the following line is changed to "if 1 {", it is executed,
125 # too, but then it dies complaining about not finding a matching temp()
126 # function. So, for now, we just comment the following block and add
127 # unconditional flunk()s.
62fa996 [t/spec] embedded comments and unspaces now have a backtick
moritz authored
128 # {
8c5363f [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
129 # #?pugs 4 todo 'feature'
130 # is temp(advance()), 3, "TEMP{} block (5)";
131 # is $next, 4, "TEMP{} block (6)";
132 # is temp(advance()), 4, "TEMP{} block (7)";
133 # is temp(advance()), 5, "TEMP{} block (8)";
62fa996 [t/spec] embedded comments and unspaces now have a backtick
moritz authored
134 # } # $next = 3
ab5204d [t] moved tests for let and temp to spec/
moritz authored
135
136 is $next, 3, "TEMP{} block (9)";
137 is advance(), 3, "TEMP{} block (10)";
138 is $next, 4, "TEMP{} block (11)";
139 }
140
141 # Following are OO tests, but I think they fit better in var/temp.t than in
142 # oo/.
143 # L<S06/Temporization/temp invokes its argument's .TEMP method.>
0573f18 @sorear [S04-blocks_and_statements/{let,temp}.t] Remove ill-placed evals and …
sorear authored
144 #?niecza 2 skip 'needs clarification on correct behavior'
ab5204d [t] moved tests for let and temp to spec/
moritz authored
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 …
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
165
58e9558 [t/spec] fix some problems found by STD.pm, TimToady++
moritz authored
166 # vim: ft=perl6
Something went wrong with that request. Please try again.