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