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