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