Fix pod coverage.
[gitmo/MooseX-AttributeHelpers.git] / t / 003_basic_hash.t
CommitLineData
d26633fc 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
c0dcad02 6use Test::More tests => 42;
69dde336 7use Test::Exception;
d26633fc 8
9BEGIN {
56803bef 10 use_ok('MooseX::AttributeHelpers');
d26633fc 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',
56803bef 25 'get' => 'get_option',
158d8e20 26 'empty' => 'has_options',
27 'count' => 'num_options',
8cf40f80 28 'clear' => 'clear_options',
158d8e20 29 'delete' => 'delete_option',
9bfd5e92 30 'exists' => 'has_option',
56803bef 31 'defined'=> 'is_defined',
c43a2317 32 },
33 curries => {
3656a0d7 34 'set' => {
35 set_quantity => ['quantity']
36 },
d26633fc 37 }
38 );
39}
40
41my $stuff = Stuff->new();
42isa_ok($stuff, 'Stuff');
43
c25a396f 44can_ok($stuff, $_) for qw[
45 set_option
46 get_option
47 has_options
48 num_options
158d8e20 49 delete_option
8cf40f80 50 clear_options
c0dcad02 51 is_defined
9bfd5e92 52 has_option
c25a396f 53];
54
55ok(!$stuff->has_options, '... we have no options');
56is($stuff->num_options, 0, '... we have no options');
57
d26633fc 58is_deeply($stuff->options, {}, '... no options yet');
57f29c32 59ok(!$stuff->has_option('foo'), '... we have no foo option');
d26633fc 60
69dde336 61lives_ok {
62 $stuff->set_option(foo => 'bar');
63} '... set the option okay';
c25a396f 64
c0dcad02 65ok($stuff->is_defined('foo'), '... foo is defined');
66
c25a396f 67ok($stuff->has_options, '... we have options');
68is($stuff->num_options, 1, '... we have 1 option(s)');
9bfd5e92 69ok($stuff->has_option('foo'), '... we have a foo option');
d26633fc 70is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
71
69dde336 72lives_ok {
73 $stuff->set_option(bar => 'baz');
74} '... set the option okay';
c25a396f 75
76is($stuff->num_options, 2, '... we have 2 option(s)');
d26633fc 77is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
78
79is($stuff->get_option('foo'), 'bar', '... got the right option');
80
05f7da43 81is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
82
83lives_ok {
84 $stuff->set_option(oink => "blah", xxy => "flop");
85} '... set the option okay';
86
87is($stuff->num_options, 4, "4 options");
88is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
89
77d02b8b 90lives_ok {
158d8e20 91 $stuff->delete_option('bar');
92} '... deleted the option okay';
93
05f7da43 94lives_ok {
95 $stuff->delete_option('oink');
96} '... deleted the option okay';
97
98lives_ok {
99 $stuff->delete_option('xxy');
100} '... deleted the option okay';
101
158d8e20 102is($stuff->num_options, 1, '... we have 1 option(s)');
103is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
104
8cf40f80 105$stuff->clear_options;
106
107is_deeply($stuff->options, { }, "... cleared options" );
108
158d8e20 109lives_ok {
3656a0d7 110 $stuff->set_quantity(4);
c43a2317 111} '... options added okay with defaults';
112
9bfd5e92 113is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
c43a2317 114
115lives_ok {
77d02b8b 116 Stuff->new(options => { foo => 'BAR' });
117} '... good constructor params';
118
69dde336 119## check some errors
120
121dies_ok {
122 $stuff->set_option(bar => {});
123} '... could not add a hash ref where an string is expected';
d26633fc 124
77d02b8b 125dies_ok {
126 Stuff->new(options => { foo => [] });
127} '... bad constructor params';
128
321ab9fe 129## test the meta
d26633fc 130
321ab9fe 131my $options = $stuff->meta->get_attribute('options');
132isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
133
134is_deeply($options->provides, {
56803bef 135 'set' => 'set_option',
136 'get' => 'get_option',
137 'empty' => 'has_options',
138 'count' => 'num_options',
139 'clear' => 'clear_options',
140 'delete' => 'delete_option',
141 'defined' => 'is_defined',
142 'exists' => 'has_option',
321ab9fe 143}, '... got the right provies mapping');
144
145is($options->type_constraint->type_parameter, 'Str', '... got the right container type');