Lots of files got moved around,a nd some got added.
[gitmo/MooseX-AttributeHelpers.git] / t / 003_basic_hash.t
CommitLineData
d26633fc 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
720fa35b 6use Test::More tests => 33;
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 => {
720fa35b 24 'set' => 'set_option',
25 'get' => 'get_option',
26 'has_items' => 'has_options',
27 'is_empty' => 'no_options',
28 'count' => 'num_options',
29 'clear' => 'clear_options',
30 'delete' => 'delete_option',
d26633fc 31 }
32 );
33}
34
35my $stuff = Stuff->new();
36isa_ok($stuff, 'Stuff');
37
c25a396f 38can_ok($stuff, $_) for qw[
39 set_option
40 get_option
41 has_options
720fa35b 42 no_options
c25a396f 43 num_options
158d8e20 44 delete_option
8cf40f80 45 clear_options
c25a396f 46];
47
720fa35b 48ok($stuff->no_options, '... we have no options');
c25a396f 49is($stuff->num_options, 0, '... we have no options');
50
d26633fc 51is_deeply($stuff->options, {}, '... no options yet');
52
69dde336 53lives_ok {
54 $stuff->set_option(foo => 'bar');
55} '... set the option okay';
c25a396f 56
57ok($stuff->has_options, '... we have options');
58is($stuff->num_options, 1, '... we have 1 option(s)');
d26633fc 59is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
60
69dde336 61lives_ok {
62 $stuff->set_option(bar => 'baz');
63} '... set the option okay';
c25a396f 64
65is($stuff->num_options, 2, '... we have 2 option(s)');
d26633fc 66is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
67
68is($stuff->get_option('foo'), 'bar', '... got the right option');
69
05f7da43 70is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
71
72lives_ok {
73 $stuff->set_option(oink => "blah", xxy => "flop");
74} '... set the option okay';
75
76is($stuff->num_options, 4, "4 options");
77is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
78
77d02b8b 79lives_ok {
158d8e20 80 $stuff->delete_option('bar');
81} '... deleted the option okay';
82
05f7da43 83lives_ok {
84 $stuff->delete_option('oink');
85} '... deleted the option okay';
86
87lives_ok {
88 $stuff->delete_option('xxy');
89} '... deleted the option okay';
90
158d8e20 91is($stuff->num_options, 1, '... we have 1 option(s)');
92is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
93
8cf40f80 94$stuff->clear_options;
95
96is_deeply($stuff->options, { }, "... cleared options" );
97
158d8e20 98lives_ok {
77d02b8b 99 Stuff->new(options => { foo => 'BAR' });
100} '... good constructor params';
101
69dde336 102## check some errors
103
104dies_ok {
105 $stuff->set_option(bar => {});
106} '... could not add a hash ref where an string is expected';
d26633fc 107
77d02b8b 108dies_ok {
109 Stuff->new(options => { foo => [] });
110} '... bad constructor params';
111
d26633fc 112