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