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