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