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