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