Skip to content

Instantly share code, notes, and snippets.

@chizmw
Created August 16, 2012 13:39
Show Gist options
  • Save chizmw/3370157 to your computer and use it in GitHub Desktop.
Save chizmw/3370157 to your computer and use it in GitHub Desktop.
Strange type checking behaviour
package My::Class;
use strict;
use warnings;
use Moose;
use MooseX::AttributeHelpers;
use MooseX::Types::Moose qw/ArrayRef Int/;
use MooseX::Types -declare => [qw(Thingy ThingyList)];
subtype Thingy,
as Int,
message { 'hrm' };
subtype ThingyList,
as ArrayRef[Int],
message { 'hrm' };
has thingy_list => (
metaclass => 'Collection::Array',
is => 'rw',
isa => ArrayRef[Thingy],
default => sub { [] },
required => 0,
provides => {
push => 'add_thingy',
set => 'set_thingy',
},
);
has blah_list => (
metaclass => 'Collection::Array',
is => 'rw',
isa => ThingyList,
default => sub { [] },
required => 0,
provides => {
push => 'add_blah',
set => 'set_blah',
},
);
1;
package main;
use strict;
use warnings;
use Test::More;
use Test::Exception;
plan skip_all => 'Misbehaviour demo'
unless $ENV{RUN_TEST_DEMO};
my $class = My::Class->new;
my @attributes = qw(thingy blah);
foreach my $attr (@attributes) {
my $add_method = "add_${attr}";
my $set_method = "set_${attr}";
my $list_method = "${attr}_list";
note 'Starting tests for attribute: ' . $list_method;
lives_ok {
$class->$add_method(0);
} 'integer 0 added to ' . $attr;
lives_ok {
$class->$add_method(1);
} 'integer 1 added to ' . $attr;
lives_ok {
$class->$set_method(6,7);
} 'integer 7 set as offset 6 for ' . $attr;
dies_ok {
$class->$add_method('Apple');
} 'dies as expected adding string for ' . $attr;
dies_ok {
$class->$set_method(4,'Apple');
} 'dies as expected setting string at offset for ' . $attr;
is_deeply(
$class->$list_method,
[
0,
1,
undef,
undef,
undef,
undef,
7
],
'final list is as expected for ' . $attr
);
}
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment