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