Remove register_implementation, warn about future deprecation
[gitmo/MooseX-AttributeHelpers.git] / t / 202_trait_array.t
CommitLineData
0951a229 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 51;
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',
21 isa => 'ArrayRef[Int]',
22 default => sub { [] },
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 }
34 );
35}
36
37my $stuff = Stuff->new(options => [ 10, 12 ]);
38isa_ok($stuff, 'Stuff');
39
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
48 clear_options
49 has_options
50];
51
52is_deeply($stuff->options, [10, 12], '... got options');
53
54ok($stuff->has_options, '... we have options');
55is($stuff->num_options, 2, '... got 2 options');
56
57is($stuff->remove_last_option, 12, '... removed the last option');
58is($stuff->remove_first_option, 10, '... removed the last option');
59
60is_deeply($stuff->options, [], '... no options anymore');
61
62ok(!$stuff->has_options, '... no options');
63is($stuff->num_options, 0, '... got no options');
64
65lives_ok {
66 $stuff->add_options(1, 2, 3);
67} '... set the option okay';
68
69is_deeply($stuff->options, [1, 2, 3], '... got options now');
70
71ok($stuff->has_options, '... no options');
72is($stuff->num_options, 3, '... got 3 options');
73
74is($stuff->get_option_at(0), 1, '... get option at index 0');
75is($stuff->get_option_at(1), 2, '... get option at index 1');
76is($stuff->get_option_at(2), 3, '... get option at index 2');
77
78lives_ok {
79 $stuff->set_option_at(1, 100);
80} '... set the option okay';
81
82is($stuff->get_option_at(1), 100, '... get option at index 1');
83
84lives_ok {
85 $stuff->add_options(10, 15);
86} '... set the option okay';
87
88is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
89
90is($stuff->num_options, 5, '... got 5 options');
91
92is($stuff->remove_last_option, 15, '... removed the last option');
93
94is($stuff->num_options, 4, '... got 4 options');
95is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
96
97lives_ok {
98 $stuff->insert_options(10, 20);
99} '... set the option okay';
100
101is($stuff->num_options, 6, '... got 6 options');
102is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
103
104is($stuff->get_option_at(0), 10, '... get option at index 0');
105is($stuff->get_option_at(1), 20, '... get option at index 1');
106is($stuff->get_option_at(3), 100, '... get option at index 3');
107
108is($stuff->remove_first_option, 10, '... getting the first option');
109
110is($stuff->num_options, 5, '... got 5 options');
111is($stuff->get_option_at(0), 20, '... get option at index 0');
112
113$stuff->clear_options;
114is_deeply( $stuff->options, [], "... clear options" );
115
116## check some errors
117
118dies_ok {
119 $stuff->add_options([]);
120} '... could not add an array ref where an int is expected';
121
122dies_ok {
123 $stuff->insert_options(undef);
124} '... could not add an undef where an int is expected';
125
126dies_ok {
127 $stuff->set_option(5, {});
128} '... could not add a hash ref where an int is expected';
129
130dies_ok {
131 Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
132} '... bad constructor params';
133
134## test the meta
135
136my $options = $stuff->meta->get_attribute('options');
137does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::Array');
138
139is_deeply($options->provides, {
140 'push' => 'add_options',
141 'pop' => 'remove_last_option',
142 'shift' => 'remove_first_option',
143 'unshift' => 'insert_options',
144 'get' => 'get_option_at',
145 'set' => 'set_option_at',
146 'count' => 'num_options',
147 'empty' => 'has_options',
148 'clear' => 'clear_options',
149}, '... got the right provies mapping');
150
151is($options->type_constraint->type_parameter, 'Int', '... got the right container type');