Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a5209c26 |
6 | use Test::More tests => 46; |
e3c07b19 |
7 | use Test::Exception; |
8 | use Test::Moose 'does_ok'; |
9 | |
e3c07b19 |
10 | { |
11 | package Stuff; |
12 | use Moose; |
a41de825 |
13 | use Moose::AttributeHelpers; |
e3c07b19 |
14 | |
15 | has 'options' => ( |
a40b446a |
16 | traits => ['Hash'], |
d50fc84a |
17 | is => 'ro', |
18 | isa => 'HashRef[Str]', |
19 | default => sub { {} }, |
20 | handles => { |
0d103ac9 |
21 | 'set_option' => 'set', |
22 | 'get_option' => 'get', |
23 | 'has_options' => 'empty', |
24 | 'num_options' => 'count', |
25 | 'clear_options' => 'clear', |
26 | 'delete_option' => 'delete', |
27 | 'has_option' => 'exists', |
28 | 'is_defined' => 'defined', |
29 | 'option_accessor' => 'accessor', |
30 | 'key_value' => 'kv', |
31 | 'options_elements' => 'elements', |
d50fc84a |
32 | 'quantity' => [ accessor => ['quantity'] ], |
59de9de4 |
33 | }, |
e3c07b19 |
34 | ); |
35 | } |
36 | |
37 | my $stuff = Stuff->new(); |
d50fc84a |
38 | isa_ok( $stuff, 'Stuff' ); |
e3c07b19 |
39 | |
d50fc84a |
40 | can_ok( $stuff, $_ ) for qw[ |
e3c07b19 |
41 | set_option |
42 | get_option |
43 | has_options |
44 | num_options |
45 | delete_option |
46 | clear_options |
59de9de4 |
47 | is_defined |
48 | has_option |
49 | quantity |
50 | option_accessor |
e3c07b19 |
51 | ]; |
52 | |
d50fc84a |
53 | ok( !$stuff->has_options, '... we have no options' ); |
54 | is( $stuff->num_options, 0, '... we have no options' ); |
e3c07b19 |
55 | |
d50fc84a |
56 | is_deeply( $stuff->options, {}, '... no options yet' ); |
57 | ok( !$stuff->has_option('foo'), '... we have no foo option' ); |
e3c07b19 |
58 | |
59 | lives_ok { |
d50fc84a |
60 | $stuff->set_option( foo => 'bar' ); |
61 | } |
62 | '... set the option okay'; |
e3c07b19 |
63 | |
d50fc84a |
64 | ok( $stuff->is_defined('foo'), '... foo is defined' ); |
59de9de4 |
65 | |
d50fc84a |
66 | ok( $stuff->has_options, '... we have options' ); |
67 | is( $stuff->num_options, 1, '... we have 1 option(s)' ); |
68 | ok( $stuff->has_option('foo'), '... we have a foo option' ); |
69 | is_deeply( $stuff->options, { foo => 'bar' }, '... got options now' ); |
e3c07b19 |
70 | |
71 | lives_ok { |
d50fc84a |
72 | $stuff->set_option( bar => 'baz' ); |
73 | } |
74 | '... set the option okay'; |
e3c07b19 |
75 | |
d50fc84a |
76 | is( $stuff->num_options, 2, '... we have 2 option(s)' ); |
77 | is_deeply( $stuff->options, { foo => 'bar', bar => 'baz' }, |
78 | '... got more options now' ); |
e3c07b19 |
79 | |
d50fc84a |
80 | is( $stuff->get_option('foo'), 'bar', '... got the right option' ); |
e3c07b19 |
81 | |
d50fc84a |
82 | is_deeply( [ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], |
83 | "get multiple options at once" ); |
e3c07b19 |
84 | |
85 | lives_ok { |
d50fc84a |
86 | $stuff->set_option( oink => "blah", xxy => "flop" ); |
87 | } |
88 | '... set the option okay'; |
e3c07b19 |
89 | |
d50fc84a |
90 | is( $stuff->num_options, 4, "4 options" ); |
91 | is_deeply( [ $stuff->get_option(qw(foo bar oink xxy)) ], |
92 | [qw(bar baz blah flop)], "get multiple options at once" ); |
e3c07b19 |
93 | |
94 | lives_ok { |
95 | $stuff->delete_option('bar'); |
d50fc84a |
96 | } |
97 | '... deleted the option okay'; |
e3c07b19 |
98 | |
99 | lives_ok { |
100 | $stuff->delete_option('oink'); |
d50fc84a |
101 | } |
102 | '... deleted the option okay'; |
e3c07b19 |
103 | |
104 | lives_ok { |
105 | $stuff->delete_option('xxy'); |
d50fc84a |
106 | } |
107 | '... deleted the option okay'; |
e3c07b19 |
108 | |
d50fc84a |
109 | is( $stuff->num_options, 1, '... we have 1 option(s)' ); |
110 | is_deeply( $stuff->options, { foo => 'bar' }, '... got more options now' ); |
e3c07b19 |
111 | |
112 | $stuff->clear_options; |
113 | |
d50fc84a |
114 | is_deeply( $stuff->options, {}, "... cleared options" ); |
e3c07b19 |
115 | |
116 | lives_ok { |
59de9de4 |
117 | $stuff->quantity(4); |
d50fc84a |
118 | } |
119 | '... options added okay with defaults'; |
59de9de4 |
120 | |
d50fc84a |
121 | is( $stuff->quantity, 4, 'reader part of curried accessor works' ); |
59de9de4 |
122 | |
d50fc84a |
123 | is_deeply( $stuff->options, { quantity => 4 }, '... returns what we expect' ); |
59de9de4 |
124 | |
125 | lives_ok { |
d50fc84a |
126 | Stuff->new( options => { foo => 'BAR' } ); |
127 | } |
128 | '... good constructor params'; |
e3c07b19 |
129 | |
130 | ## check some errors |
131 | |
132 | dies_ok { |
d50fc84a |
133 | $stuff->set_option( bar => {} ); |
134 | } |
135 | '... could not add a hash ref where an string is expected'; |
e3c07b19 |
136 | |
137 | dies_ok { |
d50fc84a |
138 | Stuff->new( options => { foo => [] } ); |
139 | } |
140 | '... bad constructor params'; |
e3c07b19 |
141 | |
142 | ## test the meta |
143 | |
144 | my $options = $stuff->meta->get_attribute('options'); |
a40b446a |
145 | does_ok( $options, 'Moose::Meta::Attribute::Trait::Native::Hash' ); |
d50fc84a |
146 | |
147 | is_deeply( |
148 | $options->handles, |
149 | { |
150 | 'set_option' => 'set', |
151 | 'get_option' => 'get', |
152 | 'has_options' => 'empty', |
153 | 'num_options' => 'count', |
154 | 'clear_options' => 'clear', |
155 | 'delete_option' => 'delete', |
156 | 'has_option' => 'exists', |
157 | 'is_defined' => 'defined', |
158 | 'option_accessor' => 'accessor', |
159 | 'key_value' => 'kv', |
160 | 'options_elements' => 'elements', |
161 | 'quantity' => [ accessor => ['quantity'] ], |
162 | }, |
163 | '... got the right handles mapping' |
164 | ); |
165 | |
166 | is( $options->type_constraint->type_parameter, 'Str', |
167 | '... got the right container type' ); |
59de9de4 |
168 | |
169 | $stuff->set_option( oink => "blah", xxy => "flop" ); |
170 | my @key_value = $stuff->key_value; |
171 | is_deeply( |
172 | \@key_value, |
173 | [ [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ], |
174 | '... got the right key value pairs' |
175 | ); |
176 | |
177 | my %options_elements = $stuff->options_elements; |
178 | is_deeply( |
179 | \%options_elements, |
180 | { |
181 | 'oink' => 'blah', |
182 | 'quantity' => 4, |
183 | 'xxy' => 'flop' |
184 | }, |
185 | '... got the right hash elements' |
186 | ); |