add native hash attribute tests to confirm that set will always return the value...
[gitmo/Moose.git] / t / 070_native_traits / 203_trait_hash.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
e3c07b19 7use Test::Exception;
8use Test::Moose 'does_ok';
9
e3c07b19 10{
11 package Stuff;
12 use Moose;
e3c07b19 13
14 has 'options' => (
a40b446a 15 traits => ['Hash'],
d50fc84a 16 is => 'ro',
17 isa => 'HashRef[Str]',
18 default => sub { {} },
19 handles => {
0d103ac9 20 'set_option' => 'set',
21 'get_option' => 'get',
bb023e15 22 'has_no_options' => 'is_empty',
0d103ac9 23 'num_options' => 'count',
24 'clear_options' => 'clear',
25 'delete_option' => 'delete',
26 'has_option' => 'exists',
27 'is_defined' => 'defined',
28 'option_accessor' => 'accessor',
29 'key_value' => 'kv',
30 'options_elements' => 'elements',
3c573ca4 31 'quantity' => [ accessor => 'quantity' ],
59de9de4 32 },
e3c07b19 33 );
34}
35
36my $stuff = Stuff->new();
d50fc84a 37isa_ok( $stuff, 'Stuff' );
e3c07b19 38
d50fc84a 39can_ok( $stuff, $_ ) for qw[
e3c07b19 40 set_option
41 get_option
af44c00c 42 has_no_options
e3c07b19 43 num_options
44 delete_option
45 clear_options
59de9de4 46 is_defined
47 has_option
48 quantity
49 option_accessor
e3c07b19 50];
51
af44c00c 52ok( $stuff->has_no_options, '... we have no options' );
d50fc84a 53is( $stuff->num_options, 0, '... we have no options' );
e3c07b19 54
d50fc84a 55is_deeply( $stuff->options, {}, '... no options yet' );
56ok( !$stuff->has_option('foo'), '... we have no foo option' );
e3c07b19 57
ea8529e4 58my $set_result;
e3c07b19 59lives_ok {
ea8529e4 60 $set_result = $stuff->set_option( foo => 'bar' );
d50fc84a 61}
62'... set the option okay';
ea8529e4 63is($set_result, 'bar', '... returns value set');
e3c07b19 64
d50fc84a 65ok( $stuff->is_defined('foo'), '... foo is defined' );
59de9de4 66
af44c00c 67ok( !$stuff->has_no_options, '... we have options' );
d50fc84a 68is( $stuff->num_options, 1, '... we have 1 option(s)' );
69ok( $stuff->has_option('foo'), '... we have a foo option' );
70is_deeply( $stuff->options, { foo => 'bar' }, '... got options now' );
e3c07b19 71
72lives_ok {
ea8529e4 73 $set_result = $stuff->set_option( bar => 'baz' );
d50fc84a 74}
75'... set the option okay';
ea8529e4 76is($set_result, 'baz', '... returns value set');
e3c07b19 77
d50fc84a 78is( $stuff->num_options, 2, '... we have 2 option(s)' );
79is_deeply( $stuff->options, { foo => 'bar', bar => 'baz' },
80 '... got more options now' );
e3c07b19 81
d50fc84a 82is( $stuff->get_option('foo'), 'bar', '... got the right option' );
e3c07b19 83
d50fc84a 84is_deeply( [ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)],
85 "get multiple options at once" );
e3c07b19 86
85592815 87is( scalar($stuff->get_option(qw( foo bar) )), "baz",
88 '... got last option in scalar context');
89
ea8529e4 90my @set_return;
e3c07b19 91lives_ok {
ea8529e4 92 @set_return = $stuff->set_option( oink => "blah", xxy => "flop" );
d50fc84a 93}
94'... set the option okay';
ea8529e4 95is_deeply(\@set_return, [ qw(blah flop) ], '... and returns values set');
e3c07b19 96
d50fc84a 97is( $stuff->num_options, 4, "4 options" );
98is_deeply( [ $stuff->get_option(qw(foo bar oink xxy)) ],
99 [qw(bar baz blah flop)], "get multiple options at once" );
e3c07b19 100
101lives_ok {
102 $stuff->delete_option('bar');
d50fc84a 103}
104'... deleted the option okay';
e3c07b19 105
106lives_ok {
9d010f57 107 $stuff->delete_option('oink','xxy');
d50fc84a 108}
9d010f57 109'... deleted multiple option okay';
e3c07b19 110
d50fc84a 111is( $stuff->num_options, 1, '... we have 1 option(s)' );
112is_deeply( $stuff->options, { foo => 'bar' }, '... got more options now' );
e3c07b19 113
114$stuff->clear_options;
115
d50fc84a 116is_deeply( $stuff->options, {}, "... cleared options" );
e3c07b19 117
118lives_ok {
59de9de4 119 $stuff->quantity(4);
d50fc84a 120}
121'... options added okay with defaults';
59de9de4 122
d50fc84a 123is( $stuff->quantity, 4, 'reader part of curried accessor works' );
59de9de4 124
d50fc84a 125is_deeply( $stuff->options, { quantity => 4 }, '... returns what we expect' );
59de9de4 126
127lives_ok {
d50fc84a 128 Stuff->new( options => { foo => 'BAR' } );
129}
130'... good constructor params';
e3c07b19 131
132## check some errors
133
134dies_ok {
d50fc84a 135 $stuff->set_option( bar => {} );
136}
137'... could not add a hash ref where an string is expected';
e3c07b19 138
139dies_ok {
d50fc84a 140 Stuff->new( options => { foo => [] } );
141}
142'... bad constructor params';
e3c07b19 143
144## test the meta
145
146my $options = $stuff->meta->get_attribute('options');
c466e58f 147does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Hash' );
d50fc84a 148
149is_deeply(
150 $options->handles,
151 {
152 'set_option' => 'set',
153 'get_option' => 'get',
bb023e15 154 'has_no_options' => 'is_empty',
d50fc84a 155 'num_options' => 'count',
156 'clear_options' => 'clear',
157 'delete_option' => 'delete',
158 'has_option' => 'exists',
159 'is_defined' => 'defined',
160 'option_accessor' => 'accessor',
161 'key_value' => 'kv',
162 'options_elements' => 'elements',
3c573ca4 163 'quantity' => [ accessor => 'quantity' ],
d50fc84a 164 },
165 '... got the right handles mapping'
166);
167
168is( $options->type_constraint->type_parameter, 'Str',
169 '... got the right container type' );
59de9de4 170
171$stuff->set_option( oink => "blah", xxy => "flop" );
caa615b3 172my @key_value = sort{ $a->[0] cmp $b->[0] } $stuff->key_value;
59de9de4 173is_deeply(
174 \@key_value,
caa615b3 175 [ sort{ $a->[0] cmp $b->[0] } [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ],
59de9de4 176 '... got the right key value pairs'
caa615b3 177) or do{ require Data::Dumper; diag(Data::Dumper::Dumper(\@key_value)) };
59de9de4 178
179my %options_elements = $stuff->options_elements;
180is_deeply(
181 \%options_elements,
182 {
183 'oink' => 'blah',
184 'quantity' => 4,
185 'xxy' => 'flop'
186 },
187 '... got the right hash elements'
188);
a28e50e4 189
190done_testing;