Trying to help confound and Sartak. jhannah newb alert!
[gitmo/Moose.git] / t / 070_attribute_helpers / 002_basic_array.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 69;
7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose::AttributeHelpers');
11}
12
13{
14 package Stuff;
15 use Moose;
16
17 has 'options' => (
18 metaclass => 'Collection::Array',
19 is => 'ro',
20 isa => 'ArrayRef[Str]',
21 default => sub { [] },
5f70ee9c 22 handles => {
23 'add_options' => 'push',
24 'remove_last_option' => 'pop',
25 'remove_first_option' => 'shift',
26 'insert_options' => 'unshift',
27 'get_option_at' => 'get',
28 'set_option_at' => 'set',
29 'num_options' => 'count',
30 'has_options' => 'empty',
31 'clear_options' => 'clear',
32 'splice_options' => 'splice',
33 'sort_options_in_place' => 'sort_in_place',
34 'option_accessor' => 'accessor',
35 'add_optons_with_speed' =>
36 [ 'push' => ['funrolls', 'funbuns'] ],
37 'prepend_prerequisites_along_with' =>
38 [ 'unshift' => ['first', 'second'] ],
39 'descending_options' =>
40 [ 'sort_in_place' => [ sub { $_[1] <=> $_[0] } ] ],
e3c07b19 41 },
42 }
43 );
44}
45
46my $stuff = Stuff->new(options => [ 10, 12 ]);
47isa_ok($stuff, 'Stuff');
48
49can_ok($stuff, $_) for qw[
50 add_options
51 remove_last_option
52 remove_first_option
53 insert_options
54 get_option_at
55 set_option_at
56 num_options
57 clear_options
58 has_options
59 sort_options_in_place
60 option_accessor
61];
62
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');
72
73ok(!$stuff->has_options, '... no options');
74is($stuff->num_options, 0, '... got no options');
75
76lives_ok {
77 $stuff->add_options(1, 2, 3);
78} '... set the option okay';
79
80is_deeply($stuff->options, [1, 2, 3], '... got options now');
81
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
89lives_ok {
90 $stuff->set_option_at(1, 100);
91} '... set the option okay';
92
93is($stuff->get_option_at(1), 100, '... get option at index 1');
94
95lives_ok {
96 $stuff->add_options(10, 15);
97} '... set the option okay';
98
99is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
100
101is($stuff->num_options, 5, '... got 5 options');
102
103is($stuff->remove_last_option, 15, '... removed the last option');
104
105is($stuff->num_options, 4, '... got 4 options');
106is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
107
108lives_ok {
109 $stuff->insert_options(10, 20);
110} '... set the option okay';
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
124$stuff->clear_options;
125is_deeply( $stuff->options, [], "... clear options" );
126
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)" );
130
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" );
141
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';
144
145$stuff->clear_options;
146
147lives_ok {
148 $stuff->add_options('tree');
149} '... set the options okay';
150
151lives_ok {
152 $stuff->add_options_with_speed('compatible', 'safe');
153} '... add options with speed okay';
154
155is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/],
156 'check options after add_options_with_speed');
157
158lives_ok {
159 $stuff->prepend_prerequisites_along_with();
160} '... add prerequisite options okay';
161
162$stuff->clear_options;
163$stuff->add_options( 1, 2 );
164
165lives_ok {
166 $stuff->splice_options( 1, 0, 'foo' );
167} '... splice_options works';
168
169is_deeply(
170 $stuff->options, [ 1, 'foo', 2 ],
171 'splice added expected option'
172);
173
174is($stuff->option_accessor(1 => 'foo++'), 'foo++');
175is($stuff->option_accessor(1), 'foo++');
176
177## check some errors
178
179#dies_ok {
180# $stuff->insert_options(undef);
181#} '... could not add an undef where a string is expected';
182#
183#dies_ok {
184# $stuff->set_option(5, {});
185#} '... could not add a hash ref where a string is expected';
186
187dies_ok {
188 Stuff->new(options => [ undef, 10, undef, 20 ]);
189} '... bad constructor params';
190
191dies_ok {
192 my $stuff = Stuff->new();
193 $stuff->add_options(undef);
194} '... rejects push of an invalid type';
195
196dies_ok {
197 my $stuff = Stuff->new();
198 $stuff->insert_options(undef);
199} '... rejects unshift of an invalid type';
200
201dies_ok {
202 my $stuff = Stuff->new();
203 $stuff->set_option_at( 0, undef );
204} '... rejects set of an invalid type';
205
206dies_ok {
207 my $stuff = Stuff->new();
208 $stuff->sort_in_place_options( undef );
209} '... sort rejects arg of invalid type';
210
211dies_ok {
212 my $stuff = Stuff->new();
213 $stuff->option_accessor();
214} '... accessor rejects 0 args';
215
216dies_ok {
217 my $stuff = Stuff->new();
218 $stuff->option_accessor(1, 2, 3);
219} '... accessor rejects 3 args';
220
221## test the meta
222
223my $options = $stuff->meta->get_attribute('options');
224isa_ok($options, 'Moose::AttributeHelpers::Collection::Array');
225
5f70ee9c 226is_deeply($options->handles,
227 'add_options' => 'push',
228 'remove_last_option' => 'pop',
229 'remove_first_option' => 'shift',
230 'insert_options' => 'unshift',
231 'get_option_at' => 'get',
232 'set_option_at' => 'set',
233 'num_options' => 'count',
234 'has_options' => 'empty',
235 'clear_options' => 'clear',
236 'splice_options' => 'splice',
237 'sort_options_in_place' => 'sort_in_place',
238 'option_accessor' => 'accessor',
239}, '... got the right handles mapping');
e3c07b19 240
241is($options->type_constraint->type_parameter, 'Str', '... got the right container type');