foo
[gitmo/Moose-Autobox.git] / t / 001_basic.t
CommitLineData
5f654d8e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
260cc81f 6use Test::More tests => 55;
5f654d8e 7
8BEGIN {
9 use_ok('Moose::Autobox');
10 use_ok('Moose::Autobox::Undef');
11}
12
13use autobox UNDEF => 'Moose::Autobox::Undef';
14
15# SCALAR & UNDEF
16
17my $s;
18ok(!$s->defined, '... got a undefined value');
19
20$s = 5;
21ok($s->defined, '... got a defined value');
22
23# CODE
24
25my $f1 = sub { @_ };
26ok($f1->defined, '... created function');
27
28my $f2 = eval { $f1->curry(1, 2, 3) };
29ok($f2->defined, '... created curried function');
30
31my $f3 = eval { $f1->rcurry(1, 2, 3) };
32ok($f3->defined, '... created right-curried function');
33
34is_deeply(
35[ $f2->(4, 5, 6) ],
36[ 1, 2, 3, 4, 5, 6 ],
37'... got the right return value from the curried function');
38
39is_deeply(
40[ $f3->(4, 5, 6) ],
41[ 4, 5, 6, 1, 2, 3 ],
42'... got the right return value from the r-curried function');
43
44ok((sub { 1 })->disjoin(sub { 0 })->(), '... disjoins properly');
260cc81f 45ok((sub { 0 })->disjoin(sub { 1 })->(), '... disjoins properly');
46
5f654d8e 47ok(!(sub { 1 })->conjoin(sub { 0 })->(), '... conjoins properly');
260cc81f 48ok(!(sub { 0 })->conjoin(sub { 1 })->(), '... conjoins properly');
5f654d8e 49
252ab1a2 50my $compose = (sub { @_, 1 })->compose(sub { @_, 2 });
5f654d8e 51
252ab1a2 52is_deeply(
53[ $compose->() ],
54[ 1, 2 ],
55'... got the right return value for compose');
260cc81f 56
5f654d8e 57# ARRAY
58
59my $a = [ 4, 2, 6, 78, 101, 2, 3 ];
60
61is($a->length, 7, '... got the right length');
62ok($a->defined, '... got the right defined value');
63
64is_deeply(
65$a->map(sub { $_ + 2 }),
66[ map { $_ + 2 } (4, 2, 6, 78, 101, 2, 3) ],
67'... got the right return value for map');
68
69is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
70
71is_deeply(
72$a->reverse(),
73[ 3, 2, 101, 78, 6, 2, 4 ],
74'... got the right return value for reverse');
75
76is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
77
78is_deeply(
79$a->grep(sub { $_ < 50 }),
80[ grep { $_ < 50 } (4, 2, 6, 78, 101, 2, 3) ],
81'... got the right return value grep');
82
83is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
84
85is($a->join(', '), '4, 2, 6, 78, 101, 2, 3', '... got the right joined string');
260cc81f 86is($a->join, '4267810123', '... got the right joined string');
5f654d8e 87ok($a->exists(0), '... exists works');
88ok(!$a->exists(10), '... exists works');
89
90is($a->pop(), 3, '... got the right pop-ed value');
91is_deeply($a, [ 4, 2, 6, 78, 101, 2 ], '... original value is now changed');
92
93is($a->shift(), 4, '... got the right unshift-ed value');
94is_deeply($a, [ 2, 6, 78, 101, 2 ], '... original value is now changed');
95
96is_deeply(
97$a->unshift(10),
98[ 10, 2, 6, 78, 101, 2 ],
99'... got the correct unshifted value');
100
101is_deeply(
102$a->unshift(15, 20, 30),
103[ 15, 20, 30, 10, 2, 6, 78, 101, 2 ],
104'... got the correct unshifted value (multiple values)');
105
106is_deeply(
107$a->push(10),
108[ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10 ],
109'... got the correct pushed value');
110
111is_deeply(
112$a->push(15, 20, 30),
113[ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ],
114'... got the correct pushed value (multiple values)');
115
116is_deeply(
117$a->sort(sub { $_[0] <=> $_[1] }),
118[ 2, 2, 6, 10, 10, 15, 15, 20, 20, 30, 30, 78, 101 ],
119'... got the correct sorted value');
120
121is_deeply(
122$a,
123[ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ],
124'... the original values are unchanged');
125
126is_deeply(
127[]->push(10, 20, 30)->map(sub { ($_, $_ + 5) })->reverse,
128[ 35, 30, 25, 20, 15, 10 ],
129'... got the correct chained value');
130
5dc78481 131is_deeply(
132$a->keys,
133[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ],
134'... the keys');
135
136is_deeply(
137$a->values,
138[ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ],
139'... the values');
140
141is_deeply(
142$a->kv,
143[ [0, 15], [1, 20], [2, 30], [3, 10], [4, 2], [5, 6], [6, 78],
144 [7, 101], [8, 2], [9, 10], [10, 15], [11, 20], [12, 30] ],
145'... [ k, v ]');
146
147is([1, 2, 3, 4, 5]->reduce(sub { $_[0] + $_[1] }), 15, '... got the right reduction');
148
149is_deeply(
260cc81f 150[1, 2, 3, 4, 5]->zip([ 5, 4, 3, 2, 1 ]),
151[ [1, 5], [2, 4], [3, 3], [4, 2], [5, 1] ],
152'... got the right zip');
5dc78481 153
260cc81f 154is_deeply(
155[1, 2, 3, 4, 5]->zip([ 6, 5, 4, 3, 2, 1 ]),
156[ [1, 6], [2, 5], [3, 4], [4, 3], [5, 2], [undef, 1] ],
157'... got the right zip');
158
159is($a->delete(2), 30, '... got the value deleted');
160is_deeply(
161$a,
162[ 15, 20, undef, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ],
163'... the value is correctly deleted');
164
165$a->put(2, 30);
166
167is_deeply(
168$a,
169[ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ],
170'... the value is correctly put');
5dc78481 171
5f654d8e 172# Hash
173
174my $h = { one => 1, two => 2, three => 3 };
175
176ok($h->defined, '... got the right defined value');
177
5dc78481 178is_deeply(
179$h->keys->sort,
180[ qw/one three two/ ],
181'... the keys');
182
183is_deeply(
184$h->values->sort,
185[ 1, 2, 3 ],
186'... the values');
5f654d8e 187
260cc81f 188is_deeply(
189$h->kv->sort(sub { $_[0]->[1] <=> $_[1]->[1] }),
190[ ['one', 1], ['two', 2], ['three', 3] ],
191'... the kvs');
192
5dc78481 193ok($h->exists('two'), '... exists works');
194ok(!$h->exists('five'), '... !exists works');
260cc81f 195
196$h->put('four' => 4);
197is_deeply(
198$h,
199{ one => 1, two => 2, three => 3, four => 4 },
200'... got the value added correctly');
201
202is($h->at('four'), 4, '... got the value at "four"');
203
204$h->delete('four');
205is_deeply(
206$h,
207{ one => 1, two => 2, three => 3 },
208'... got the value deleted correctly');
209
210