All unit tests passing with refactored stuff, documentation updated significantly.
[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 => 33;
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             'has_items' => 'has_options',
27             'is_empty'  => 'no_options',
28             'count'     => 'num_options',
29             'clear'     => 'clear_options',
30             'delete'    => 'delete_option',
31         }
32     );
33 }
34
35 my $stuff = Stuff->new();
36 isa_ok($stuff, 'Stuff');
37
38 can_ok($stuff, $_) for qw[
39     set_option
40     get_option
41     has_options
42     no_options
43     num_options
44     delete_option
45     clear_options
46 ];
47
48 ok($stuff->no_options, '... we have no options');
49 is($stuff->num_options, 0, '... we have no options');
50
51 is_deeply($stuff->options, {}, '... no options yet');
52
53 lives_ok {
54     $stuff->set_option(foo => 'bar');
55 } '... set the option okay';
56
57 ok($stuff->has_options, '... we have options');
58 is($stuff->num_options, 1, '... we have 1 option(s)');
59 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
60
61 lives_ok {
62     $stuff->set_option(bar => 'baz');
63 } '... set the option okay';
64
65 is($stuff->num_options, 2, '... we have 2 option(s)');
66 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
67
68 is($stuff->get_option('foo'), 'bar', '... got the right option');
69
70 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
71
72 lives_ok {
73     $stuff->set_option(oink => "blah", xxy => "flop");
74 } '... set the option okay';
75
76 is($stuff->num_options, 4, "4 options");
77 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
78
79 lives_ok {
80     $stuff->delete_option('bar');
81 } '... deleted the option okay';
82
83 lives_ok {
84     $stuff->delete_option('oink');
85 } '... deleted the option okay';
86
87 lives_ok {
88     $stuff->delete_option('xxy');
89 } '... deleted the option okay';
90
91 is($stuff->num_options, 1, '... we have 1 option(s)');
92 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
93
94 $stuff->clear_options;
95
96 is_deeply($stuff->options, { }, "... cleared options" );
97
98 lives_ok {
99     Stuff->new(options => { foo => 'BAR' });
100 } '... good constructor params';
101
102 ## check some errors
103
104 dies_ok {
105     $stuff->set_option(bar => {});
106 } '... could not add a hash ref where an string is expected';
107
108 dies_ok {
109     Stuff->new(options => { foo => [] });
110 } '... bad constructor params';
111
112