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