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