From: Stevan Little Date: Tue, 22 May 2007 04:34:30 +0000 (+0000) Subject: more tests and tweaks X-Git-Tag: 0.18_01~80 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=69dde336792f25d5ed7d0a47d237a5382b4593a9;p=gitmo%2FMooseX-AttributeHelpers.git more tests and tweaks --- diff --git a/Build.PL b/Build.PL index f887a07..da4ff14 100644 --- a/Build.PL +++ b/Build.PL @@ -6,7 +6,7 @@ my $build = Module::Build->new( module_name => 'MooseX::AttributeHelpers', license => 'perl', requires => { - 'Moose' => '0.19', + 'Moose' => '0.21', }, optional => { }, diff --git a/lib/MooseX/AttributeHelpers/Collection.pm b/lib/MooseX/AttributeHelpers/Collection.pm index e009bde..d821b15 100644 --- a/lib/MooseX/AttributeHelpers/Collection.pm +++ b/lib/MooseX/AttributeHelpers/Collection.pm @@ -25,13 +25,15 @@ has 'container_type_constraint' => ( my $container_type = $self->container_type; my $constraint = find_type_constraint($container_type); - - $constraint = subtype( - 'Object', - sub { - $_->isa($container_type) || ($_->can('does') && $_->does($container_type)) - } - ) unless $constraint; + + # NOTE: + # I am not sure DWIM-ery is a good thing + # here, so i am going to err on the side + # of caution, and blow up if you have + # not made a type constraint for this yet. + # - SL + (defined $constraint) + || confess "You must predefine the '$container_type' constraint before you can use it as a container type"; return $constraint; } diff --git a/lib/MooseX/AttributeHelpers/Collection/Array.pm b/lib/MooseX/AttributeHelpers/Collection/Array.pm index 387e453..fbc3b1a 100644 --- a/lib/MooseX/AttributeHelpers/Collection/Array.pm +++ b/lib/MooseX/AttributeHelpers/Collection/Array.pm @@ -20,7 +20,7 @@ has '+method_constructors' => ( return sub { my $instance = shift; $container_type_constraint->check($_) - || confess "Value $_ did not pass container type constraint" + || confess "Value " . ($_||'undef') . " did not pass container type constraint" foreach @_; push @{$attr->get_value($instance)} => @_; }; @@ -43,7 +43,7 @@ has '+method_constructors' => ( return sub { my $instance = shift; $container_type_constraint->check($_) - || confess "Value $_ did not pass container type constraint" + || confess "Value " . ($_||'undef') . " did not pass container type constraint" foreach @_; unshift @{$attr->get_value($instance)} => @_; }; @@ -69,7 +69,7 @@ has '+method_constructors' => ( my $container_type_constraint = $attr->container_type_constraint; return sub { ($container_type_constraint->check($_[2])) - || confess "Value $_[2] did not pass container type constraint"; + || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; $attr->get_value($_[0])->[$_[1]] = $_[2] }; } diff --git a/lib/MooseX/AttributeHelpers/Collection/Hash.pm b/lib/MooseX/AttributeHelpers/Collection/Hash.pm index fc07031..27f2a9e 100644 --- a/lib/MooseX/AttributeHelpers/Collection/Hash.pm +++ b/lib/MooseX/AttributeHelpers/Collection/Hash.pm @@ -22,7 +22,7 @@ has '+method_constructors' => ( my $container_type_constraint = $attr->container_type_constraint; return sub { ($container_type_constraint->check($_[2])) - || confess "Value $_[2] did not pass container type constraint"; + || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; $attr->get_value($_[0])->{$_[1]} = $_[2] }; } diff --git a/t/001_basic_counter.t b/t/001_basic_counter.t index 64cc641..5430d44 100644 --- a/t/001_basic_counter.t +++ b/t/001_basic_counter.t @@ -28,8 +28,10 @@ BEGIN { my $page = MyHomePage->new(); isa_ok($page, 'MyHomePage'); -can_ok($page, 'inc_counter'); -can_ok($page, 'dec_counter'); +can_ok($page, $_) for qw[ + dec_counter + inc_counter +]; is($page->counter, 0, '... got the default value'); diff --git a/t/002_basic_array.t b/t/002_basic_array.t index 0679576..479269e 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'); @@ -50,7 +51,10 @@ is_deeply($stuff->options, [], '... no options yet'); 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 +64,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 +83,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 +99,20 @@ 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'); +## 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'; + ## test the meta my $options = $stuff->meta->get_attribute('options'); diff --git a/t/003_basic_hash.t b/t/003_basic_hash.t index 3748a3f..f2f73df 100644 --- a/t/003_basic_hash.t +++ b/t/003_basic_hash.t @@ -4,6 +4,7 @@ use strict; use warnings; use Test::More no_plan => 1; +use Test::Exception; BEGIN { use_ok('MooseX::AttributeHelpers'); @@ -42,18 +43,27 @@ is($stuff->num_options, 0, '... we have no options'); is_deeply($stuff->options, {}, '... no options yet'); -$stuff->set_option(foo => 'bar'); +lives_ok { + $stuff->set_option(foo => 'bar'); +} '... set the option okay'; ok($stuff->has_options, '... we have options'); is($stuff->num_options, 1, '... we have 1 option(s)'); is_deeply($stuff->options, { foo => 'bar' }, '... got options now'); -$stuff->set_option(bar => 'baz'); +lives_ok { + $stuff->set_option(bar => 'baz'); +} '... set the option okay'; is($stuff->num_options, 2, '... we have 2 option(s)'); is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now'); is($stuff->get_option('foo'), 'bar', '... got the right option'); +## check some errors + +dies_ok { + $stuff->set_option(bar => {}); +} '... could not add a hash ref where an string is expected';