Commit | Line | Data |
22d869ff |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
9a976497 |
6 | use Test::More tests => 51; |
69dde336 |
7 | use Test::Exception; |
22d869ff |
8 | |
9 | BEGIN { |
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 |
36 | my $stuff = Stuff->new(options => [ 10, 12 ]); |
22d869ff |
37 | isa_ok($stuff, 'Stuff'); |
38 | |
8c651099 |
39 | can_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 |
51 | is_deeply($stuff->options, [10, 12], '... got options'); |
52 | |
53 | ok($stuff->has_options, '... we have options'); |
54 | is($stuff->num_options, 2, '... got 2 options'); |
55 | |
56 | is($stuff->remove_last_option, 12, '... removed the last option'); |
57 | is($stuff->remove_first_option, 10, '... removed the last option'); |
58 | |
59 | is_deeply($stuff->options, [], '... no options anymore'); |
22d869ff |
60 | |
8c651099 |
61 | ok(!$stuff->has_options, '... no options'); |
62 | is($stuff->num_options, 0, '... got no options'); |
63 | |
69dde336 |
64 | lives_ok { |
65 | $stuff->add_options(1, 2, 3); |
66 | } '... set the option okay'; |
67 | |
22d869ff |
68 | is_deeply($stuff->options, [1, 2, 3], '... got options now'); |
69 | |
8c651099 |
70 | ok($stuff->has_options, '... no options'); |
71 | is($stuff->num_options, 3, '... got 3 options'); |
72 | |
73 | is($stuff->get_option_at(0), 1, '... get option at index 0'); |
74 | is($stuff->get_option_at(1), 2, '... get option at index 1'); |
75 | is($stuff->get_option_at(2), 3, '... get option at index 2'); |
76 | |
69dde336 |
77 | lives_ok { |
78 | $stuff->set_option_at(1, 100); |
79 | } '... set the option okay'; |
8c651099 |
80 | |
81 | is($stuff->get_option_at(1), 100, '... get option at index 1'); |
82 | |
69dde336 |
83 | lives_ok { |
84 | $stuff->add_options(10, 15); |
85 | } '... set the option okay'; |
86 | |
8c651099 |
87 | is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now'); |
88 | |
89 | is($stuff->num_options, 5, '... got 5 options'); |
22d869ff |
90 | |
91 | is($stuff->remove_last_option, 15, '... removed the last option'); |
92 | |
8c651099 |
93 | is($stuff->num_options, 4, '... got 4 options'); |
94 | is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now'); |
95 | |
69dde336 |
96 | lives_ok { |
97 | $stuff->insert_options(10, 20); |
98 | } '... set the option okay'; |
8c651099 |
99 | |
100 | is($stuff->num_options, 6, '... got 6 options'); |
101 | is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now'); |
102 | |
103 | is($stuff->get_option_at(0), 10, '... get option at index 0'); |
104 | is($stuff->get_option_at(1), 20, '... get option at index 1'); |
105 | is($stuff->get_option_at(3), 100, '... get option at index 3'); |
106 | |
107 | is($stuff->remove_first_option, 10, '... getting the first option'); |
108 | |
109 | is($stuff->num_options, 5, '... got 5 options'); |
110 | is($stuff->get_option_at(0), 20, '... get option at index 0'); |
111 | |
8cf40f80 |
112 | $stuff->clear_options; |
113 | is_deeply( $stuff->options, [], "... clear options" ); |
114 | |
69dde336 |
115 | ## check some errors |
116 | |
117 | dies_ok { |
118 | $stuff->add_options([]); |
119 | } '... could not add an array ref where an int is expected'; |
120 | |
121 | dies_ok { |
122 | $stuff->insert_options(undef); |
123 | } '... could not add an undef where an int is expected'; |
124 | |
125 | dies_ok { |
126 | $stuff->set_option(5, {}); |
127 | } '... could not add a hash ref where an int is expected'; |
128 | |
77d02b8b |
129 | dies_ok { |
130 | Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]); |
131 | } '... bad constructor params'; |
132 | |
8c651099 |
133 | ## test the meta |
134 | |
135 | my $options = $stuff->meta->get_attribute('options'); |
136 | isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array'); |
22d869ff |
137 | |
8c651099 |
138 | is_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 |
150 | is($options->type_constraint->type_parameter, 'Int', '... got the right container type'); |