Debian Codegraph @

t/Hash-StoredIterator.t

raw symbols SCIP
1 use Test2::Bundle::Extended -target => 'Hash::StoredIterator';
2 use Test2::Tools::Spec;
3
4 use Hash::StoredIterator ':all';
5
6 my %hash = ( a => 1, b => 2, c => 3 );
7
8 my @want_outer = (
9 [a => 1],
10 [b => 2],
11 [c => 3],
12 );
13
14 my @want_inner = (
15 [a => 1],
16 [a => 1],
17 [a => 1],
18 [b => 2],
19 [b => 2],
20 [b => 2],
21 [c => 3],
22 [c => 3],
23 [c => 3],
24 );
25
26 describe eaches => sub {
27 my $interference;
28
29 case no_interference => sub {
30 $interference = sub { 1 };
31 };
32
33 # In the interference case, this sub which uses keys, values, and each on
34 # our hash is called nested inside our custom calls, they should not
35 # interfere as they would with builtins.
36 case interference => sub {
37 $interference = sub {
38 my @garbage = keys(%hash), values(%hash);
39 while ( my ( $k, $v ) = each(%hash) ) {
40 # Effectively do nothing
41 my $foo = $k . $v;
42 }
43 };
44 };
45
46 tests nested_eich => sub {
47 my @inner;
48 my @outer;
49
50 warnings {
51 my $o_it;
52 while ( my ( $k, $v ) = eich( %hash, $o_it ) ) {
53 push @outer => [$k, $v];
54 $interference->();
55
56 my $i_it;
57 while ( my ( $k, $v ) = eich( %hash, $i_it ) ) {
58 push @inner => [$k, $v];
59
60 $interference->();
61 }
62 }
63 };
64
65 is(
66 [sort { $a->[0] cmp $b->[0] } @outer],
67 \@want_outer,
68 "Out loop got all keys"
69 );
70
71 is(
72 [sort { $a->[0] cmp $b->[0] } @inner],
73 \@want_inner,
74 "Inner loop got all keys multiple times"
75 );
76 };
77
78 tests nested_hmap => sub {
79 my @inner;
80 my @outer;
81
82 #<<< no-tidy Perltidy hates this...
83 hmap {
84 my ( $k, $v ) = @_;
85 ok( $k, "Got key" );
86 ok( $v, "Got val" );
87 is( $k, $_, '$_ is set to key' );
88 is( $k, $a, '$a is set to key' );
89 is( $v, $b, '$b is set to val' );
90
91 push @outer => [$k, $v];
92 $interference->();
93
94 hmap {
95 my ( $k2, $v2 ) = @_;
96
97 is( $k2, $_, '$_ is set to key' );
98 is( $k2, $a, '$a is set to key' );
99 is( $v2, $b, '$b is set to val' );
100
101 push @inner => [$k, $v];
102
103 $interference->();
104 } %hash;
105
106 is( $k, $_, '$_ is not squashed by inner loop' );
107 is( $k, $a, '$a is not squashed by inner loop' );
108 is( $v, $b, '$a is not squashed by inner loop' );
109 } %hash;
110 #>>>
111
112 is(
113 [sort { $a->[0] cmp $b->[0] } @outer],
114 \@want_outer,
115 "Outer loop got all keys"
116 );
117
118 is(
119 [sort { $a->[0] cmp $b->[0] } @inner],
120 \@want_inner,
121 "Inner loop got all keys multiple times"
122 );
123 };
124 };
125
126 tests get_from_eich => sub {
127 my $i;
128 my $h = {%hash};
129
130 warnings {
131 my ( $k, $v ) = eich( %$h, $i );
132 ok( $k, "Got a key" );
133 ok( $v, "got a value" );
134 };
135 };
136
137 tests keys_and_vals => sub {
138 is(
139 [sort( hkeys(%hash) )],
140 [sort keys %hash],
141 "Same list from both keys and hkeys"
142 );
143
144 is(
145 [sort( hvalues(%hash) )],
146 [sort values %hash],
147 "Same list from both values and hvalues"
148 );
149 };
150
151 tests death => sub {
152 my $counter = 0;
153
154 my %hash = map { $_ => $_ } 'a' .. 'z';
155
156 # Set an initial iterator
157 my ($garbage) = each(%hash);
158
159 #<<< no-tidy Perltidy hates this...
160 eval {
161 hmap {
162 my ( $k, $v ) = @_;
163 die "foo" if $counter++ > 3;
164 } %hash;
165 };
166 #>>>
167
168 like( $@, qr/foo/, "Got error" );
169
170 # Get key at current iteration, it should match the second key
171 my ($key) = each(%hash);
172 my ( $fkey, $skey ) = keys(%hash);
173 is( $garbage, $fkey, "First key matches initial garbage key" );
174 is( $key, $skey, "Iterator was restored" );
175 };
176
177 tests strange_edge_case => { todo => "Not sure what the problem is here..." } => sub {
178 is(
179 [sort hkeys %hash],
180 [sort keys %hash],
181 "Same list from both keys and hkeys"
182 );
183
184 is(
185 [sort hkeys(%hash)],
186 [sort keys %hash],
187 "Same list from both keys and hkeys"
188 );
189 };
190
191 tests iterator => sub {
192 my $i = iterator %hash;
193
194 my $nh = {
195 $i->(),
196 $i->(),
197 $i->(),
198 };
199
200 is(
201 $nh,
202 \%hash,
203 "Copied hash via iterator"
204 );
205
206 is(
207 [$i->()],
208 [],
209 "End, no more"
210 );
211
212 my ($k, $v) = $i->();
213 ok( $k && $v, "Got a key and value, iterator was reset" );
214 };
215
216 done_testing;