X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F003_basic_hash.t;h=79ca4cdedb778f17b5b719830f1906c6c3d890e5;hb=671d0d24bba344c05d06cc0950a73b4e46fc668c;hp=f0baeba9c981e1946ba11b535eba2e8990b1beed;hpb=5431dff2bf81c8286d40150d9f87a42a2a3e994c;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/t/003_basic_hash.t b/t/003_basic_hash.t index f0baeba..79ca4cd 100644 --- a/t/003_basic_hash.t +++ b/t/003_basic_hash.t @@ -3,11 +3,11 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 45; use Test::Exception; BEGIN { - use_ok('MooseX::AttributeHelpers'); + use_ok('MooseX::AttributeHelpers'); } { @@ -21,11 +21,20 @@ BEGIN { isa => 'HashRef[Str]', default => sub { {} }, provides => { - 'set' => 'set_option', - 'get' => 'get_option', - 'empty' => 'has_options', - 'count' => 'num_options', - 'delete' => 'delete_option', + 'set' => 'set_option', + 'get' => 'get_option', + 'empty' => 'has_options', + 'count' => 'num_options', + 'clear' => 'clear_options', + 'delete' => 'delete_option', + 'exists' => 'has_option', + 'defined' => 'is_defined', + 'accessor' => 'option_accessor', + }, + curries => { + 'accessor' => { + quantity => ['quantity'], + }, } ); } @@ -39,19 +48,28 @@ can_ok($stuff, $_) for qw[ has_options num_options delete_option + clear_options + is_defined + has_option + quantity + option_accessor ]; ok(!$stuff->has_options, '... we have no options'); is($stuff->num_options, 0, '... we have no options'); is_deeply($stuff->options, {}, '... no options yet'); +ok(!$stuff->has_option('foo'), '... we have no foo option'); lives_ok { $stuff->set_option(foo => 'bar'); } '... set the option okay'; +ok($stuff->is_defined('foo'), '... foo is defined'); + ok($stuff->has_options, '... we have options'); is($stuff->num_options, 1, '... we have 1 option(s)'); +ok($stuff->has_option('foo'), '... we have a foo option'); is_deeply($stuff->options, { foo => 'bar' }, '... got options now'); lives_ok { @@ -63,13 +81,42 @@ is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options is($stuff->get_option('foo'), 'bar', '... got the right option'); +is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once"); + +lives_ok { + $stuff->set_option(oink => "blah", xxy => "flop"); +} '... set the option okay'; + +is($stuff->num_options, 4, "4 options"); +is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once"); + lives_ok { $stuff->delete_option('bar'); } '... deleted the option okay'; +lives_ok { + $stuff->delete_option('oink'); +} '... deleted the option okay'; + +lives_ok { + $stuff->delete_option('xxy'); +} '... deleted the option okay'; + is($stuff->num_options, 1, '... we have 1 option(s)'); is_deeply($stuff->options, { foo => 'bar' }, '... got more options now'); +$stuff->clear_options; + +is_deeply($stuff->options, { }, "... cleared options" ); + +lives_ok { + $stuff->quantity(4); +} '... options added okay with defaults'; + +is($stuff->quantity, 4, 'reader part of curried accessor works'); + +is_deeply($stuff->options, {quantity => 4}, '... returns what we expect'); + lives_ok { Stuff->new(options => { foo => 'BAR' }); } '... good constructor params'; @@ -84,4 +131,21 @@ dies_ok { Stuff->new(options => { foo => [] }); } '... bad constructor params'; +## test the meta + +my $options = $stuff->meta->get_attribute('options'); +isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash'); + +is_deeply($options->provides, { + 'set' => 'set_option', + 'get' => 'get_option', + 'empty' => 'has_options', + 'count' => 'num_options', + 'clear' => 'clear_options', + 'delete' => 'delete_option', + 'defined' => 'is_defined', + 'exists' => 'has_option', + 'accessor' => 'option_accessor', +}, '... got the right provides mapping'); +is($options->type_constraint->type_parameter, 'Str', '... got the right container type');