Commit | Line | Data |
d26633fc |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
2a6837a2 |
6 | use Test::More tests => 48; |
69dde336 |
7 | use Test::Exception; |
d26633fc |
8 | |
9 | BEGIN { |
56803bef |
10 | use_ok('MooseX::AttributeHelpers'); |
d26633fc |
11 | } |
12 | |
13 | { |
14 | package Stuff; |
15 | use Moose; |
5431dff2 |
16 | use MooseX::AttributeHelpers; |
d26633fc |
17 | |
18 | has 'options' => ( |
19 | metaclass => 'Collection::Hash', |
20 | is => 'ro', |
c25a396f |
21 | isa => 'HashRef[Str]', |
d26633fc |
22 | default => sub { {} }, |
23 | provides => { |
671d0d24 |
24 | 'set' => 'set_option', |
25 | 'get' => 'get_option', |
26 | 'empty' => 'has_options', |
27 | 'count' => 'num_options', |
28 | 'clear' => 'clear_options', |
29 | 'delete' => 'delete_option', |
30 | 'exists' => 'has_option', |
31 | 'defined' => 'is_defined', |
32 | 'accessor' => 'option_accessor', |
c43a2317 |
33 | }, |
34 | curries => { |
671d0d24 |
35 | 'accessor' => { |
36 | quantity => ['quantity'], |
3656a0d7 |
37 | }, |
d26633fc |
38 | } |
39 | ); |
40 | } |
41 | |
42 | my $stuff = Stuff->new(); |
43 | isa_ok($stuff, 'Stuff'); |
44 | |
c25a396f |
45 | can_ok($stuff, $_) for qw[ |
46 | set_option |
47 | get_option |
48 | has_options |
49 | num_options |
158d8e20 |
50 | delete_option |
8cf40f80 |
51 | clear_options |
c0dcad02 |
52 | is_defined |
9bfd5e92 |
53 | has_option |
671d0d24 |
54 | quantity |
55 | option_accessor |
c25a396f |
56 | ]; |
57 | |
58 | ok(!$stuff->has_options, '... we have no options'); |
59 | is($stuff->num_options, 0, '... we have no options'); |
60 | |
d26633fc |
61 | is_deeply($stuff->options, {}, '... no options yet'); |
57f29c32 |
62 | ok(!$stuff->has_option('foo'), '... we have no foo option'); |
d26633fc |
63 | |
69dde336 |
64 | lives_ok { |
65 | $stuff->set_option(foo => 'bar'); |
66 | } '... set the option okay'; |
c25a396f |
67 | |
c0dcad02 |
68 | ok($stuff->is_defined('foo'), '... foo is defined'); |
69 | |
c25a396f |
70 | ok($stuff->has_options, '... we have options'); |
71 | is($stuff->num_options, 1, '... we have 1 option(s)'); |
9bfd5e92 |
72 | ok($stuff->has_option('foo'), '... we have a foo option'); |
d26633fc |
73 | is_deeply($stuff->options, { foo => 'bar' }, '... got options now'); |
74 | |
69dde336 |
75 | lives_ok { |
76 | $stuff->set_option(bar => 'baz'); |
77 | } '... set the option okay'; |
c25a396f |
78 | |
79 | is($stuff->num_options, 2, '... we have 2 option(s)'); |
d26633fc |
80 | is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now'); |
81 | |
82 | is($stuff->get_option('foo'), 'bar', '... got the right option'); |
83 | |
05f7da43 |
84 | is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once"); |
85 | |
86 | lives_ok { |
87 | $stuff->set_option(oink => "blah", xxy => "flop"); |
88 | } '... set the option okay'; |
89 | |
90 | is($stuff->num_options, 4, "4 options"); |
91 | is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once"); |
92 | |
77d02b8b |
93 | lives_ok { |
158d8e20 |
94 | $stuff->delete_option('bar'); |
95 | } '... deleted the option okay'; |
96 | |
05f7da43 |
97 | lives_ok { |
98 | $stuff->delete_option('oink'); |
99 | } '... deleted the option okay'; |
100 | |
101 | lives_ok { |
102 | $stuff->delete_option('xxy'); |
103 | } '... deleted the option okay'; |
104 | |
158d8e20 |
105 | is($stuff->num_options, 1, '... we have 1 option(s)'); |
106 | is_deeply($stuff->options, { foo => 'bar' }, '... got more options now'); |
107 | |
8cf40f80 |
108 | $stuff->clear_options; |
109 | |
110 | is_deeply($stuff->options, { }, "... cleared options" ); |
111 | |
158d8e20 |
112 | lives_ok { |
671d0d24 |
113 | $stuff->quantity(4); |
c43a2317 |
114 | } '... options added okay with defaults'; |
115 | |
671d0d24 |
116 | is($stuff->quantity, 4, 'reader part of curried accessor works'); |
117 | |
9bfd5e92 |
118 | is_deeply($stuff->options, {quantity => 4}, '... returns what we expect'); |
c43a2317 |
119 | |
120 | lives_ok { |
77d02b8b |
121 | Stuff->new(options => { foo => 'BAR' }); |
122 | } '... good constructor params'; |
123 | |
69dde336 |
124 | ## check some errors |
125 | |
126 | dies_ok { |
127 | $stuff->set_option(bar => {}); |
128 | } '... could not add a hash ref where an string is expected'; |
d26633fc |
129 | |
77d02b8b |
130 | dies_ok { |
131 | Stuff->new(options => { foo => [] }); |
132 | } '... bad constructor params'; |
133 | |
2a6837a2 |
134 | dies_ok { |
135 | my $stuff = Stuff->new; |
136 | $stuff->option_accessor(); |
137 | } '... accessor dies on 0 args'; |
138 | |
139 | dies_ok { |
140 | my $stuff = Stuff->new; |
141 | $stuff->option_accessor(1 => 2, 3); |
142 | } '... accessor dies on 3 args'; |
143 | |
144 | dies_ok { |
145 | my $stuff = Stuff->new; |
146 | $stuff->option_accessor(1 => 2, 3 => 4); |
147 | } '... accessor dies on 4 args'; |
148 | |
321ab9fe |
149 | ## test the meta |
d26633fc |
150 | |
321ab9fe |
151 | my $options = $stuff->meta->get_attribute('options'); |
152 | isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash'); |
153 | |
154 | is_deeply($options->provides, { |
671d0d24 |
155 | 'set' => 'set_option', |
156 | 'get' => 'get_option', |
157 | 'empty' => 'has_options', |
158 | 'count' => 'num_options', |
159 | 'clear' => 'clear_options', |
160 | 'delete' => 'delete_option', |
161 | 'defined' => 'is_defined', |
162 | 'exists' => 'has_option', |
163 | 'accessor' => 'option_accessor', |
164 | }, '... got the right provides mapping'); |
321ab9fe |
165 | |
166 | is($options->type_constraint->type_parameter, 'Str', '... got the right container type'); |