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