autoboxing
[gitmo/Moose-Autobox.git] / t / 001_basic.t
CommitLineData
5f654d8e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
252ab1a2 6use Test::More tests => 44;
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');
45ok(!(sub { 1 })->conjoin(sub { 0 })->(), '... conjoins properly');
46
252ab1a2 47my $compose = (sub { @_, 1 })->compose(sub { @_, 2 });
5f654d8e 48
252ab1a2 49is_deeply(
50[ $compose->() ],
51[ 1, 2 ],
52'... got the right return value for compose');
5f654d8e 53
54
55# ARRAY
56
57my $a = [ 4, 2, 6, 78, 101, 2, 3 ];
58
59is($a->length, 7, '... got the right length');
60ok($a->defined, '... got the right defined value');
61
62is_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
67is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
68
69is_deeply(
70$a->reverse(),
71[ 3, 2, 101, 78, 6, 2, 4 ],
72'... got the right return value for reverse');
73
74is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
75
76is_deeply(
77$a->grep(sub { $_ < 50 }),
78[ grep { $_ < 50 } (4, 2, 6, 78, 101, 2, 3) ],
79'... got the right return value grep');
80
81is_deeply($a, [ 4, 2, 6, 78, 101, 2, 3 ], '... original value is unchanged');
82
83is($a->join(', '), '4, 2, 6, 78, 101, 2, 3', '... got the right joined string');
84ok($a->exists(0), '... exists works');
85ok(!$a->exists(10), '... exists works');
86
87is($a->pop(), 3, '... got the right pop-ed value');
88is_deeply($a, [ 4, 2, 6, 78, 101, 2 ], '... original value is now changed');
89
90is($a->shift(), 4, '... got the right unshift-ed value');
91is_deeply($a, [ 2, 6, 78, 101, 2 ], '... original value is now changed');
92
93is_deeply(
94$a->unshift(10),
95[ 10, 2, 6, 78, 101, 2 ],
96'... got the correct unshifted value');
97
98is_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
103is_deeply(
104$a->push(10),
105[ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10 ],
106'... got the correct pushed value');
107
108is_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
113is_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
118is_deeply(
119$a,
120[ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ],
121'... the original values are unchanged');
122
123is_deeply(
124[]->push(10, 20, 30)->map(sub { ($_, $_ + 5) })->reverse,
125[ 35, 30, 25, 20, 15, 10 ],
126'... got the correct chained value');
127
5dc78481 128is_deeply(
129$a->keys,
130[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ],
131'... the keys');
132
133is_deeply(
134$a->values,
135[ 15, 20, 30, 10, 2, 6, 78, 101, 2, 10, 15, 20, 30 ],
136'... the values');
137
138is_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
144is([1, 2, 3, 4, 5]->reduce(sub { $_[0] + $_[1] }), 15, '... got the right reduction');
145
146is_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
5f654d8e 152# Hash
153
154my $h = { one => 1, two => 2, three => 3 };
155
156ok($h->defined, '... got the right defined value');
157
5dc78481 158is_deeply(
159$h->keys->sort,
160[ qw/one three two/ ],
161'... the keys');
162
163is_deeply(
164$h->values->sort,
165[ 1, 2, 3 ],
166'... the values');
5f654d8e 167
5dc78481 168ok($h->exists('two'), '... exists works');
169ok(!$h->exists('five'), '... !exists works');