X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F002_basic_array.t;h=4cc3404018d6ac8a66d07aeda1788191b6df18ff;hb=2dee86e968a5bcf2ed8502a2861ecd26853fef9e;hp=067957650bd9c42a7fd29633081cf6dc140e3cce;hpb=c25a396fc4e4fbaa12eea8a77233632c3ef5eaa6;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/t/002_basic_array.t b/t/002_basic_array.t index 0679576..4cc3404 100644 --- a/t/002_basic_array.t +++ b/t/002_basic_array.t @@ -4,6 +4,7 @@ use strict; use warnings; use Test::More no_plan => 1; +use Test::Exception; BEGIN { use_ok('MooseX::AttributeHelpers'); @@ -27,11 +28,12 @@ BEGIN { 'set' => 'set_option_at', 'count' => 'num_options', 'empty' => 'has_options', + 'clear' => 'clear_options', } ); } -my $stuff = Stuff->new(); +my $stuff = Stuff->new(options => [ 10, 12 ]); isa_ok($stuff, 'Stuff'); can_ok($stuff, $_) for qw[ @@ -42,15 +44,27 @@ can_ok($stuff, $_) for qw[ get_option_at set_option_at num_options + clear_options has_options ]; -is_deeply($stuff->options, [], '... no options yet'); +is_deeply($stuff->options, [10, 12], '... got options'); + +ok($stuff->has_options, '... we have options'); +is($stuff->num_options, 2, '... got 2 options'); + +is($stuff->remove_last_option, 12, '... removed the last option'); +is($stuff->remove_first_option, 10, '... removed the last option'); + +is_deeply($stuff->options, [], '... no options anymore'); ok(!$stuff->has_options, '... no options'); is($stuff->num_options, 0, '... got no options'); -$stuff->add_options(1, 2, 3); +lives_ok { + $stuff->add_options(1, 2, 3); +} '... set the option okay'; + is_deeply($stuff->options, [1, 2, 3], '... got options now'); ok($stuff->has_options, '... no options'); @@ -60,11 +74,16 @@ is($stuff->get_option_at(0), 1, '... get option at index 0'); is($stuff->get_option_at(1), 2, '... get option at index 1'); is($stuff->get_option_at(2), 3, '... get option at index 2'); -$stuff->set_option_at(1, 100); +lives_ok { + $stuff->set_option_at(1, 100); +} '... set the option okay'; is($stuff->get_option_at(1), 100, '... get option at index 1'); -$stuff->add_options(10, 15); +lives_ok { + $stuff->add_options(10, 15); +} '... set the option okay'; + is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now'); is($stuff->num_options, 5, '... got 5 options'); @@ -74,7 +93,9 @@ is($stuff->remove_last_option, 15, '... removed the last option'); is($stuff->num_options, 4, '... got 4 options'); is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now'); -$stuff->insert_options(10, 20); +lives_ok { + $stuff->insert_options(10, 20); +} '... set the option okay'; is($stuff->num_options, 6, '... got 6 options'); is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now'); @@ -88,6 +109,27 @@ is($stuff->remove_first_option, 10, '... getting the first option'); is($stuff->num_options, 5, '... got 5 options'); is($stuff->get_option_at(0), 20, '... get option at index 0'); +$stuff->clear_options; +is_deeply( $stuff->options, [], "... clear options" ); + +## check some errors + +dies_ok { + $stuff->add_options([]); +} '... could not add an array ref where an int is expected'; + +dies_ok { + $stuff->insert_options(undef); +} '... could not add an undef where an int is expected'; + +dies_ok { + $stuff->set_option(5, {}); +} '... could not add a hash ref where an int is expected'; + +dies_ok { + Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]); +} '... bad constructor params'; + ## test the meta my $options = $stuff->meta->get_attribute('options'); @@ -102,6 +144,7 @@ is_deeply($options->provides, { 'set' => 'set_option_at', 'count' => 'num_options', 'empty' => 'has_options', + 'clear' => 'clear_options', }, '... got the right provies mapping'); is($options->container_type, 'Int', '... got the right container type');