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