foo
[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 => 44;
7
8 BEGIN {
9     use_ok('Moose::Autobox');
10     use_ok('Moose::Autobox::Undef');
11 }
12
13 use autobox UNDEF => 'Moose::Autobox::Undef';
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');
45 ok(!(sub { 1 })->conjoin(sub { 0 })->(), '... conjoins properly');
46
47 my $compose = (sub { @_, 1 })->compose(sub { @_, 2 });
48
49 is_deeply(
50 [ $compose->() ],
51 [ 1, 2 ],
52 '... got the right return value for compose');
53
54     
55 # ARRAY    
56     
57 my $a = [ 4, 2, 6, 78, 101, 2, 3 ];
58
59 is($a->length, 7, '... got the right length');
60 ok($a->defined, '... got the right defined value');
61
62 is_deeply(
63 $a->map(sub { $_ + 2 }),
64 [ map { $_ + 2 } (4, 2, 6, 78, 101, 2, 3) ],
65 '... got the right return value for map');
66
67 is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
68
69 is_deeply(
70 $a->reverse(),
71 [ 3, 2, 101, 78, 6, 2, 4 ],
72 '... got the right return value for reverse');
73
74 is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
75
76 is_deeply(
77 $a->grep(sub { $_ < 50 }),
78 [ grep { $_ < 50 } (4, 2, 6, 78, 101, 2, 3) ],
79 '... got the right return value grep');
80
81 is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
82
83 is($a->join(', '), '4, 2, 6, 78, 101, 2, 3', '... got the right joined string');
84 ok($a->exists(0), '... exists works');
85 ok(!$a->exists(10), '... exists works');
86
87 is($a->pop(), 3, '... got the right pop-ed value');
88 is_deeply($a, [ 4, 2, 6, 78, 101, 2 ], '... original value is now changed');
89
90 is($a->shift(), 4, '... got the right unshift-ed value');
91 is_deeply($a, [ 2, 6, 78, 101, 2 ], '... original value is now changed');
92
93 is_deeply(
94 $a->unshift(10), 
95 [ 10, 2, 6, 78, 101, 2 ], 
96 '... got the correct unshifted value');
97
98 is_deeply(
99 $a->unshift(15, 20, 30), 
100 [ 15, 20, 30, 10, 2, 6, 78, 101, 2 ], 
101 '... got the correct unshifted value (multiple values)');
102
103 is_deeply(
104 $a->push(10), 
105 [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10 ], 
106 '... got the correct pushed value');
107
108 is_deeply(
109 $a->push(15, 20, 30), 
110 [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], 
111 '... got the correct pushed value (multiple values)');
112
113 is_deeply(
114 $a->sort(sub { $_[0] <=> $_[1] }), 
115 [ 2, 2, 6, 10, 10, 15, 15, 20, 20, 30, 30, 78, 101 ],
116 '... got the correct sorted value');
117
118 is_deeply(
119 $a, 
120 [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], 
121 '... the original values are unchanged');
122
123 is_deeply(
124 []->push(10, 20, 30)->map(sub { ($_, $_ + 5) })->reverse, 
125 [ 35, 30, 25, 20, 15, 10 ], 
126 '... got the correct chained value');
127
128 is_deeply(
129 $a->keys, 
130 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ],
131 '... the keys');
132
133 is_deeply(
134 $a->values, 
135 [ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ], 
136 '... the values');
137
138 is_deeply(
139 $a->kv, 
140 [ [0, 15], [1, 20], [2, 30], [3, 10], [4, 2], [5, 6], [6, 78], 
141   [7, 101], [8, 2], [9, 10], [10, 15], [11, 20], [12, 30] ], 
142 '... [ k, v ]');
143
144 is([1, 2, 3, 4, 5]->reduce(sub { $_[0] + $_[1] }), 15, '... got the right reduction');
145
146 is_deeply(
147     [1, 2, 3, 4, 5]->zip([ 5, 4, 3, 2, 1 ]), 
148     [ [1, 5], [2, 4], [3, 3], [4, 2], [5, 1] ],
149     '... got the right zip');
150
151
152 # Hash
153
154 my $h = { one => 1, two => 2, three => 3 };
155
156 ok($h->defined, '... got the right defined value');
157
158 is_deeply(
159 $h->keys->sort, 
160 [ qw/one three two/ ],
161 '... the keys');
162
163 is_deeply(
164 $h->values->sort, 
165 [ 1, 2, 3 ],
166 '... the values');
167
168 ok($h->exists('two'), '... exists works');
169 ok(!$h->exists('five'), '... !exists works');
170
171