0.06
[gitmo/MooseX-AttributeHelpers.git] / t / 003_basic_hash.t
CommitLineData
d26633fc 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
9a976497 6use Test::More tests => 26;
69dde336 7use Test::Exception;
d26633fc 8
9BEGIN {
10 use_ok('MooseX::AttributeHelpers');
11}
12
13{
14 package Stuff;
15 use Moose;
5431dff2 16 use MooseX::AttributeHelpers;
d26633fc 17
18 has 'options' => (
19 metaclass => 'Collection::Hash',
20 is => 'ro',
c25a396f 21 isa => 'HashRef[Str]',
d26633fc 22 default => sub { {} },
23 provides => {
158d8e20 24 'set' => 'set_option',
25 'get' => 'get_option',
26 'empty' => 'has_options',
27 'count' => 'num_options',
8cf40f80 28 'clear' => 'clear_options',
158d8e20 29 'delete' => 'delete_option',
d26633fc 30 }
31 );
32}
33
34my $stuff = Stuff->new();
35isa_ok($stuff, 'Stuff');
36
c25a396f 37can_ok($stuff, $_) for qw[
38 set_option
39 get_option
40 has_options
41 num_options
158d8e20 42 delete_option
8cf40f80 43 clear_options
c25a396f 44];
45
46ok(!$stuff->has_options, '... we have no options');
47is($stuff->num_options, 0, '... we have no options');
48
d26633fc 49is_deeply($stuff->options, {}, '... no options yet');
50
69dde336 51lives_ok {
52 $stuff->set_option(foo => 'bar');
53} '... set the option okay';
c25a396f 54
55ok($stuff->has_options, '... we have options');
56is($stuff->num_options, 1, '... we have 1 option(s)');
d26633fc 57is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
58
69dde336 59lives_ok {
60 $stuff->set_option(bar => 'baz');
61} '... set the option okay';
c25a396f 62
63is($stuff->num_options, 2, '... we have 2 option(s)');
d26633fc 64is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
65
66is($stuff->get_option('foo'), 'bar', '... got the right option');
67
77d02b8b 68lives_ok {
158d8e20 69 $stuff->delete_option('bar');
70} '... deleted the option okay';
71
72is($stuff->num_options, 1, '... we have 1 option(s)');
73is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
74
8cf40f80 75$stuff->clear_options;
76
77is_deeply($stuff->options, { }, "... cleared options" );
78
158d8e20 79lives_ok {
77d02b8b 80 Stuff->new(options => { foo => 'BAR' });
81} '... good constructor params';
82
69dde336 83## check some errors
84
85dies_ok {
86 $stuff->set_option(bar => {});
87} '... could not add a hash ref where an string is expected';
d26633fc 88
77d02b8b 89dies_ok {
90 Stuff->new(options => { foo => [] });
91} '... bad constructor params';
92
d26633fc 93