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