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