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