more tests and tweaks
[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;
16
17 has 'options' => (
18 metaclass => 'Collection::Hash',
19 is => 'ro',
c25a396f 20 isa => 'HashRef[Str]',
d26633fc 21 default => sub { {} },
22 provides => {
c25a396f 23 'set' => 'set_option',
24 'get' => 'get_option',
25 'empty' => 'has_options',
26 'count' => 'num_options',
d26633fc 27 }
28 );
29}
30
31my $stuff = Stuff->new();
32isa_ok($stuff, 'Stuff');
33
c25a396f 34can_ok($stuff, $_) for qw[
35 set_option
36 get_option
37 has_options
38 num_options
39];
40
41ok(!$stuff->has_options, '... we have no options');
42is($stuff->num_options, 0, '... we have no options');
43
d26633fc 44is_deeply($stuff->options, {}, '... no options yet');
45
69dde336 46lives_ok {
47 $stuff->set_option(foo => 'bar');
48} '... set the option okay';
c25a396f 49
50ok($stuff->has_options, '... we have options');
51is($stuff->num_options, 1, '... we have 1 option(s)');
d26633fc 52is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
53
69dde336 54lives_ok {
55 $stuff->set_option(bar => 'baz');
56} '... set the option okay';
c25a396f 57
58is($stuff->num_options, 2, '... we have 2 option(s)');
d26633fc 59is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
60
61is($stuff->get_option('foo'), 'bar', '... got the right option');
62
69dde336 63## check some errors
64
65dies_ok {
66 $stuff->set_option(bar => {});
67} '... could not add a hash ref where an string is expected';
d26633fc 68
69