Added test for "kv", added new method "elements" to MX::AH::MP::ImmutableHash
[gitmo/MooseX-AttributeHelpers.git] / t / 003_basic_hash.t
index 1f48afc..1f842ae 100644 (file)
@@ -3,11 +3,11 @@
 use strict;
 use warnings;
 
-use Test::More tests => 39;
+use Test::More tests => 50;
 use Test::Exception;
 
 BEGIN {
-    use_ok('MooseX::AttributeHelpers');   
+    use_ok('MooseX::AttributeHelpers');
 }
 
 {
@@ -21,17 +21,21 @@ 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',
-            'exists' => 'has_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   => {
-            'set'    => {
-                set_quantity => ['quantity']
+            'accessor' => {
+                quantity => ['quantity'],
             },
         }
     );
@@ -47,18 +51,24 @@ 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');
@@ -102,9 +112,11 @@ $stuff->clear_options;
 is_deeply($stuff->options, { }, "... cleared options" );
 
 lives_ok {
-    $stuff->set_quantity(4);
+    $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 {
@@ -121,19 +133,57 @@ dies_ok {
     Stuff->new(options => { foo => [] });
 } '... bad constructor params';
 
+dies_ok {
+    my $stuff = Stuff->new;
+    $stuff->option_accessor();
+} '... accessor dies on 0 args';
+
+dies_ok {
+    my $stuff = Stuff->new;
+    $stuff->option_accessor(1 => 2, 3);
+} '... accessor dies on 3 args';
+
+dies_ok {
+    my $stuff = Stuff->new;
+    $stuff->option_accessor(1 => 2, 3 => 4);
+} '... accessor dies on 4 args';
+
 ## 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',
-    'exists' => 'has_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 = $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'
+);