From: Johannes Plunien Date: Wed, 17 Jun 2009 13:15:10 +0000 (+0200) Subject: Added test for "kv", added new method "elements" to MX::AH::MP::ImmutableHash X-Git-Tag: 0.20~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=23940f5024cbda43201409c2601620a2f2c6b63e;p=gitmo%2FMooseX-AttributeHelpers.git Added test for "kv", added new method "elements" to MX::AH::MP::ImmutableHash --- diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm index 03436c5..8b8a4c4 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm @@ -47,6 +47,16 @@ sub kv : method { }; } +sub elements : method { + my ($attr, $reader, $writer) = @_; + return sub { + my $h = $reader->($_[0]); + map { + $_, $h->{$_} + } CORE::keys %{$h} + }; +} + sub count : method { my ($attr, $reader, $writer) = @_; return sub { scalar CORE::keys %{$reader->($_[0])} }; @@ -114,7 +124,11 @@ Returns the list of values in the hash. =item B -Returns the key, value pairs in the hash +Returns the key, value pairs in the hash as array references + +=item B + +Returns the key, value pairs in the hash as a flattened list =back diff --git a/t/003_basic_hash.t b/t/003_basic_hash.t index 5f66998..1f842ae 100644 --- a/t/003_basic_hash.t +++ b/t/003_basic_hash.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 48; +use Test::More tests => 50; use Test::Exception; BEGIN { @@ -30,6 +30,8 @@ BEGIN { 'exists' => 'has_option', 'defined' => 'is_defined', 'accessor' => 'option_accessor', + 'kv' => 'key_value', + 'elements' => 'options_elements', }, curries => { 'accessor' => { @@ -161,6 +163,27 @@ is_deeply($options->provides, { '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 = $stuff->key_value; +is_deeply( + \@key_value, + [ [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ], + '... 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' +);