adding in the new junk to this
[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 => 26;
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         }
31     );
32 }
33
34 my $stuff = Stuff->new();
35 isa_ok($stuff, 'Stuff');
36
37 can_ok($stuff, $_) for qw[
38     set_option
39     get_option
40     has_options
41     num_options
42     delete_option
43     clear_options
44 ];
45
46 ok(!$stuff->has_options, '... we have no options');
47 is($stuff->num_options, 0, '... we have no options');
48
49 is_deeply($stuff->options, {}, '... no options yet');
50
51 lives_ok {
52     $stuff->set_option(foo => 'bar');
53 } '... set the option okay';
54
55 ok($stuff->has_options, '... we have options');
56 is($stuff->num_options, 1, '... we have 1 option(s)');
57 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
58
59 lives_ok {
60     $stuff->set_option(bar => 'baz');
61 } '... set the option okay';
62
63 is($stuff->num_options, 2, '... we have 2 option(s)');
64 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
65
66 is($stuff->get_option('foo'), 'bar', '... got the right option');
67
68 lives_ok {
69     $stuff->delete_option('bar');
70 } '... deleted the option okay';
71
72 is($stuff->num_options, 1, '... we have 1 option(s)');
73 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
74
75 $stuff->clear_options;
76
77 is_deeply($stuff->options, { }, "... cleared options" );
78
79 lives_ok {
80     Stuff->new(options => { foo => 'BAR' });
81 } '... good constructor params';
82
83 ## check some errors
84
85 dies_ok {
86     $stuff->set_option(bar => {});
87 } '... could not add a hash ref where an string is expected';
88
89 dies_ok {
90     Stuff->new(options => { foo => [] });
91 } '... bad constructor params';
92
93