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