Fix tests
[gitmo/Mouse.git] / t / 070_native_traits / 203_trait_hash.t
CommitLineData
fde8e43f 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!!!
4use t::lib::MooseCompat;
5
6use strict;
7use warnings;
8
9use Test::More;
10$TODO = q{Mouse is not yet completed};
11use Test::Exception;
12use 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
40my $stuff = Stuff->new();
41isa_ok( $stuff, 'Stuff' );
42
43can_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
56ok( $stuff->has_no_options, '... we have no options' );
57is( $stuff->num_options, 0, '... we have no options' );
58
59is_deeply( $stuff->options, {}, '... no options yet' );
60ok( !$stuff->has_option('foo'), '... we have no foo option' );
61
62my $set_result;
63lives_ok {
64 $set_result = $stuff->set_option( foo => 'bar' );
65}
66'... set the option okay';
67is($set_result, 'bar', '... returns value set');
68
69ok( $stuff->is_defined('foo'), '... foo is defined' );
70
71ok( !$stuff->has_no_options, '... we have options' );
72is( $stuff->num_options, 1, '... we have 1 option(s)' );
73ok( $stuff->has_option('foo'), '... we have a foo option' );
74is_deeply( $stuff->options, { foo => 'bar' }, '... got options now' );
75
76lives_ok {
77 $set_result = $stuff->set_option( bar => 'baz' );
78}
79'... set the option okay';
80is($set_result, 'baz', '... returns value set');
81
82is( $stuff->num_options, 2, '... we have 2 option(s)' );
83is_deeply( $stuff->options, { foo => 'bar', bar => 'baz' },
84 '... got more options now' );
85
86is( $stuff->get_option('foo'), 'bar', '... got the right option' );
87
88is_deeply( [ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)],
89 "get multiple options at once" );
90
91is( scalar($stuff->get_option(qw( foo bar) )), "baz",
92 '... got last option in scalar context');
93
94my @set_return;
95lives_ok {
96 @set_return = $stuff->set_option( oink => "blah", xxy => "flop" );
97}
98'... set the option okay';
99is_deeply(\@set_return, [ qw(blah flop) ], '... and returns values set');
100
101is( $stuff->num_options, 4, "4 options" );
102is_deeply( [ $stuff->get_option(qw(foo bar oink xxy)) ],
103 [qw(bar baz blah flop)], "get multiple options at once" );
104
105lives_ok {
106 $stuff->delete_option('bar');
107}
108'... deleted the option okay';
109
110lives_ok {
111 $stuff->delete_option('oink','xxy');
112}
113'... deleted multiple option okay';
114
115is( $stuff->num_options, 1, '... we have 1 option(s)' );
116is_deeply( $stuff->options, { foo => 'bar' }, '... got more options now' );
117
118$stuff->clear_options;
119
120is_deeply( $stuff->options, {}, "... cleared options" );
121
122lives_ok {
123 $stuff->quantity(4);
124}
125'... options added okay with defaults';
126
127is( $stuff->quantity, 4, 'reader part of curried accessor works' );
128
129is_deeply( $stuff->options, { quantity => 4 }, '... returns what we expect' );
130
131lives_ok {
132 Stuff->new( options => { foo => 'BAR' } );
133}
134'... good constructor params';
135
136## check some errors
137
138dies_ok {
139 $stuff->set_option( bar => {} );
140}
141'... could not add a hash ref where an string is expected';
142
143dies_ok {
144 Stuff->new( options => { foo => [] } );
145}
146'... bad constructor params';
147
148## test the meta
149
150my $options = $stuff->meta->get_attribute('options');
151does_ok( $options, 'Mouse::Meta::Attribute::Native::Trait::Hash' );
152
153is_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
172is( $options->type_constraint->type_parameter, 'Str',
173 '... got the right container type' );
174
175$stuff->set_option( oink => "blah", xxy => "flop" );
176my @key_value = sort{ $a->[0] cmp $b->[0] } $stuff->key_value;
177is_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
183my %options_elements = $stuff->options_elements;
184is_deeply(
185 \%options_elements,
186 {
187 'oink' => 'blah',
188 'quantity' => 4,
189 'xxy' => 'flop'
190 },
191 '... got the right hash elements'
192);
193
194done_testing;