8 use Test::Moose 'does_ok';
17 isa => 'HashRef[Str]',
18 default => sub { {} },
20 'set_option' => 'set',
21 'get_option' => 'get',
22 'has_no_options' => 'is_empty',
23 'num_options' => 'count',
24 'clear_options' => 'clear',
25 'delete_option' => 'delete',
26 'has_option' => 'exists',
27 'is_defined' => 'defined',
28 'option_accessor' => 'accessor',
30 'options_elements' => 'elements',
31 'quantity' => [ accessor => 'quantity' ],
36 my $stuff = Stuff->new();
37 isa_ok( $stuff, 'Stuff' );
39 can_ok( $stuff, $_ ) for qw[
52 ok( $stuff->has_no_options, '... we have no options' );
53 is( $stuff->num_options, 0, '... we have no options' );
55 is_deeply( $stuff->options, {}, '... no options yet' );
56 ok( !$stuff->has_option('foo'), '... we have no foo option' );
59 $stuff->set_option( foo => 'bar' );
61 '... set the option okay';
63 ok( $stuff->is_defined('foo'), '... foo is defined' );
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' );
71 $stuff->set_option( bar => 'baz' );
73 '... set the option okay';
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' );
79 is( $stuff->get_option('foo'), 'bar', '... got the right option' );
81 is_deeply( [ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)],
82 "get multiple options at once" );
84 is( scalar($stuff->get_option(qw( foo bar) )), "baz",
85 '... got last option in scalar context');
88 $stuff->set_option( oink => "blah", xxy => "flop" );
90 '... set the option okay';
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" );
97 $stuff->delete_option('bar');
99 '... deleted the option okay';
102 $stuff->delete_option('oink','xxy');
104 '... deleted multiple option okay';
106 is( $stuff->num_options, 1, '... we have 1 option(s)' );
107 is_deeply( $stuff->options, { foo => 'bar' }, '... got more options now' );
109 $stuff->clear_options;
111 is_deeply( $stuff->options, {}, "... cleared options" );
116 '... options added okay with defaults';
118 is( $stuff->quantity, 4, 'reader part of curried accessor works' );
120 is_deeply( $stuff->options, { quantity => 4 }, '... returns what we expect' );
123 Stuff->new( options => { foo => 'BAR' } );
125 '... good constructor params';
130 $stuff->set_option( bar => {} );
132 '... could not add a hash ref where an string is expected';
135 Stuff->new( options => { foo => [] } );
137 '... bad constructor params';
141 my $options = $stuff->meta->get_attribute('options');
142 does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Hash' );
147 'set_option' => 'set',
148 'get_option' => 'get',
149 'has_no_options' => 'is_empty',
150 'num_options' => 'count',
151 'clear_options' => 'clear',
152 'delete_option' => 'delete',
153 'has_option' => 'exists',
154 'is_defined' => 'defined',
155 'option_accessor' => 'accessor',
157 'options_elements' => 'elements',
158 'quantity' => [ accessor => 'quantity' ],
160 '... got the right handles mapping'
163 is( $options->type_constraint->type_parameter, 'Str',
164 '... got the right container type' );
166 $stuff->set_option( oink => "blah", xxy => "flop" );
167 my @key_value = sort{ $a->[0] cmp $b->[0] } $stuff->key_value;
170 [ sort{ $a->[0] cmp $b->[0] } [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ],
171 '... got the right key value pairs'
172 ) or do{ require Data::Dumper; diag(Data::Dumper::Dumper(\@key_value)) };
174 my %options_elements = $stuff->options_elements;
182 '... got the right hash elements'