Make coderef for sort_in_place optional as well.
[gitmo/MooseX-AttributeHelpers.git] / t / 002_basic_array.t
CommitLineData
22d869ff 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
71703b28 6use Test::More tests => 62;
69dde336 7use Test::Exception;
22d869ff 8
9BEGIN {
10 use_ok('MooseX::AttributeHelpers');
11}
12
13{
14 package Stuff;
15 use Moose;
16
17 has 'options' => (
d26633fc 18 metaclass => 'Collection::Array',
22d869ff 19 is => 'ro',
c43a2317 20 isa => 'ArrayRef[Str]',
22d869ff 21 default => sub { [] },
71703b28 22 provides => {
23 'push' => 'add_options',
24 'pop' => 'remove_last_option',
25 'shift' => 'remove_first_option',
26 'unshift' => 'insert_options',
27 'get' => 'get_option_at',
28 'set' => 'set_option_at',
29 'count' => 'num_options',
30 'empty' => 'has_options',
31 'clear' => 'clear_options',
32 'sort_in_place' => 'sort_options_in_place',
33 },
c43a2317 34 curries => {
3656a0d7 35 'push' => {
36 add_options_with_speed => ['funrolls', 'funbuns']
37 },
38 'unshift' => {
39 prepend_prerequisites_along_with => ['first', 'second']
80894c0a 40 },
71703b28 41 'sort_in_place' => { descending_options => [ sub { $_[1] <=> $_[0] } ],
80894c0a 42 },
22d869ff 43 }
44 );
45}
46
77d02b8b 47my $stuff = Stuff->new(options => [ 10, 12 ]);
22d869ff 48isa_ok($stuff, 'Stuff');
49
8c651099 50can_ok($stuff, $_) for qw[
51 add_options
52 remove_last_option
53 remove_first_option
54 insert_options
55 get_option_at
56 set_option_at
57 num_options
8cf40f80 58 clear_options
8c651099 59 has_options
71703b28 60 sort_options_in_place
8c651099 61];
62
77d02b8b 63is_deeply($stuff->options, [10, 12], '... got options');
64
65ok($stuff->has_options, '... we have options');
66is($stuff->num_options, 2, '... got 2 options');
67
68is($stuff->remove_last_option, 12, '... removed the last option');
69is($stuff->remove_first_option, 10, '... removed the last option');
70
71is_deeply($stuff->options, [], '... no options anymore');
22d869ff 72
8c651099 73ok(!$stuff->has_options, '... no options');
74is($stuff->num_options, 0, '... got no options');
75
69dde336 76lives_ok {
77 $stuff->add_options(1, 2, 3);
78} '... set the option okay';
79
22d869ff 80is_deeply($stuff->options, [1, 2, 3], '... got options now');
81
8c651099 82ok($stuff->has_options, '... no options');
83is($stuff->num_options, 3, '... got 3 options');
84
85is($stuff->get_option_at(0), 1, '... get option at index 0');
86is($stuff->get_option_at(1), 2, '... get option at index 1');
87is($stuff->get_option_at(2), 3, '... get option at index 2');
88
69dde336 89lives_ok {
90 $stuff->set_option_at(1, 100);
91} '... set the option okay';
8c651099 92
93is($stuff->get_option_at(1), 100, '... get option at index 1');
94
69dde336 95lives_ok {
96 $stuff->add_options(10, 15);
97} '... set the option okay';
98
8c651099 99is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
100
101is($stuff->num_options, 5, '... got 5 options');
22d869ff 102
103is($stuff->remove_last_option, 15, '... removed the last option');
104
8c651099 105is($stuff->num_options, 4, '... got 4 options');
106is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
107
69dde336 108lives_ok {
109 $stuff->insert_options(10, 20);
110} '... set the option okay';
8c651099 111
112is($stuff->num_options, 6, '... got 6 options');
113is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
114
115is($stuff->get_option_at(0), 10, '... get option at index 0');
116is($stuff->get_option_at(1), 20, '... get option at index 1');
117is($stuff->get_option_at(3), 100, '... get option at index 3');
118
119is($stuff->remove_first_option, 10, '... getting the first option');
120
121is($stuff->num_options, 5, '... got 5 options');
122is($stuff->get_option_at(0), 20, '... get option at index 0');
123
8cf40f80 124$stuff->clear_options;
125is_deeply( $stuff->options, [], "... clear options" );
126
71703b28 127$stuff->add_options(5, 1, 2, 3);
128$stuff->sort_options_in_place;
129is_deeply( $stuff->options, [1, 2, 3, 5], "... sort options in place (default sort order)" );
80894c0a 130
71703b28 131$stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
132is_deeply( $stuff->options, [5, 3, 2, 1], "... sort options in place (descending order)" );
133
134$stuff->clear_options();
135$stuff->add_options(5, 1, 2, 3);
136lives_ok {
137 $stuff->descending_options();
138} '... curried sort in place lives ok';
139
140is_deeply( $stuff->options, [5, 3, 2, 1], "... sort currying" );
80894c0a 141
71703b28 142throws_ok { $stuff->sort_options_in_place('foo') } qr/Argument must be a code reference/,
143 'error when sort_in_place receives a non-coderef argument';
80894c0a 144
145$stuff->clear_options;
146
c43a2317 147lives_ok {
148 $stuff->add_options('tree');
149} '... set the options okay';
69dde336 150
c43a2317 151lives_ok {
152 $stuff->add_options_with_speed('compatible', 'safe');
153} '... add options with speed okay';
69dde336 154
c43a2317 155is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/]);
69dde336 156
c43a2317 157lives_ok {
158 $stuff->prepend_prerequisites_along_with();
159} '... add prerequisite options okay';
160
161## check some errors
162
163#dies_ok {
164# $stuff->insert_options(undef);
165#} '... could not add an undef where a string is expected';
166#
167#dies_ok {
168# $stuff->set_option(5, {});
169#} '... could not add a hash ref where a string is expected';
69dde336 170
77d02b8b 171dies_ok {
c43a2317 172 Stuff->new(options => [ undef, 10, undef, 20 ]);
77d02b8b 173} '... bad constructor params';
174
5b380a52 175dies_ok {
176 my $stuff = Stuff->new();
177 $stuff->add_options(undef);
178} '... rejects push of an invalid type';
179
180dies_ok {
181 my $stuff = Stuff->new();
182 $stuff->insert_options(undef);
183} '... rejects unshift of an invalid type';
184
185dies_ok {
186 my $stuff = Stuff->new();
187 $stuff->set_option_at( 0, undef );
188} '... rejects set of an invalid type';
189
80894c0a 190dies_ok {
191 my $stuff = Stuff->new();
192 $stuff->sort_in_place_options( undef );
193} '... sort rejects arg of invalid type';
194
8c651099 195## test the meta
196
197my $options = $stuff->meta->get_attribute('options');
198isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
22d869ff 199
8c651099 200is_deeply($options->provides, {
201 'push' => 'add_options',
202 'pop' => 'remove_last_option',
203 'shift' => 'remove_first_option',
204 'unshift' => 'insert_options',
205 'get' => 'get_option_at',
206 'set' => 'set_option_at',
207 'count' => 'num_options',
208 'empty' => 'has_options',
8cf40f80 209 'clear' => 'clear_options',
71703b28 210 'sort_in_place' => 'sort_options_in_place',
211}, '... got the right provides mapping');
22d869ff 212
c43a2317 213is($options->type_constraint->type_parameter, 'Str', '... got the right container type');