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