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