X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F203_trait_hash.t;h=173e5db4f4a726680050c4442769f3b0c2dc261d;hb=ea8717c9fd7d7f42fd1eb5bfc66a2dbb1a5769d3;hp=509b2a6c0ff97384c8f365a8f162a4591ebddaf1;hpb=c1984b5cba19ecded4359b572d572baf13d8fc1d;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/t/203_trait_hash.t b/t/203_trait_hash.t index 509b2a6..173e5db 100644 --- a/t/203_trait_hash.t +++ b/t/203_trait_hash.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 35; +use Test::More tests => 47; use Test::Exception; use Test::Moose 'does_ok'; @@ -22,12 +22,22 @@ BEGIN { isa => 'HashRef[Str]', default => sub { {} }, provides => { - 'set' => 'set_option', - 'get' => 'get_option', - 'empty' => 'has_options', - 'count' => 'num_options', - 'clear' => 'clear_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', + 'kv' => 'key_value', + 'elements' => 'options_elements', + }, + curries => { + 'accessor' => { + quantity => ['quantity'], + }, } ); } @@ -42,19 +52,27 @@ can_ok($stuff, $_) for qw[ 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 { @@ -95,6 +113,14 @@ $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'; @@ -114,12 +140,36 @@ my $options = $stuff->meta->get_attribute('options'); does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::Hash'); is_deeply($options->provides, { - 'set' => 'set_option', - 'get' => 'get_option', - 'empty' => 'has_options', - 'count' => 'num_options', - 'clear' => 'clear_options', - 'delete' => 'delete_option', -}, '... got the right provies mapping'); + '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', + 'kv' => 'key_value', + 'elements' => 'options_elements', +}, '... got the right provides mapping'); is($options->type_constraint->type_parameter, 'Str', '... got the right container type'); + +$stuff->set_option( oink => "blah", xxy => "flop" ); +my @key_value = sort { $a->[0] cmp $b->[0] } $stuff->key_value; +is_deeply( + \@key_value, + [ [ 'oink', 'blah' ], [ 'quantity', 4 ], [ 'xxy', 'flop' ] ], + '... got the right key value pairs' +); + +my %options_elements = $stuff->options_elements; +is_deeply( + \%options_elements, + { + 'oink' => 'blah', + 'quantity' => 4, + 'xxy' => 'flop' + }, + '... got the right hash elements' +);