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