foo
[gitmo/Moose-Autobox.git] / t / 001_basic.t
CommitLineData
5f654d8e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 1;
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
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
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
128# Hash
129
130my $h = { one => 1, two => 2, three => 3 };
131
132ok($h->defined, '... got the right defined value');
133
134
135
136