Skip to content
Browse files

more detailed bug test

  • Loading branch information...
1 parent dff3072 commit 6d8e23181cd2e6d5c051e3acc00d38db55c3452d @jjn1056 committed Sep 7, 2011
Showing with 34 additions and 8 deletions.
  1. +34 −8 t/09-coercions-bug.t
View
42 t/09-coercions-bug.t
@@ -1,46 +1,72 @@
package Person;
+use Test::More;
+
use Moose;
use MooseX::Types::Parameterizable qw(Parameterizable);
use MooseX::Types::Moose qw(Int Str HashRef ArrayRef);
use MooseX::Types -declare=>[qw(
- InfoHash OlderThanAge2 DefinedOlderThanAge
+ Varchar InfoHash OlderThanAge
)];
+
+subtype Varchar,
+as Parameterizable[Str,Int],
+where {
+ my($string, $int) = @_;
+ $int >= length($string) ? 1:0;
+},
+message { "'$_[0]' is too long (max length $_[1])" };
+
+coerce Varchar,
+from ArrayRef,
+via {
+ my ($arrayref, $int) = @_;
+ my $str = join('', @$arrayref);
+# die $str;
+ return $str;
+};
+
subtype( InfoHash,
as HashRef[Int],
where {
defined $_->{older_than};
}),
-subtype( OlderThanAge2,
+subtype( OlderThanAge,
as Parameterizable[Int, InfoHash],
where {
my ($value, $dict) = @_;
return $value > $dict->{older_than} ? 1:0;
});
-coerce OlderThanAge2,
+coerce OlderThanAge,
from HashRef,
-via {
+via {
my ($hashref, $constraining_value) = @_;
return scalar(keys(%$hashref));
},
from ArrayRef,
-via {
+via {
my ($arrayref, $constraining_value) = @_;
my $age;
$age += $_ for @$arrayref;
return $age;
};
-has age=>(is=>'rw',isa=>OlderThanAge2[older_than=>2],coerce=>1);
-use Test::More;
+my $olderthan = OlderThanAge[older_than=>2];
+my $varchar = Varchar[5];
+
+has age=>(is=>'rw', isa=>$olderthan, coerce=>1);
+has name=>(is=>'rw', isa=>$varchar,coerce=>1);
ok my $person = Person->new,
'Created a testable object';
-s
+
+ok $person->name('john'), 'john is less than 5 chars';
+is $person->name([qw/j o h n/]), 'john', 'j o h n is john';
+
ok $person->age(3),
'3 is older than 2';
is $person->age([1..10]), 55,

0 comments on commit 6d8e231

Please sign in to comment.
Something went wrong with that request. Please try again.