Commit | Line | Data |
5f654d8e |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
aaf111de |
6 | use Test::More tests => 68; |
5f654d8e |
7 | |
8 | BEGIN { |
9 | use_ok('Moose::Autobox'); |
5f654d8e |
10 | } |
11 | |
7dad2765 |
12 | use Moose::Autobox; |
5f654d8e |
13 | |
3f4dd8b7 |
14 | my $VAR1; # for eval of dumps |
15 | |
5f654d8e |
16 | # SCALAR & UNDEF |
17 | |
18 | my $s; |
19 | ok(!$s->defined, '... got a undefined value'); |
20 | |
21 | $s = 5; |
22 | ok($s->defined, '... got a defined value'); |
23 | |
3f4dd8b7 |
24 | eval $s->dump; |
25 | is($VAR1, 5 , '... eval of SCALAR->dump works'); |
26 | |
27 | eval $s->perl; |
28 | is($s->perl, $s->dump, '... SCALAR->dump equals SCALAR->perl'); |
29 | |
5f654d8e |
30 | # CODE |
31 | |
32 | my $f1 = sub { @_ }; |
33 | ok($f1->defined, '... created function'); |
34 | |
35 | my $f2 = eval { $f1->curry(1, 2, 3) }; |
36 | ok($f2->defined, '... created curried function'); |
37 | |
38 | my $f3 = eval { $f1->rcurry(1, 2, 3) }; |
39 | ok($f3->defined, '... created right-curried function'); |
40 | |
41 | is_deeply( |
42 | [ $f2->(4, 5, 6) ], |
43 | [ 1, 2, 3, 4, 5, 6 ], |
44 | '... got the right return value from the curried function'); |
45 | |
46 | is_deeply( |
47 | [ $f3->(4, 5, 6) ], |
48 | [ 4, 5, 6, 1, 2, 3 ], |
49 | '... got the right return value from the r-curried function'); |
50 | |
51 | ok((sub { 1 })->disjoin(sub { 0 })->(), '... disjoins properly'); |
260cc81f |
52 | ok((sub { 0 })->disjoin(sub { 1 })->(), '... disjoins properly'); |
53 | |
5f654d8e |
54 | ok(!(sub { 1 })->conjoin(sub { 0 })->(), '... conjoins properly'); |
260cc81f |
55 | ok(!(sub { 0 })->conjoin(sub { 1 })->(), '... conjoins properly'); |
5f654d8e |
56 | |
252ab1a2 |
57 | my $compose = (sub { @_, 1 })->compose(sub { @_, 2 }); |
5f654d8e |
58 | |
252ab1a2 |
59 | is_deeply( |
60 | [ $compose->() ], |
61 | [ 1, 2 ], |
62 | '... got the right return value for compose'); |
260cc81f |
63 | |
5f654d8e |
64 | # ARRAY |
65 | |
66 | my $a = [ 4, 2, 6, 78, 101, 2, 3 ]; |
67 | |
68 | is($a->length, 7, '... got the right length'); |
69 | ok($a->defined, '... got the right defined value'); |
70 | |
71 | is_deeply( |
72 | $a->map(sub { $_ + 2 }), |
73 | [ map { $_ + 2 } (4, 2, 6, 78, 101, 2, 3) ], |
74 | '... got the right return value for map'); |
75 | |
76 | is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged'); |
77 | |
78 | is_deeply( |
79 | $a->reverse(), |
80 | [ 3, 2, 101, 78, 6, 2, 4 ], |
81 | '... got the right return value for reverse'); |
82 | |
83 | is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged'); |
84 | |
85 | is_deeply( |
86 | $a->grep(sub { $_ < 50 }), |
87 | [ grep { $_ < 50 } (4, 2, 6, 78, 101, 2, 3) ], |
88 | '... got the right return value grep'); |
89 | |
90 | is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged'); |
91 | |
92 | is($a->join(', '), '4, 2, 6, 78, 101, 2, 3', '... got the right joined string'); |
260cc81f |
93 | is($a->join, '4267810123', '... got the right joined string'); |
5f654d8e |
94 | ok($a->exists(0), '... exists works'); |
95 | ok(!$a->exists(10), '... exists works'); |
96 | |
97 | is($a->pop(), 3, '... got the right pop-ed value'); |
98 | is_deeply($a, [ 4, 2, 6, 78, 101, 2 ], '... original value is now changed'); |
99 | |
100 | is($a->shift(), 4, '... got the right unshift-ed value'); |
101 | is_deeply($a, [ 2, 6, 78, 101, 2 ], '... original value is now changed'); |
102 | |
c11e6a74 |
103 | |
104 | is_deeply( |
105 | $a->slice([ 1, 2, 4 ]), |
106 | [ 6, 78, 2 ], |
107 | '... got the right sliced value'); |
108 | |
5f654d8e |
109 | is_deeply( |
110 | $a->unshift(10), |
111 | [ 10, 2, 6, 78, 101, 2 ], |
112 | '... got the correct unshifted value'); |
113 | |
114 | is_deeply( |
115 | $a->unshift(15, 20, 30), |
116 | [ 15, 20, 30, 10, 2, 6, 78, 101, 2 ], |
117 | '... got the correct unshifted value (multiple values)'); |
118 | |
119 | is_deeply( |
120 | $a->push(10), |
121 | [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10 ], |
122 | '... got the correct pushed value'); |
123 | |
124 | is_deeply( |
125 | $a->push(15, 20, 30), |
126 | [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], |
127 | '... got the correct pushed value (multiple values)'); |
128 | |
129 | is_deeply( |
130 | $a->sort(sub { $_[0] <=> $_[1] }), |
131 | [ 2, 2, 6, 10, 10, 15, 15, 20, 20, 30, 30, 78, 101 ], |
132 | '... got the correct sorted value'); |
133 | |
134 | is_deeply( |
135 | $a, |
136 | [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], |
137 | '... the original values are unchanged'); |
138 | |
139 | is_deeply( |
140 | []->push(10, 20, 30)->map(sub { ($_, $_ + 5) })->reverse, |
141 | [ 35, 30, 25, 20, 15, 10 ], |
142 | '... got the correct chained value'); |
143 | |
5dc78481 |
144 | is_deeply( |
145 | $a->keys, |
146 | [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ], |
147 | '... the keys'); |
148 | |
149 | is_deeply( |
150 | $a->values, |
151 | [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], |
152 | '... the values'); |
153 | |
154 | is_deeply( |
155 | $a->kv, |
156 | [ [0, 15], [1, 20], [2, 30], [3, 10], [4, 2], [5, 6], [6, 78], |
157 | [7, 101], [8, 2], [9, 10], [10, 15], [11, 20], [12, 30] ], |
158 | '... [ k, v ]'); |
159 | |
160 | is([1, 2, 3, 4, 5]->reduce(sub { $_[0] + $_[1] }), 15, '... got the right reduction'); |
161 | |
162 | is_deeply( |
260cc81f |
163 | [1, 2, 3, 4, 5]->zip([ 5, 4, 3, 2, 1 ]), |
164 | [ [1, 5], [2, 4], [3, 3], [4, 2], [5, 1] ], |
165 | '... got the right zip'); |
5dc78481 |
166 | |
260cc81f |
167 | is_deeply( |
168 | [1, 2, 3, 4, 5]->zip([ 6, 5, 4, 3, 2, 1 ]), |
169 | [ [1, 6], [2, 5], [3, 4], [4, 3], [5, 2], [undef, 1] ], |
170 | '... got the right zip'); |
171 | |
172 | is($a->delete(2), 30, '... got the value deleted'); |
173 | is_deeply( |
174 | $a, |
175 | [ 15, 20, undef, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], |
176 | '... the value is correctly deleted'); |
177 | |
178 | $a->put(2, 30); |
179 | |
180 | is_deeply( |
181 | $a, |
182 | [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], |
183 | '... the value is correctly put'); |
5dc78481 |
184 | |
3f4dd8b7 |
185 | eval($a->dump); |
186 | is_deeply( $VAR1, |
187 | [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], |
188 | '... the value is correctly dumped'); |
9b2ca73c |
189 | |
9b2ca73c |
190 | is( $a->dump, |
191 | $a->perl, |
192 | '... the value is correctly dumped with perl()' ); |
193 | |
aaf111de |
194 | is([1, 2, 3, 4, 5]->any, 3, '... any correctly found a value'); |
195 | is([1, 1, 2, 3, 4, 5, 5]->one, 2, '... one correctly found one value'); |
196 | is([1, 1, 2, 3, 4, 5, 5]->none , 6, '... none correctly did not find any of a value'); |
197 | is([3, 3, 3]->all, 3, '... all correctly found all of a value'); |
9b2ca73c |
198 | |
5f654d8e |
199 | # Hash |
200 | |
201 | my $h = { one => 1, two => 2, three => 3 }; |
202 | |
203 | ok($h->defined, '... got the right defined value'); |
204 | |
5dc78481 |
205 | is_deeply( |
206 | $h->keys->sort, |
207 | [ qw/one three two/ ], |
208 | '... the keys'); |
209 | |
210 | is_deeply( |
211 | $h->values->sort, |
212 | [ 1, 2, 3 ], |
213 | '... the values'); |
5f654d8e |
214 | |
260cc81f |
215 | is_deeply( |
216 | $h->kv->sort(sub { $_[0]->[1] <=> $_[1]->[1] }), |
217 | [ ['one', 1], ['two', 2], ['three', 3] ], |
218 | '... the kvs'); |
219 | |
5dc78481 |
220 | ok($h->exists('two'), '... exists works'); |
221 | ok(!$h->exists('five'), '... !exists works'); |
260cc81f |
222 | |
223 | $h->put('four' => 4); |
224 | is_deeply( |
225 | $h, |
226 | { one => 1, two => 2, three => 3, four => 4 }, |
227 | '... got the value added correctly'); |
228 | |
229 | is($h->at('four'), 4, '... got the value at "four"'); |
230 | |
231 | $h->delete('four'); |
232 | is_deeply( |
233 | $h, |
234 | { one => 1, two => 2, three => 3 }, |
235 | '... got the value deleted correctly'); |
236 | |
bc9177cb |
237 | is_deeply( |
238 | $h->merge({ three => 33, four => 44 }), |
239 | { one => 1, two => 2, three => 33, four => 44 }, |
240 | '... got the hashes merged correctly'); |
241 | |
3f4dd8b7 |
242 | eval($h->dump); |
243 | is_deeply( $VAR1, |
244 | { one => 1, two => 2, three => 3 }, |
245 | '... the value is correctly dumped'); |
246 | |
247 | is( $h->dump, |
248 | $h->perl, |
249 | '... the value is correctly dumped with perl()' ); |
260cc81f |
250 | |
18ef6e1c |
251 | is_deeply( { one => 1, two => 2, three => 3 }->slice([qw/three one/]), |
252 | [ qw/3 1/ ], |
253 | '... hash slices ok' ); |
f7ea23f6 |
254 | |
255 | is_deeply( { one => 1, two => 2, three => 3 }->hslice([qw/two three/]), |
256 | { two => 2, three => 3 }, |
257 | '... hash hslices ok' ); |