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; |
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', |
af44c00c |
22 | 'has_no_options' => '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 | |
af44c00c |
52 | ok( $stuff->has_no_options, '... we have no options' ); |
d50fc84a |
53 | is( $stuff->num_options, 0, '... we have no options' ); |
e3c07b19 |
54 | |
d50fc84a |
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 | } |
61 | '... set the option okay'; |
e3c07b19 |
62 | |
d50fc84a |
63 | ok( $stuff->is_defined('foo'), '... foo is defined' ); |
59de9de4 |
64 | |
af44c00c |
65 | ok( !$stuff->has_no_options, '... we have options' ); |
d50fc84a |
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 | } |
73 | '... set the option okay'; |
e3c07b19 |
74 | |
d50fc84a |
75 | is( $stuff->num_options, 2, '... we have 2 option(s)' ); |
76 | is_deeply( $stuff->options, { foo => 'bar', bar => 'baz' }, |
77 | '... got more options now' ); |
e3c07b19 |
78 | |
d50fc84a |
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 | |
84 | lives_ok { |
d50fc84a |
85 | $stuff->set_option( oink => "blah", xxy => "flop" ); |
86 | } |
87 | '... set the option okay'; |
e3c07b19 |
88 | |
d50fc84a |
89 | is( $stuff->num_options, 4, "4 options" ); |
90 | is_deeply( [ $stuff->get_option(qw(foo bar oink xxy)) ], |
91 | [qw(bar baz blah flop)], "get multiple options at once" ); |
e3c07b19 |
92 | |
93 | lives_ok { |
94 | $stuff->delete_option('bar'); |
d50fc84a |
95 | } |
96 | '... deleted the option okay'; |
e3c07b19 |
97 | |
98 | lives_ok { |
99 | $stuff->delete_option('oink'); |
d50fc84a |
100 | } |
101 | '... deleted the option okay'; |
e3c07b19 |
102 | |
103 | lives_ok { |
104 | $stuff->delete_option('xxy'); |
d50fc84a |
105 | } |
106 | '... deleted the option okay'; |
e3c07b19 |
107 | |
d50fc84a |
108 | is( $stuff->num_options, 1, '... we have 1 option(s)' ); |
109 | is_deeply( $stuff->options, { foo => 'bar' }, '... got more options now' ); |
e3c07b19 |
110 | |
111 | $stuff->clear_options; |
112 | |
d50fc84a |
113 | is_deeply( $stuff->options, {}, "... cleared options" ); |
e3c07b19 |
114 | |
115 | lives_ok { |
59de9de4 |
116 | $stuff->quantity(4); |
d50fc84a |
117 | } |
118 | '... options added okay with defaults'; |
59de9de4 |
119 | |
d50fc84a |
120 | is( $stuff->quantity, 4, 'reader part of curried accessor works' ); |
59de9de4 |
121 | |
d50fc84a |
122 | is_deeply( $stuff->options, { quantity => 4 }, '... returns what we expect' ); |
59de9de4 |
123 | |
124 | lives_ok { |
d50fc84a |
125 | Stuff->new( options => { foo => 'BAR' } ); |
126 | } |
127 | '... good constructor params'; |
e3c07b19 |
128 | |
129 | ## check some errors |
130 | |
131 | dies_ok { |
d50fc84a |
132 | $stuff->set_option( bar => {} ); |
133 | } |
134 | '... could not add a hash ref where an string is expected'; |
e3c07b19 |
135 | |
136 | dies_ok { |
d50fc84a |
137 | Stuff->new( options => { foo => [] } ); |
138 | } |
139 | '... bad constructor params'; |
e3c07b19 |
140 | |
141 | ## test the meta |
142 | |
143 | my $options = $stuff->meta->get_attribute('options'); |
c466e58f |
144 | does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Hash' ); |
d50fc84a |
145 | |
146 | is_deeply( |
147 | $options->handles, |
148 | { |
149 | 'set_option' => 'set', |
150 | 'get_option' => 'get', |
af44c00c |
151 | 'has_no_options' => 'empty', |
d50fc84a |
152 | 'num_options' => 'count', |
153 | 'clear_options' => 'clear', |
154 | 'delete_option' => 'delete', |
155 | 'has_option' => 'exists', |
156 | 'is_defined' => 'defined', |
157 | 'option_accessor' => 'accessor', |
158 | 'key_value' => 'kv', |
159 | 'options_elements' => 'elements', |
3c573ca4 |
160 | 'quantity' => [ accessor => 'quantity' ], |
d50fc84a |
161 | }, |
162 | '... got the right handles mapping' |
163 | ); |
164 | |
165 | is( $options->type_constraint->type_parameter, 'Str', |
166 | '... got the right container type' ); |
59de9de4 |
167 | |
168 | $stuff->set_option( oink => "blah", xxy => "flop" ); |
169 | my @key_value = $stuff->key_value; |
170 | is_deeply( |
171 | \@key_value, |
172 | [ [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ], |
173 | '... got the right key value pairs' |
174 | ); |
175 | |
176 | my %options_elements = $stuff->options_elements; |
177 | is_deeply( |
178 | \%options_elements, |
179 | { |
180 | 'oink' => 'blah', |
181 | 'quantity' => 4, |
182 | 'xxy' => 'flop' |
183 | }, |
184 | '... got the right hash elements' |
185 | ); |