All unit tests passing with refactored stuff, documentation updated significantly.
[gitmo/MooseX-AttributeHelpers.git] / t / 002_basic_array.t
CommitLineData
22d869ff 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
720fa35b 6use Test::More tests => 54;
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',
8c651099 20 isa => 'ArrayRef[Int]',
22d869ff 21 default => sub { [] },
22 provides => {
720fa35b 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 'is_empty' => 'no_options',
31 'has_items' => 'has_options',
32 'clear' => 'clear_options',
22d869ff 33 }
34 );
35}
36
77d02b8b 37my $stuff = Stuff->new(options => [ 10, 12 ]);
22d869ff 38isa_ok($stuff, 'Stuff');
39
8c651099 40can_ok($stuff, $_) for qw[
41 add_options
42 remove_last_option
43 remove_first_option
44 insert_options
45 get_option_at
46 set_option_at
47 num_options
8cf40f80 48 clear_options
8c651099 49 has_options
720fa35b 50 no_options
8c651099 51];
52
77d02b8b 53is_deeply($stuff->options, [10, 12], '... got options');
54
55ok($stuff->has_options, '... we have options');
56is($stuff->num_options, 2, '... got 2 options');
57
58is($stuff->remove_last_option, 12, '... removed the last option');
59is($stuff->remove_first_option, 10, '... removed the last option');
60
61is_deeply($stuff->options, [], '... no options anymore');
22d869ff 62
720fa35b 63ok($stuff->no_options, '... no options');
8c651099 64is($stuff->num_options, 0, '... got no options');
65
69dde336 66lives_ok {
67 $stuff->add_options(1, 2, 3);
68} '... set the option okay';
69
22d869ff 70is_deeply($stuff->options, [1, 2, 3], '... got options now');
71
720fa35b 72ok($stuff->has_options, '... have options');
8c651099 73is($stuff->num_options, 3, '... got 3 options');
74
75is($stuff->get_option_at(0), 1, '... get option at index 0');
76is($stuff->get_option_at(1), 2, '... get option at index 1');
77is($stuff->get_option_at(2), 3, '... get option at index 2');
78
69dde336 79lives_ok {
80 $stuff->set_option_at(1, 100);
81} '... set the option okay';
8c651099 82
83is($stuff->get_option_at(1), 100, '... get option at index 1');
84
69dde336 85lives_ok {
86 $stuff->add_options(10, 15);
87} '... set the option okay';
88
8c651099 89is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
90
91is($stuff->num_options, 5, '... got 5 options');
22d869ff 92
93is($stuff->remove_last_option, 15, '... removed the last option');
94
8c651099 95is($stuff->num_options, 4, '... got 4 options');
96is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
97
69dde336 98lives_ok {
99 $stuff->insert_options(10, 20);
100} '... set the option okay';
8c651099 101
102is($stuff->num_options, 6, '... got 6 options');
103is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
104
105is($stuff->get_option_at(0), 10, '... get option at index 0');
106is($stuff->get_option_at(1), 20, '... get option at index 1');
107is($stuff->get_option_at(3), 100, '... get option at index 3');
108
109is($stuff->remove_first_option, 10, '... getting the first option');
110
111is($stuff->num_options, 5, '... got 5 options');
112is($stuff->get_option_at(0), 20, '... get option at index 0');
113
8cf40f80 114$stuff->clear_options;
115is_deeply( $stuff->options, [], "... clear options" );
116
720fa35b 117lives_ok {
118 $stuff->set_option_at([0..2], 10, 20, 30);
119} '... set multiple options';
120
121is_deeply(
122 [$stuff->get_option_at(0..2)],
123 [10,20,30],
124 '... and got what we set'
125);
126
69dde336 127## check some errors
128
129dies_ok {
130 $stuff->add_options([]);
131} '... could not add an array ref where an int is expected';
132
133dies_ok {
134 $stuff->insert_options(undef);
135} '... could not add an undef where an int is expected';
136
137dies_ok {
138 $stuff->set_option(5, {});
139} '... could not add a hash ref where an int is expected';
140
77d02b8b 141dies_ok {
142 Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
143} '... bad constructor params';
144
8c651099 145## test the meta
146
147my $options = $stuff->meta->get_attribute('options');
148isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
22d869ff 149
8c651099 150is_deeply($options->provides, {
720fa35b 151 'push' => 'add_options',
152 'pop' => 'remove_last_option',
153 'shift' => 'remove_first_option',
154 'unshift' => 'insert_options',
155 'get' => 'get_option_at',
156 'set' => 'set_option_at',
157 'count' => 'num_options',
158 'is_empty' => 'no_options',
159 'has_items' => 'has_options',
160 'clear' => 'clear_options',
161}, '... got the right provides mapping');
22d869ff 162
9a976497 163is($options->type_constraint->type_parameter, 'Int', '... got the right container type');