Add doy to the main list of authors
[gitmo/MooseX-AttributeHelpers.git] / t / 203_trait_hash.t
CommitLineData
0951a229 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 35;
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' => (
20 traits => [qw/Collection::Hash/],
21 is => 'ro',
22 isa => 'HashRef[Str]',
23 default => sub { {} },
24 provides => {
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 }
32 );
33}
34
35my $stuff = Stuff->new();
36isa_ok($stuff, 'Stuff');
37
38can_ok($stuff, $_) for qw[
39 set_option
40 get_option
41 has_options
42 num_options
43 delete_option
44 clear_options
45];
46
47ok(!$stuff->has_options, '... we have no options');
48is($stuff->num_options, 0, '... we have no options');
49
50is_deeply($stuff->options, {}, '... no options yet');
51
52lives_ok {
53 $stuff->set_option(foo => 'bar');
54} '... set the option okay';
55
56ok($stuff->has_options, '... we have options');
57is($stuff->num_options, 1, '... we have 1 option(s)');
58is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
59
60lives_ok {
61 $stuff->set_option(bar => 'baz');
62} '... set the option okay';
63
64is($stuff->num_options, 2, '... we have 2 option(s)');
65is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
66
67is($stuff->get_option('foo'), 'bar', '... got the right option');
68
69is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
70
71lives_ok {
72 $stuff->set_option(oink => "blah", xxy => "flop");
73} '... set the option okay';
74
75is($stuff->num_options, 4, "4 options");
76is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
77
78lives_ok {
79 $stuff->delete_option('bar');
80} '... deleted the option okay';
81
82lives_ok {
83 $stuff->delete_option('oink');
84} '... deleted the option okay';
85
86lives_ok {
87 $stuff->delete_option('xxy');
88} '... deleted the option okay';
89
90is($stuff->num_options, 1, '... we have 1 option(s)');
91is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
92
93$stuff->clear_options;
94
95is_deeply($stuff->options, { }, "... cleared options" );
96
97lives_ok {
98 Stuff->new(options => { foo => 'BAR' });
99} '... good constructor params';
100
101## check some errors
102
103dies_ok {
104 $stuff->set_option(bar => {});
105} '... could not add a hash ref where an string is expected';
106
107dies_ok {
108 Stuff->new(options => { foo => [] });
109} '... bad constructor params';
110
111## test the meta
112
113my $options = $stuff->meta->get_attribute('options');
114does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::Hash');
115
116is_deeply($options->provides, {
117 'set' => 'set_option',
118 'get' => 'get_option',
119 'empty' => 'has_options',
120 'count' => 'num_options',
121 'clear' => 'clear_options',
122 'delete' => 'delete_option',
123}, '... got the right provies mapping');
124
125is($options->type_constraint->type_parameter, 'Str', '... got the right container type');