6 use Test::More tests => 46;
8 use Test::Moose 'does_ok';
13 use Moose::AttributeHelpers;
16 traits => [qw/Collection::Hash/],
18 isa => 'HashRef[Str]',
19 default => sub { {} },
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',
31 'options_elements' => 'elements',
32 'quantity' => [ accessor => ['quantity'] ],
37 my $stuff = Stuff->new();
38 isa_ok( $stuff, 'Stuff' );
40 can_ok( $stuff, $_ ) for qw[
53 ok( !$stuff->has_options, '... we have no options' );
54 is( $stuff->num_options, 0, '... we have no options' );
56 is_deeply( $stuff->options, {}, '... no options yet' );
57 ok( !$stuff->has_option('foo'), '... we have no foo option' );
60 $stuff->set_option( foo => 'bar' );
62 '... set the option okay';
64 ok( $stuff->is_defined('foo'), '... foo is defined' );
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' );
72 $stuff->set_option( bar => 'baz' );
74 '... set the option okay';
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' );
80 is( $stuff->get_option('foo'), 'bar', '... got the right option' );
82 is_deeply( [ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)],
83 "get multiple options at once" );
86 $stuff->set_option( oink => "blah", xxy => "flop" );
88 '... set the option okay';
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" );
95 $stuff->delete_option('bar');
97 '... deleted the option okay';
100 $stuff->delete_option('oink');
102 '... deleted the option okay';
105 $stuff->delete_option('xxy');
107 '... deleted the option okay';
109 is( $stuff->num_options, 1, '... we have 1 option(s)' );
110 is_deeply( $stuff->options, { foo => 'bar' }, '... got more options now' );
112 $stuff->clear_options;
114 is_deeply( $stuff->options, {}, "... cleared options" );
119 '... options added okay with defaults';
121 is( $stuff->quantity, 4, 'reader part of curried accessor works' );
123 is_deeply( $stuff->options, { quantity => 4 }, '... returns what we expect' );
126 Stuff->new( options => { foo => 'BAR' } );
128 '... good constructor params';
133 $stuff->set_option( bar => {} );
135 '... could not add a hash ref where an string is expected';
138 Stuff->new( options => { foo => [] } );
140 '... bad constructor params';
144 my $options = $stuff->meta->get_attribute('options');
145 does_ok( $options, 'Moose::AttributeHelpers::Trait::Collection::Hash' );
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',
160 'options_elements' => 'elements',
161 'quantity' => [ accessor => ['quantity'] ],
163 '... got the right handles mapping'
166 is( $options->type_constraint->type_parameter, 'Str',
167 '... got the right container type' );
169 $stuff->set_option( oink => "blah", xxy => "flop" );
170 my @key_value = $stuff->key_value;
173 [ [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ],
174 '... got the right key value pairs'
177 my %options_elements = $stuff->options_elements;
185 '... got the right hash elements'