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