adding support for checking container types in the constructor
[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 no_plan => 1;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('MooseX::AttributeHelpers');   
11 }
12
13 {
14     package Stuff;
15     use Moose;
16
17     has 'options' => (
18         metaclass => 'Collection::Hash',
19         is        => 'ro',
20         isa       => 'HashRef[Str]',
21         default   => sub { {} },
22         provides  => {
23             'set'   => 'set_option',
24             'get'   => 'get_option',            
25             'empty' => 'has_options',
26             'count' => 'num_options',
27         }
28     );
29 }
30
31 my $stuff = Stuff->new();
32 isa_ok($stuff, 'Stuff');
33
34 can_ok($stuff, $_) for qw[
35     set_option
36     get_option
37     has_options
38     num_options
39 ];
40
41 ok(!$stuff->has_options, '... we have no options');
42 is($stuff->num_options, 0, '... we have no options');
43
44 is_deeply($stuff->options, {}, '... no options yet');
45
46 lives_ok {
47     $stuff->set_option(foo => 'bar');
48 } '... set the option okay';
49
50 ok($stuff->has_options, '... we have options');
51 is($stuff->num_options, 1, '... we have 1 option(s)');
52 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
53
54 lives_ok {
55     $stuff->set_option(bar => 'baz');
56 } '... set the option okay';
57
58 is($stuff->num_options, 2, '... we have 2 option(s)');
59 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
60
61 is($stuff->get_option('foo'), 'bar', '... got the right option');
62
63 lives_ok {
64     Stuff->new(options => { foo => 'BAR' });
65 } '... good constructor params';
66
67 ## check some errors
68
69 dies_ok {
70     $stuff->set_option(bar => {});
71 } '... could not add a hash ref where an string is expected';
72
73 dies_ok {
74     Stuff->new(options => { foo => [] });
75 } '... bad constructor params';
76
77