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