From: Florian Ragwitz Date: Tue, 1 Jun 2010 21:25:45 +0000 (+0200) Subject: Convert from Module::Install to Dist::Zilla X-Git-Tag: 0.23~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8dbdca20076b8b21e23c3235ee1655d047065e6e;p=gitmo%2FMooseX-Types-Structured.git Convert from Module::Install to Dist::Zilla Also clean up a lot of whitespace. --- diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP deleted file mode 100644 index ea08ec5..0000000 --- a/MANIFEST.SKIP +++ /dev/null @@ -1,44 +0,0 @@ - -# Avoid version control files. -\bRCS\b -\bCVS\b -,v$ -\B\.svn\b -\B\.git - -# Avoid Makemaker generated and utility files. -\bMakefile$ -\bblib -\bMakeMaker-\d -\bpm_to_blib$ -\bblibdirs$ -^MANIFEST\.SKIP$ - -# for developers only :) -^TODO$ -^VERSIONING\.SKETCH$ - -# Avoid Module::Build generated and utility files. -\bBuild$ -\b_build - -# Avoid temp and backup files. -~$ -\.tmp$ -\.old$ -\.bak$ -\#$ -\b\.# - -# avoid OS X finder files -\.DS_Store$ - -#skip komodo project files -\.kpf$ - - -# Don't ship the last dist we built :) -\.tar\.gz$ - -# Skip maint stuff -^maint/ diff --git a/Makefile.PL b/Makefile.PL deleted file mode 100644 index b1a5bf5..0000000 --- a/Makefile.PL +++ /dev/null @@ -1,33 +0,0 @@ -use inc::Module::Install; - -## All the required meta information -name 'MooseX-Types-Structured'; -all_from 'lib/MooseX/Types/Structured.pm'; -abstract 'Moose Type Constraint classes for Structured Types'; -author 'John Napiorkowski '; -license 'perl'; - -## Module dependencies -requires 'Moose' => '1.06'; -requires 'MooseX::Types' => '0.22'; -requires 'Devel::PartialDump' => '0.10'; -requires 'Sub::Exporter' => '0.982'; - -## Testing dependencies -build_requires 'Test::More' => '0.70'; -build_requires 'Test::Exception' => '0.27'; - -## Author tests dependencies -author_requires 'Test::Pod' => '1.14'; -author_requires 'Test::Pod::Coverage' => '1.08'; - -## Build README -system 'pod2text lib/MooseX/Types/Structured.pm > README' - if -e 'MANIFEST.SKIP'; - -## Instructions to Module::Install -auto_install; -tests_recursive; -WriteAll; - -1; diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..3d19b51 --- /dev/null +++ b/dist.ini @@ -0,0 +1,13 @@ +name = MooseX-Types-Structured +version = 0.22 +author = John Napiorkowski +author = Florian Ragwitz +author = Yuval Kogman +author = Tomas Doran +license = Perl_5 +copyright_holder = John Napiorkowski + +[@FLORA] +dist = MooseX-Types-Structured +authority = cpan:JJNAPIORK +repository_at = gitmo diff --git a/lib/MooseX/Meta/TypeCoercion/Structured.pm b/lib/MooseX/Meta/TypeCoercion/Structured.pm index b42eba9..2a8a57b 100644 --- a/lib/MooseX/Meta/TypeCoercion/Structured.pm +++ b/lib/MooseX/Meta/TypeCoercion/Structured.pm @@ -1,38 +1,22 @@ package ## Hide from PAUSE MooseX::Meta::TypeCoercion::Structured; +# ABSTRACT: MooseX::Meta::TypeCoercion::Structured - Coerce structured type constraints. use Moose; extends 'Moose::Meta::TypeCoercion'; -=head1 NAME - -MooseX::Meta::TypeCoercion::Structured - Coerce structured type constraints. - =head1 DESCRIPTION We need to make sure we can properly coerce the structure elements inside a structured type constraint. However requirements for the best way to allow this are still in flux. For now this class is a placeholder. -=head1 METHODS - -This class defines the following methods. - =head1 SEE ALSO The following modules or resources may be of interest. L, L -=head1 AUTHOR - -John Napiorkowski, C<< >> - -=head1 COPYRIGHT & LICENSE - -This program is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - =cut __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/MooseX/Meta/TypeCoercion/Structured/Optional.pm b/lib/MooseX/Meta/TypeCoercion/Structured/Optional.pm index 340cdc6..dcb254c 100644 --- a/lib/MooseX/Meta/TypeCoercion/Structured/Optional.pm +++ b/lib/MooseX/Meta/TypeCoercion/Structured/Optional.pm @@ -3,6 +3,10 @@ package MooseX::Meta::TypeCoercion::Structured::Optional; use Moose; extends 'Moose::Meta::TypeCoercion'; +=method compile_type_coercion + +=cut + sub compile_type_coercion { my ($self) = @_; my $constraint = $self->type_constraint->type_parameter; diff --git a/lib/MooseX/Meta/TypeConstraint/Structured.pm b/lib/MooseX/Meta/TypeConstraint/Structured.pm index 44691e4..9a76e4c 100644 --- a/lib/MooseX/Meta/TypeConstraint/Structured.pm +++ b/lib/MooseX/Meta/TypeConstraint/Structured.pm @@ -1,5 +1,6 @@ package ## Hide from PAUSE MooseX::Meta::TypeConstraint::Structured; +# ABSTRACT: MooseX::Meta::TypeConstraint::Structured - Structured type constraints. use Moose; use Devel::PartialDump; @@ -7,9 +8,6 @@ use Moose::Util::TypeConstraints (); use MooseX::Meta::TypeCoercion::Structured; extends 'Moose::Meta::TypeConstraint'; -=head1 NAME - -MooseX::Meta::TypeConstraint::Structured - Structured type constraints. =head1 DESCRIPTION @@ -19,15 +17,11 @@ idea here is that a Type Constraint could be something like, "An Int followed by an Int and then a Str" and that this could be done so with a declaration like: Tuple[Int,Int,Str]; ## Example syntax - + So a structure is a list of Type constraints (the "Int,Int,Str" in the above example) which are intended to function together. -=head1 ATTRIBUTES - -This class defines the following attributes. - -=head2 type_constraints +=attr type_constraints A list of L objects. @@ -39,7 +33,7 @@ has 'type_constraints' => ( predicate=>'has_type_constraints', ); -=head2 constraint_generator +=attr constraint_generator A subref or closure that contains the way we validate incoming values against a set of type constraints. @@ -58,16 +52,6 @@ has coercion => ( builder => '_build_coercion', ); -=head1 METHODS - -This class defines the following methods. - -=head2 new - -Initialization stuff. - -=cut - sub _build_coercion { my ($self) = @_; return MooseX::Meta::TypeCoercion::Structured->new( @@ -75,9 +59,10 @@ sub _build_coercion { ); } -=head2 validate +=method validate Messing with validate so that we can support niced error messages. + =cut override 'validate' => sub { @@ -98,7 +83,7 @@ override 'validate' => sub { } }; -=head2 generate_constraint_for ($type_constraints) +=method generate_constraint_for ($type_constraints) Given some type constraints, use them to generate validation rules for an ref of values (to be passed at check time) @@ -115,14 +100,13 @@ sub generate_constraint_for { }; } -=head2 parameterize (@type_constraints) +=method parameterize (@type_constraints) Given a ref of type constraints, create a structured type. =cut sub parameterize { - my ($self, @type_constraints) = @_; my $class = ref $self; my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']'; @@ -136,7 +120,7 @@ sub parameterize { ); } -=head2 __infer_constraint_generator +=method __infer_constraint_generator This returns a CODEREF which generates a suitable constraint generator. Not user servicable, you'll never call this directly. @@ -152,12 +136,12 @@ sub __infer_constraint_generator { ## I'm not sure about this stuff but everything seems to work my $tc = shift @_; my $merged_tc = [@$tc, @{$self->parent->type_constraints}]; - $self->constraint->($merged_tc, @_); + $self->constraint->($merged_tc, @_); }; - } + } } -=head2 compile_type_constraint +=method compile_type_constraint hook into compile_type_constraint so we can set the correct validation rules. @@ -165,17 +149,17 @@ hook into compile_type_constraint so we can set the correct validation rules. around 'compile_type_constraint' => sub { my ($compile_type_constraint, $self, @args) = @_; - + if($self->has_type_constraints) { my $type_constraints = $self->type_constraints; my $constraint = $self->generate_constraint_for($type_constraints); - $self->_set_constraint($constraint); + $self->_set_constraint($constraint); } return $self->$compile_type_constraint(@args); }; -=head2 create_child_type +=method create_child_type modifier to make sure we get the constraint_generator @@ -189,11 +173,11 @@ around 'create_child_type' => sub { ); }; -=head2 is_a_type_of +=method is_a_type_of -=head2 is_subtype_of +=method is_subtype_of -=head2 equals +=method equals Override the base class behavior. @@ -204,7 +188,7 @@ sub equals { my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); return unless $other->isa(__PACKAGE__); - + return ( $self->parent->equals($other->parent) and @@ -263,7 +247,7 @@ sub is_subtype_of { } } -=head2 type_constraints_equals +=method type_constraints_equals Checks to see if the internal type constraints are equal. @@ -288,14 +272,14 @@ sub _type_constraints_op_all { while(@self_type_constraints) { my $self_type_constraint = shift @self_type_constraints; my $other_type_constraint = shift @other_type_constraints; - + $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) for $self_type_constraint, $other_type_constraint; my $result = $self_type_constraint->$op($other_type_constraint); return unless $result; } - + return 1; ##If we get this far, everything is good. } @@ -313,17 +297,17 @@ sub _type_constraints_op_any { while(@self_type_constraints) { my $self_type_constraint = shift @self_type_constraints; my $other_type_constraint = shift @other_type_constraints; - + $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_) for $self_type_constraint, $other_type_constraint; - + return 1 if $self_type_constraint->$op($other_type_constraint); } return 0; } -=head2 get_message +=method get_message Give you a better peek into what's causing the error. For now we stringify the incoming deep value with L and pass that on to either your @@ -345,15 +329,6 @@ The following modules or resources may be of interest. L, L -=head1 AUTHOR - -John Napiorkowski, C<< >> - -=head1 COPYRIGHT & LICENSE - -This program is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - =cut __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/MooseX/Types/Structured.pm b/lib/MooseX/Types/Structured.pm index b2ea0af..214dcd9 100644 --- a/lib/MooseX/Types/Structured.pm +++ b/lib/MooseX/Types/Structured.pm @@ -1,23 +1,17 @@ package MooseX::Types::Structured; +# ABSTRACT: MooseX::Types::Structured - Structured Type Constraints for Moose use 5.008; -use Moose::Util::TypeConstraints; +use Moose::Util::TypeConstraints 1.06; use MooseX::Meta::TypeConstraint::Structured; use MooseX::Meta::TypeConstraint::Structured::Optional; use MooseX::Types::Structured::OverflowHandler; -use MooseX::Types -declare => [qw(Dict Map Tuple Optional)]; -use Sub::Exporter -setup => [ qw(Dict Map Tuple Optional slurpy) ]; -use Devel::PartialDump; +use MooseX::Types 0.22 -declare => [qw(Dict Map Tuple Optional)]; +use Sub::Exporter 0.982 -setup => [ qw(Dict Map Tuple Optional slurpy) ]; +use Devel::PartialDump 0.10; use Scalar::Util qw(blessed); -our $VERSION = '0.22'; -our $AUTHORITY = 'cpan:JJNAPIORK'; - -=head1 NAME - -MooseX::Types::Structured - Structured Type Constraints for Moose - =head1 SYNOPSIS The following is example usage for this module. @@ -45,7 +39,7 @@ The following is example usage for this module. ], ); - ## Remainder of your class attributes and methods + ## Remainder of your class attributes and methods Then you can instantiate this class with something like: @@ -150,7 +144,7 @@ example: use MooseX::Types -declare [qw(StringIntOptionalHashRef)]; use MooseX::Types::Moose qw(Str Int); - use MooseX::Types::Structured qw(Tuple Optional); + use MooseX::Types::Structured qw(Tuple Optional); subtype StringIntOptionalHashRef, as Tuple[ @@ -175,7 +169,7 @@ Please note the subtle difference between undefined and null. If you wish to allow both null and undefined, you should use the core Moose 'Maybe' type constraint instead: - package MyApp::Types; + package MyApp::Types; use MooseX::Types -declare [qw(StringIntMaybeHashRef)]; use MooseX::Types::Moose qw(Str Int Maybe); @@ -228,24 +222,24 @@ combine various structured, parameterized and simple constraints all together: Which would match: - [1, {name=>'John', age=>25},[10,11,12]]; + [1, {name=>'John', age=>25},[10,11,12]]; Please notice how the type parameters can be visually arranged to your liking and to improve the clarity of your meaning. You don't need to run then altogether onto a single line. Additionally, since the 'Dict' type constraint defines a hash constraint, the key order is not meaningful. For example: - subtype AnyKeyOrder, - as Dict[ - key1=>Int, - key2=>Str, - key3=>Int, - ]; + subtype AnyKeyOrder, + as Dict[ + key1=>Int, + key2=>Str, + key3=>Int, + ]; Would validate both: - {key1 => 1, key2 => "Hi!", key3 => 2}; - {key2 => "Hi!", key1 => 100, key3 => 300}; + {key1 => 1, key2 => "Hi!", key3 => 2}; + {key2 => "Hi!", key1 => 100, key3 => 300}; As you would expect, since underneath its just a plain old Perl hash at work. @@ -420,53 +414,53 @@ Newer versions of L support recursive type constraints. That is you can include a type constraint as a contained type constraint of itself. For example: - subtype Person, - as Dict[ - name=>Str, - friends=>Optional[ - ArrayRef[Person] - ], - ]; + subtype Person, + as Dict[ + name=>Str, + friends=>Optional[ + ArrayRef[Person] + ], + ]; This would declare a Person subtype that contains a name and an optional ArrayRef of Persons who are friends as in: - { - name => 'Mike', - friends => [ - { name => 'John' }, - { name => 'Vincent' }, - { - name => 'Tracey', - friends => [ - { name => 'Stephenie' }, - { name => 'Ilya' }, - ], - }, - ], - }; + { + name => 'Mike', + friends => [ + { name => 'John' }, + { name => 'Vincent' }, + { + name => 'Tracey', + friends => [ + { name => 'Stephenie' }, + { name => 'Ilya' }, + ], + }, + ], + }; Please take care to make sure the recursion node is either Optional, or declare a Union with an non recursive option such as: - subtype Value - as Tuple[ - Str, - Str|Tuple, - ]; + subtype Value + as Tuple[ + Str, + Str|Tuple, + ]; Which validates: - [ - 'Hello', [ - 'World', [ - 'Is', [ - 'Getting', - 'Old', - ], - ], - ], - ]; + [ + 'Hello', [ + 'World', [ + 'Is', [ + 'Getting', + 'Old', + ], + ], + ], + ]; Otherwise you will define a subtype thatis impossible to validate since it is infinitely recursive. For more information about defining recursive types, @@ -635,25 +629,25 @@ other MooseX::Types libraries. subtype Person, as Dict[ - name=>Str, - age=>Int, + name=>Str, + age=>Int, ]; coerce Person, from Dict[ - first=>Str, - last=>Str, - years=>Int, + first=>Str, + last=>Str, + years=>Int, ], via { +{ name => "$_->{first} $_->{last}", age => $_->{years}, }}, from Dict[ - fullname=>Dict[ - last=>Str, - first=>Str, - ], - dob=>DateTime, + fullname=>Dict[ + last=>Str, + first=>Str, + ], + dob=>DateTime, ], ## DateTime needs to be inside of single quotes here to disambiguate the ## class package from the DataTime type constraint imported via the @@ -728,13 +722,13 @@ Moose::Util::TypeConstraints::register_type_constraint($Optional); Moose::Util::TypeConstraints::add_parameterizable_type($Optional); Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( - MooseX::Meta::TypeConstraint::Structured->new( - name => "MooseX::Types::Structured::Tuple" , - parent => find_type_constraint('ArrayRef'), - constraint_generator=> sub { - ## Get the constraints and values to check + MooseX::Meta::TypeConstraint::Structured->new( + name => "MooseX::Types::Structured::Tuple" , + parent => find_type_constraint('ArrayRef'), + constraint_generator=> sub { + ## Get the constraints and values to check my ($type_constraints, $values) = @_; - my @type_constraints = defined $type_constraints ? + my @type_constraints = defined $type_constraints ? @$type_constraints : (); my $overflow_handler; @@ -743,28 +737,28 @@ Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( $overflow_handler = pop @type_constraints; } - my @values = defined $values ? @$values: (); - ## Perform the checking - while(@type_constraints) { - my $type_constraint = shift @type_constraints; - if(@values) { - my $value = shift @values; - unless($type_constraint->check($value)) { + my @values = defined $values ? @$values: (); + ## Perform the checking + while(@type_constraints) { + my $type_constraint = shift @type_constraints; + if(@values) { + my $value = shift @values; + unless($type_constraint->check($value)) { $_[2]->{message} = $type_constraint->get_message($value) if ref $_[2]; - return; - } - } else { + return; + } + } else { ## Test if the TC supports null values unless ($type_constraint->is_subtype_of($Optional)) { $_[2]->{message} = $type_constraint->get_message('NULL') if ref $_[2]; - return; - } - } - } - ## Make sure there are no leftovers. - if(@values) { + return; + } + } + } + ## Make sure there are no leftovers. + if(@values) { if($overflow_handler) { return $overflow_handler->check([@values], $_[2]); } else { @@ -772,26 +766,26 @@ Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( if ref $_[2]; return; } - } elsif(@type_constraints) { + } elsif(@type_constraints) { $_[2]->{message} = "Not enough values for all defined type constraints. Remaining: ". join(', ',@type_constraints) if ref $_[2]; - return; - } else { - return 1; - } - } - ) + return; + } else { + return 1; + } + } + ) ); Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( - MooseX::Meta::TypeConstraint::Structured->new( - name => "MooseX::Types::Structured::Dict", - parent => find_type_constraint('HashRef'), - constraint_generator=> sub { - ## Get the constraints and values to check + MooseX::Meta::TypeConstraint::Structured->new( + name => "MooseX::Types::Structured::Dict", + parent => find_type_constraint('HashRef'), + constraint_generator=> sub { + ## Get the constraints and values to check my ($type_constraints, $values) = @_; - my @type_constraints = defined $type_constraints ? + my @type_constraints = defined $type_constraints ? @$type_constraints : (); my $overflow_handler; @@ -800,30 +794,30 @@ Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( $overflow_handler = pop @type_constraints; } my (%type_constraints) = @type_constraints; - my %values = defined $values ? %$values: (); - ## Perform the checking - while(%type_constraints) { - my($key, $type_constraint) = each %type_constraints; - delete $type_constraints{$key}; - if(exists $values{$key}) { - my $value = $values{$key}; - delete $values{$key}; - unless($type_constraint->check($value)) { + my %values = defined $values ? %$values: (); + ## Perform the checking + while(%type_constraints) { + my($key, $type_constraint) = each %type_constraints; + delete $type_constraints{$key}; + if(exists $values{$key}) { + my $value = $values{$key}; + delete $values{$key}; + unless($type_constraint->check($value)) { $_[2]->{message} = $type_constraint->get_message($value) if ref $_[2]; - return; - } - } else { + return; + } + } else { ## Test to see if the TC supports null values unless ($type_constraint->is_subtype_of($Optional)) { $_[2]->{message} = $type_constraint->get_message('NULL') if ref $_[2]; - return; - } - } - } - ## Make sure there are no leftovers. - if(%values) { + return; + } + } + } + ## Make sure there are no leftovers. + if(%values) { if($overflow_handler) { return $overflow_handler->check(+{%values}); } else { @@ -831,16 +825,16 @@ Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( if ref $_[2]; return; } - } elsif(%type_constraints) { + } elsif(%type_constraints) { $_[2]->{message} = "Not enough values for all defined type constraints. Remaining: ". join(', ',values %values) if ref $_[2]; - return; - } else { - return 1; - } - }, - ) + return; + } else { + return 1; + } + }, + ) ); Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( @@ -884,8 +878,8 @@ Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( ); sub slurpy ($) { - my ($tc) = @_; - return MooseX::Types::Structured::OverflowHandler->new( + my ($tc) = @_; + return MooseX::Types::Structured::OverflowHandler->new( type_constraint => $tc, ); } @@ -901,30 +895,10 @@ L Here's a list of stuff I would be happy to get volunteers helping with: - * All POD examples need test cases in t/documentation/*.t - * Want to break out the examples section to a separate cookbook style POD. - * Want more examples and best practice / usage guidance for authors - * Need to clarify deep coercions, - -=head1 AUTHOR - -John Napiorkowski - -=head1 CONTRIBUTORS - -The following people have contributed to this module and agree with the listed -Copyright & license information included below: - - Florian Ragwitz, - Yuval Kogman, - Tomas Doran, - -=head1 COPYRIGHT & LICENSE - -Copyright 2008-2009, John Napiorkowski - -This program is free software; you can redistribute it and/or modify it under -the same terms as Perl itself. + * All POD examples need test cases in t/documentation/*.t + * Want to break out the examples section to a separate cookbook style POD. + * Want more examples and best practice / usage guidance for authors + * Need to clarify deep coercions, =cut diff --git a/lib/MooseX/Types/Structured/OverflowHandler.pm b/lib/MooseX/Types/Structured/OverflowHandler.pm index 20b9f7a..98ccb80 100644 --- a/lib/MooseX/Types/Structured/OverflowHandler.pm +++ b/lib/MooseX/Types/Structured/OverflowHandler.pm @@ -4,6 +4,10 @@ use Moose; use overload '""' => 'name', fallback => 1; +=attr type_constraint + +=cut + has type_constraint => ( is => 'ro', isa => 'Moose::Meta::TypeConstraint', @@ -11,6 +15,10 @@ has type_constraint => ( handles => [qw/check/], ); +=method name + +=cut + sub name { my ($self) = @_; return 'slurpy ' . $self->type_constraint->name; diff --git a/t-author/pod-coverage.t b/t-author/pod-coverage.t deleted file mode 100644 index fc40a57..0000000 --- a/t-author/pod-coverage.t +++ /dev/null @@ -1,18 +0,0 @@ -use strict; -use warnings; -use Test::More; - -# Ensure a recent version of Test::Pod::Coverage -my $min_tpc = 1.08; -eval "use Test::Pod::Coverage $min_tpc"; -plan skip_all => "Test::Pod::Coverage $min_tpc required for testing POD coverage" - if $@; - -# Test::Pod::Coverage doesn't require a minimum Pod::Coverage version, -# but older versions don't recognize some common documentation styles -my $min_pc = 0.18; -eval "use Pod::Coverage $min_pc"; -plan skip_all => "Pod::Coverage $min_pc required for testing POD coverage" - if $@; - -all_pod_coverage_ok(); diff --git a/t-author/pod.t b/t-author/pod.t deleted file mode 100644 index 056e192..0000000 --- a/t-author/pod.t +++ /dev/null @@ -1,10 +0,0 @@ -use strict; -use warnings; -use Test::More; - -# Ensure a recent version of Test::Pod -my $min_tp = 1.22; -eval "use Test::Pod $min_tp"; -plan skip_all => "Test::Pod $min_tp required for testing POD" if $@; - -all_pod_files_ok(); diff --git a/t/02-tuple.t b/t/02-tuple.t index 1acd950..68718aa 100644 --- a/t/02-tuple.t +++ b/t/02-tuple.t @@ -1,8 +1,8 @@ BEGIN { - use strict; - use warnings; - use Test::More tests=>32; - use Test::Exception; + use strict; + use warnings; + use Test::More tests=>32; + use Test::Exception; } { @@ -10,33 +10,33 @@ BEGIN { use Moose; use MooseX::Types::Structured qw(Tuple); - use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); - use MooseX::Types -declare => [qw(MyString MoreThanFive FiveByFive MyArrayRefMoreThanTwoInt)]; - + use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); + use MooseX::Types -declare => [qw(MyString MoreThanFive FiveByFive MyArrayRefMoreThanTwoInt)]; + subtype MyString, as Str, where { $_=~m/abc/}; - + subtype MoreThanFive, as Int, where { $_ > 5}; - + subtype MyArrayRefMoreThanTwoInt, as ArrayRef[MoreThanFive], where { scalar @$_ > 2 }; - + subtype FiveByFive, as Tuple[MoreThanFive, MyArrayRefMoreThanTwoInt]; - + #use Data::Dump qw/dump/; warn dump Tuple; has 'tuple' => (is=>'rw', isa=>Tuple[Int, Str, MyString]); - has 'tuple_with_param' => (is=>'rw', isa=>Tuple[Int, Str, ArrayRef[Int]]); - has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int], Object]); - has 'tuple_with_maybe2' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int]]); - has 'tuple_with_union' => (is=>'rw', isa=>Tuple[Int,Str,Int|Object,Int]); - has 'tuple2' => (is=>'rw', isa=>Tuple[Int,Str,Int]); - has 'tuple_with_parameterized' => (is=>'rw', isa=>Tuple[Int,Str,Int,ArrayRef[Int]]); + has 'tuple_with_param' => (is=>'rw', isa=>Tuple[Int, Str, ArrayRef[Int]]); + has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int], Object]); + has 'tuple_with_maybe2' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int]]); + has 'tuple_with_union' => (is=>'rw', isa=>Tuple[Int,Str,Int|Object,Int]); + has 'tuple2' => (is=>'rw', isa=>Tuple[Int,Str,Int]); + has 'tuple_with_parameterized' => (is=>'rw', isa=>Tuple[Int,Str,Int,ArrayRef[Int]]); has 'FiveByFiveAttr' => (is=>'rw', isa=>FiveByFive); } @@ -44,10 +44,10 @@ BEGIN { ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Tuple->new => 'Instantiated new Record test class.'; - + isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Tuple' => 'Created correct object type.'; - + ## Test Tuple type constraint lives_ok sub { @@ -64,12 +64,12 @@ is $record->tuple->[2], 'test.abc.test' => 'correct set the tuple attribute index 2'; throws_ok sub { - $record->tuple([1,'hello', 'test.xxx.test']); + $record->tuple([1,'hello', 'test.xxx.test']); }, qr/Attribute \(tuple\) does not pass the type constraint/ => 'Properly failed for bad value in custom type constraint'; - + throws_ok sub { - $record->tuple(['asdasd',2, 'test.abc.test']); + $record->tuple(['asdasd',2, 'test.abc.test']); }, qr/Attribute \(tuple\) does not pass the type constraint/ => 'Got Expected Error for violating constraints'; @@ -128,15 +128,15 @@ ok $record->tuple2([1,'hello',3]) => "[1,'hello',3] properly suceeds"; throws_ok sub { - $record->tuple2([1,2,'world']); + $record->tuple2([1,2,'world']); }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "[1,2,'world'] properly fails"; throws_ok sub { - $record->tuple2(['hello1',2,3]); + $record->tuple2(['hello1',2,3]); }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,3] properly fails"; throws_ok sub { - $record->tuple2(['hello2',2,'world']); + $record->tuple2(['hello2',2,'world']); }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,'world'] properly fails"; @@ -146,22 +146,22 @@ ok $record->tuple_with_parameterized([1,'hello',3,[1,2,3]]) => "[1,'hello',3,[1,2,3]] properly suceeds"; throws_ok sub { - $record->tuple_with_parameterized([1,2,'world']); + $record->tuple_with_parameterized([1,2,'world']); }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/ => "[1,2,'world'] properly fails"; throws_ok sub { - $record->tuple_with_parameterized(['hello1',2,3]); + $record->tuple_with_parameterized(['hello1',2,3]); }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/ => "['hello',2,3] properly fails"; throws_ok sub { - $record->tuple_with_parameterized(['hello2',2,'world']); + $record->tuple_with_parameterized(['hello2',2,'world']); }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/ => "['hello',2,'world'] properly fails"; throws_ok sub { - $record->tuple_with_parameterized([1,'hello',3,[1,2,'world']]); + $record->tuple_with_parameterized([1,'hello',3,[1,2,'world']]); }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/ => "[1,'hello',3,[1,2,'world']] properly fails"; @@ -172,27 +172,27 @@ lives_ok sub { } => 'Set FiveByFiveAttr correctly'; throws_ok sub { - $record->FiveByFiveAttr([1,'hello', 'test']); + $record->FiveByFiveAttr([1,'hello', 'test']); }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/ => q{Properly failed for bad value in FiveByFiveAttr [1,'hello', 'test']}; throws_ok sub { - $record->FiveByFiveAttr([1,[8,9,10]]); + $record->FiveByFiveAttr([1,[8,9,10]]); }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/ => q{Properly failed for bad value in FiveByFiveAttr [1,[8,9,10]]}; - + throws_ok sub { - $record->FiveByFiveAttr([10,[11,12,0]]); + $record->FiveByFiveAttr([10,[11,12,0]]); }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/ => q{Properly failed for bad value in FiveByFiveAttr [10,[11,12,0]]}; - + throws_ok sub { - $record->FiveByFiveAttr([1,[1,1,0]]); + $record->FiveByFiveAttr([1,[1,1,0]]); }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/ => q{Properly failed for bad value in FiveByFiveAttr [1,[1,1,0]]}; throws_ok sub { - $record->FiveByFiveAttr([10,[11,12]]); + $record->FiveByFiveAttr([10,[11,12]]); }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/ => q{Properly failed for bad value in FiveByFiveAttr [10,[11,12]}; - + diff --git a/t/03-dict.t b/t/03-dict.t index 1f4780e..79176ab 100644 --- a/t/03-dict.t +++ b/t/03-dict.t @@ -1,8 +1,8 @@ BEGIN { - use strict; - use warnings; - use Test::More tests=>17; - use Test::Exception; + use strict; + use warnings; + use Test::More tests=>17; + use Test::Exception; } { @@ -10,15 +10,15 @@ BEGIN { use Moose; use MooseX::Types::Structured qw(Dict Tuple); - use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); - use MooseX::Types -declare => [qw(MyString)]; - + use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); + use MooseX::Types -declare => [qw(MyString)]; + subtype MyString, as Str, where { $_=~m/abc/}; - + has 'dict' => (is=>'rw', isa=>Dict[name=>Str, age=>Int]); - has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>Str, age=>Maybe[Int]]); + has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>Str, age=>Maybe[Int]]); has 'dict_with_tuple_with_union' => (is=>'rw', isa=>Dict[key1=>Str|Object, key2=>Tuple[Int,Str|Object]] ); } @@ -26,12 +26,12 @@ BEGIN { ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Dict->new => 'Instantiated new Record test class.'; - + isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Dict' => 'Created correct object type.'; - + # Test dict Dict[name=>Str, age=>Int] - + lives_ok sub { $record->dict({name=>'frith', age=>23}); } => 'Set dict attribute without error'; @@ -41,12 +41,12 @@ is $record->dict->{name}, 'frith' is $record->dict->{age}, 23 => 'correct set the dict attribute age'; - + throws_ok sub { - $record->dict({name=>[1,2,3], age=>'sdfsdfsd'}); + $record->dict({name=>[1,2,3], age=>'sdfsdfsd'}); }, qr/Attribute \(dict\) does not pass the type constraint/ => 'Got Expected Error for bad value in dict'; - + ## Test dict_with_maybe lives_ok sub { @@ -58,14 +58,14 @@ is $record->dict_with_maybe->{name}, 'frith' is $record->dict_with_maybe->{age}, 23 => 'correct set the dict attribute age'; - + throws_ok sub { - $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'}); + $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'}); }, qr/Attribute \(dict_with_maybe\) does not pass the type constraint/ => 'Got Expected Error for bad value in dict'; throws_ok sub { - $record->dict_with_maybe({age=>30}); + $record->dict_with_maybe({age=>30}); }, qr/Attribute \(dict_with_maybe\) does not pass the type constraint/ => 'Got Expected Error for missing named parameter'; @@ -83,7 +83,7 @@ throws_ok sub { $record->dict_with_tuple_with_union({key1=>'Hello', key2=>['World',2]}); }, qr/Attribute \(dict_with_tuple_with_union\) does not pass the type constraint/ => 'Threw error on bad constraint'; - + lives_ok sub { $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']}); } => 'Set tuple attribute without error'; diff --git a/t/04-combined.t b/t/04-combined.t index 242d2b5..2d2389b 100644 --- a/t/04-combined.t +++ b/t/04-combined.t @@ -1,8 +1,8 @@ BEGIN { - use strict; - use warnings; - use Test::More tests=>9; - use Test::Exception; + use strict; + use warnings; + use Test::More tests=>9; + use Test::Exception; } { @@ -10,9 +10,9 @@ BEGIN { use Moose; use MooseX::Types::Structured qw(Dict Tuple); - use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); - - has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>Str, key2=>Tuple[Int,Str]]); + use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); + + has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>Str, key2=>Tuple[Int,Str]]); has 'dict_with_tuple_with_union' => (is=>'rw', isa=>Dict[key1=>Str|Object, key2=>Tuple[Int,Str|Object]] ); } @@ -20,10 +20,10 @@ BEGIN { ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Combined->new => 'Instantiated new Record test class.'; - + isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Combined' => 'Created correct object type.'; - + ## Test dict_with_tuple lives_ok sub { @@ -34,7 +34,7 @@ throws_ok sub { $record->dict_with_tuple({key1=>'Hello', key2=>['World',2]}); }, qr/Attribute \(dict_with_tuple\) does not pass the type constraint/ => 'Threw error on bad constraint'; - + ## Test dict_with_tuple_with_union: Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']] lives_ok sub { @@ -45,7 +45,7 @@ throws_ok sub { $record->dict_with_tuple_with_union({key1=>'Hello', key2=>['World',2]}); }, qr/Attribute \(dict_with_tuple_with_union\) does not pass the type constraint/ => 'Threw error on bad constraint'; - + lives_ok sub { $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']}); } => 'Set tuple attribute without error'; diff --git a/t/05-advanced.t b/t/05-advanced.t index 76cd134..34ec60a 100644 --- a/t/05-advanced.t +++ b/t/05-advanced.t @@ -1,8 +1,8 @@ BEGIN { - use strict; - use warnings; - use Test::More tests=>16; - use Test::Exception; + use strict; + use warnings; + use Test::More tests=>16; + use Test::Exception; } { @@ -10,21 +10,21 @@ BEGIN { use Moose; use MooseX::Types::Structured qw(Dict Tuple); - use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); - use MooseX::Types -declare => [qw( + use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); + use MooseX::Types -declare => [qw( EqualLength MoreThanFive MoreLengthPlease PersonalInfo MorePersonalInfo MinFiveChars )]; - + subtype MoreThanFive, as Int, where { $_ > 5}; - + ## Tuple contains two equal length Arrays subtype EqualLength, as Tuple[ArrayRef[MoreThanFive],ArrayRef[MoreThanFive]], where { $#{$_->[0]} == $#{$_->[1]} }; - + ## subclass the complex tuple subtype MoreLengthPlease, as EqualLength, @@ -33,16 +33,16 @@ BEGIN { ## Complexe Dict subtype PersonalInfo, as Dict[name=>Str, stats=>MoreLengthPlease|Object]; - + ## Minimum 5 char string subtype MinFiveChars, as Str, - where { length($_) > 5}; - + where { length($_) > 5}; + ## Dict key overloading subtype MorePersonalInfo, as PersonalInfo[name=>MinFiveChars, stats=>MoreLengthPlease|Object]; - + has 'EqualLengthAttr' => (is=>'rw', isa=>EqualLength); has 'MoreLengthPleaseAttr' => (is=>'rw', isa=>MoreLengthPlease); has 'PersonalInfoAttr' => (is=>'rw', isa=>PersonalInfo); @@ -53,10 +53,10 @@ BEGIN { ok my $obj = Test::MooseX::Meta::TypeConstraint::Structured::Advanced->new => 'Instantiated new Record test class.'; - + isa_ok $obj => 'Test::MooseX::Meta::TypeConstraint::Structured::Advanced' => 'Created correct object type.'; - + ## Test EqualLengthAttr lives_ok sub { @@ -64,20 +64,20 @@ lives_ok sub { } => 'Set EqualLengthAttr attribute without error'; throws_ok sub { - $obj->EqualLengthAttr([1,'hello', 'test.xxx.test']); + $obj->EqualLengthAttr([1,'hello', 'test.xxx.test']); }, qr/Attribute \(EqualLengthAttr\) does not pass the type constraint/ => q{EqualLengthAttr correctly fails [1,'hello', 'test.xxx.test']}; - + throws_ok sub { - $obj->EqualLengthAttr([[6,7],[9,10,11]]); + $obj->EqualLengthAttr([[6,7],[9,10,11]]); }, qr/Attribute \(EqualLengthAttr\) does not pass the type constraint/ => q{EqualLengthAttr correctly fails [[6,7],[9,10,11]]}; - + throws_ok sub { - $obj->EqualLengthAttr([[6,7,1],[9,10,11]]); + $obj->EqualLengthAttr([[6,7,1],[9,10,11]]); }, qr/Attribute \(EqualLengthAttr\) does not pass the type constraint/ => q{EqualLengthAttr correctly fails [[6,7,1],[9,10,11]]}; - + ## Test MoreLengthPleaseAttr lives_ok sub { @@ -85,10 +85,10 @@ lives_ok sub { } => 'Set MoreLengthPleaseAttr attribute without error'; throws_ok sub { - $obj->MoreLengthPleaseAttr([[6,7,8,9],[11,12,13,14]]); + $obj->MoreLengthPleaseAttr([[6,7,8,9],[11,12,13,14]]); }, qr/Attribute \(MoreLengthPleaseAttr\) does not pass the type constraint/ => q{MoreLengthPleaseAttr correctly fails [[6,7,8,9],[11,12,13,14]]}; - + ## Test PersonalInfoAttr lives_ok sub { @@ -100,12 +100,12 @@ lives_ok sub { } => 'Set PersonalInfoAttr attribute without error 2'; throws_ok sub { - $obj->PersonalInfoAttr({name=>'John', stats=>[[6,7,8,9],[11,12,13,14]]}); + $obj->PersonalInfoAttr({name=>'John', stats=>[[6,7,8,9],[11,12,13,14]]}); }, qr/Attribute \(PersonalInfoAttr\) does not pass the type constraint/ => q{PersonalInfoAttr correctly fails name=>'John', stats=>[[6,7,8,9],[11,12,13,14]]}; throws_ok sub { - $obj->PersonalInfoAttr({name=>'John', extra=>1, stats=>[[6,7,8,9,10],[11,12,13,14,15]]}); + $obj->PersonalInfoAttr({name=>'John', extra=>1, stats=>[[6,7,8,9,10],[11,12,13,14,15]]}); }, qr/Attribute \(PersonalInfoAttr\) does not pass the type constraint/ => q{PersonalInfoAttr correctly fails name=>'John', extra=>1, stats=>[[6,7,8,9,10],[11,12,13,14,15]]}; @@ -116,18 +116,18 @@ lives_ok sub { } => 'Set MorePersonalInfo attribute without error 1'; throws_ok sub { - $obj->MorePersonalInfo({name=>'Johnnap', stats=>[[6,7,8,9],[11,12,13,14]]}); + $obj->MorePersonalInfo({name=>'Johnnap', stats=>[[6,7,8,9],[11,12,13,14]]}); }, qr/Attribute \(MorePersonalInfo\) does not pass the type constraint/ => q{MorePersonalInfo correctly fails name=>'Johnnap', stats=>[[6,7,8,9],[11,12,13,14]]}; throws_ok sub { - $obj->MorePersonalInfo({name=>'Johnnap', extra=>1, stats=>[[6,7,8,9,10],[11,12,13,14,15]]}); + $obj->MorePersonalInfo({name=>'Johnnap', extra=>1, stats=>[[6,7,8,9,10],[11,12,13,14,15]]}); }, qr/Attribute \(MorePersonalInfo\) does not pass the type constraint/ => q{MorePersonalInfo correctly fails name=>'Johnnap', extra=>1, stats=>[[6,7,8,9,10],[11,12,13,14,15]]}; throws_ok sub { - $obj->MorePersonalInfo({name=>'.bc', stats=>[[6,7,8,9,10],[11,12,13,14,15]]}); + $obj->MorePersonalInfo({name=>'.bc', stats=>[[6,7,8,9,10],[11,12,13,14,15]]}); }, qr/Attribute \(MorePersonalInfo\) does not pass the type constraint/ - => q{MorePersonalInfo correctly fails name=>'.bc', stats=>[[6,7,8,9,10],[11,12,13,14,15]]}; + => q{MorePersonalInfo correctly fails name=>'.bc', stats=>[[6,7,8,9,10],[11,12,13,14,15]]}; diff --git a/t/06-api.t b/t/06-api.t index 5e0de0f..37e09af 100644 --- a/t/06-api.t +++ b/t/06-api.t @@ -1,7 +1,7 @@ BEGIN { - use strict; - use warnings; - use Test::More tests=>88; + use strict; + use warnings; + use Test::More tests=>88; } { @@ -153,19 +153,19 @@ ok (!MyTuple2->is_subtype_of(MyTuple3), 'MyTuple2 NOT is_subtype_of MyTuple3'); PARAMETERIZE: { ok (my $int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Int'), 'Got Int'); - ok (my $str = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Str'), 'Got Str'); - ok (my $hashref = Moose::Util::TypeConstraints::find_or_parse_type_constraint('HashRef[Int]'), 'Got HashRef'); + ok (my $str = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Str'), 'Got Str'); + ok (my $hashref = Moose::Util::TypeConstraints::find_or_parse_type_constraint('HashRef[Int]'), 'Got HashRef'); ## Test Dict->parameterize ok (my $test_dict = Dict(), 'Created Test Dict'); ok (my $person = $test_dict->parameterize(name=>$str, age=>$int), 'Parameterized It'); ok ($person->check({name=>'John', age=>21}), 'Passed'); ok ($person->check({age=>25, name=>'User'}), 'Passed'); - + ## Test Tuple->parameterize ok (my $test_tuple = Tuple(), 'Created Test Tuple'); ok (my $int_and_hashref = $test_tuple->parameterize($int, $hashref), 'Parameterized It'); ok ($int_and_hashref->check([1, {key=>2, key2=>3}]), "Passed"); - ok (!$int_and_hashref->check(['a', {key=>2, key2=>3}]), "Not Passed"); + ok (!$int_and_hashref->check(['a', {key=>2, key2=>3}]), "Not Passed"); ok (!$int_and_hashref->check([1, {key=>'a', key2=>3}]), "Not Passed"); } diff --git a/t/07-coerce.t b/t/07-coerce.t index 7433acd..e24ebb7 100644 --- a/t/07-coerce.t +++ b/t/07-coerce.t @@ -1,8 +1,8 @@ BEGIN { - use strict; - use warnings; - use Test::More tests=>16; - use Test::Exception; + use strict; + use warnings; + use Test::More tests=>16; + use Test::Exception; } { @@ -10,28 +10,28 @@ BEGIN { use Moose; use MooseX::Types::Structured qw(Dict Tuple); - use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef); - use MooseX::Types -declare => [qw( + use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef); + use MooseX::Types -declare => [qw( myDict myTuple Fullname )]; - + subtype myDict, as Dict[name=>Str, age=>Int]; - + subtype Fullname, as Dict[first=>Str, last=>Str]; - + coerce Fullname, from ArrayRef, via { +{first=>$_->[0], last=>$_->[1]} }; - + subtype myTuple, as Tuple[Str, Int]; ## Create some coercions. Note the dob_epoch could be a more useful convert ## from a dob datetime object, I'm just lazy. - + coerce myDict, from Int, via { +{name=>'JohnDoe', age=>$_} }, @@ -71,12 +71,12 @@ is_deeply $person->stuff, {name=>"JohnDoe",age=>30}, 'Correct set'; ok $person->stuff({aname=>{first=>"frank", last=>"herbert"},dob_in_years=>80}), '{{first=>"frank", last=>"herbert"},80}'; - + is_deeply $person->stuff, {name=>"frank herbert",age=>80}, 'Correct set'; ok $person->stuff({bname=>{first=>"frankbbb", last=>"herbert"},dob_in_years=>84}), '{{first=>"frankbbb", last=>"herbert"},84}'; - + is_deeply $person->stuff, {name=>"frankbbb herbert",age=>84}, 'Correct set'; ok $person->stuff(["mary",40]), 'Set Stuff ["mary",40]'; @@ -84,16 +84,16 @@ is_deeply $person->stuff, {name=>"mary",age=>40}, 'Correct set'; ok $person->stuff({fullname=>{first=>"frank", last=>"herbert1"},dob_epoch=>85}), '{{first=>"frank", last=>"herbert1"},85}'; - + is_deeply $person->stuff, {name=>"frank herbert1",age=>85}, 'Correct set'; SKIP: { skip 'deep coercions not yet supported', 2, 1; - + ok $person->stuff({fullname=>["frank", "herbert2"],dob_epoch=>86}), '{fullname=>["frank", "herbert2"],dob_epoch=>86}'; - - is_deeply $person->stuff, {name=>"frank herbert2",age=>86}, 'Correct set'; + + is_deeply $person->stuff, {name=>"frank herbert2",age=>86}, 'Correct set'; } diff --git a/t/08-examples.t b/t/08-examples.t index 50208b1..14de38f 100644 --- a/t/08-examples.t +++ b/t/08-examples.t @@ -1,8 +1,8 @@ BEGIN { - use strict; - use warnings; - use Test::More; - + use strict; + use warnings; + use Test::More; + eval "use MooseX::Types::DateTime"; plan $@ ? ( skip_all => "Tests require MooseX::Types::DateTime" ) @@ -10,70 +10,70 @@ BEGIN { } { - ## Normalize a HashRef + ## Normalize a HashRef package Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize; use Moose; - use DateTime; + use DateTime; use MooseX::Types::Structured qw(Dict Tuple); - use MooseX::Types::DateTime qw(DateTime); - use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef); - use MooseX::Types -declare => [qw( + use MooseX::Types::DateTime qw(DateTime); + use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef); + use MooseX::Types -declare => [qw( Name Age Person FullName )]; - - ## So that our test works, we'll set Now to 2008. - sub Now { - return 'DateTime'->new(year=>2008); - } - - subtype FullName, - as Dict[last=>Str, first=>Str]; - + + ## So that our test works, we'll set Now to 2008. + sub Now { + return 'DateTime'->new(year=>2008); + } + + subtype FullName, + as Dict[last=>Str, first=>Str]; + subtype Person, - as Dict[name=>Str, age=>Int]; - - coerce Person, - from Dict[first=>Str, last=>Str, years=>Int], - via { +{ - name => "$_->{first} $_->{last}", - age=>$_->{years}, - }}, - from Dict[fullname=>FullName, dob=>DateTime], - via { +{ - name => "$_->{fullname}{first} $_->{fullname}{last}", - age => ($_->{dob} - Now)->years, - }}; - - has person => (is=>'rw', isa=>Person, coerce=>1); + as Dict[name=>Str, age=>Int]; + + coerce Person, + from Dict[first=>Str, last=>Str, years=>Int], + via { +{ + name => "$_->{first} $_->{last}", + age=>$_->{years}, + }}, + from Dict[fullname=>FullName, dob=>DateTime], + via { +{ + name => "$_->{fullname}{first} $_->{fullname}{last}", + age => ($_->{dob} - Now)->years, + }}; + + has person => (is=>'rw', isa=>Person, coerce=>1); } NORMALIZE: { - ok my $normalize = Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize->new(); - isa_ok $normalize, 'Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize'; - - ok $normalize->person({name=>'John', age=>25}) - => 'Set value'; - - is_deeply $normalize->person, {name=>'John', age=>25} - => 'Value is correct'; - - ok $normalize->person({first=>'John', last=>'Napiorkowski', years=>35}) - => 'Set value'; - - is_deeply $normalize->person, {name=>'John Napiorkowski', age=>35} - => 'Value is correct'; - - ok $normalize->person({years=>36, last=>'Napiorkowski', first=>'John'}) - => 'Set value'; - - is_deeply $normalize->person, {name=>'John Napiorkowski', age=>36} - => 'Value is correct'; - - ok $normalize->person({fullname=>{first=>'Vanessa', last=>'Li'}, dob=>DateTime->new(year=>1974)}) - => 'Set value'; - - is_deeply $normalize->person, {name=>'Vanessa Li', age=>34} - => 'Value is correct'; + ok my $normalize = Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize->new(); + isa_ok $normalize, 'Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize'; + + ok $normalize->person({name=>'John', age=>25}) + => 'Set value'; + + is_deeply $normalize->person, {name=>'John', age=>25} + => 'Value is correct'; + + ok $normalize->person({first=>'John', last=>'Napiorkowski', years=>35}) + => 'Set value'; + + is_deeply $normalize->person, {name=>'John Napiorkowski', age=>35} + => 'Value is correct'; + + ok $normalize->person({years=>36, last=>'Napiorkowski', first=>'John'}) + => 'Set value'; + + is_deeply $normalize->person, {name=>'John Napiorkowski', age=>36} + => 'Value is correct'; + + ok $normalize->person({fullname=>{first=>'Vanessa', last=>'Li'}, dob=>DateTime->new(year=>1974)}) + => 'Set value'; + + is_deeply $normalize->person, {name=>'Vanessa Li', age=>34} + => 'Value is correct'; } diff --git a/t/09-optional.t b/t/09-optional.t index 7079f4b..03d0b94 100755 --- a/t/09-optional.t +++ b/t/09-optional.t @@ -8,51 +8,51 @@ use MooseX::Types::Structured qw(Optional); APITEST: { - ok my $Optional = Moose::Util::TypeConstraints::find_or_parse_type_constraint('MooseX::Types::Structured::Optional') - => 'Got Optional'; - - isa_ok $Optional - => 'Moose::Meta::TypeConstraint::Parameterizable'; - - ok my $int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Int') - => 'Got Int'; - - ok my $arrayref = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Int]') - => 'Got ArrayRef[Int]'; - - BASIC: { - ok my $Optional_Int = $Optional->parameterize($int), 'Parameterized Int'; - ok my $Optional_ArrayRef = $Optional->parameterize($arrayref), 'Parameterized ArrayRef'; - - ok $Optional_Int->check() => 'Optional is allowed to not exist'; - - ok !$Optional_Int->check(undef) => 'Optional is NOT allowed to be undef'; - ok $Optional_Int->check(199) => 'Correctly validates 199'; - ok !$Optional_Int->check("a") => 'Correctly fails "a"'; - - ok $Optional_ArrayRef->check() => 'Optional is allowed to not exist'; - ok !$Optional_ArrayRef->check(undef) => 'Optional is NOT allowed to be undef'; - ok $Optional_ArrayRef->check([1,2,3]) => 'Correctly validates [1,2,3]'; - ok !$Optional_ArrayRef->check("a") => 'Correctly fails "a"'; - ok !$Optional_ArrayRef->check(["a","b"]) => 'Correctly fails ["a","b"]'; - } - - SUBREF: { - ok my $Optional_Int = Optional->parameterize($int),'Parameterized Int'; - ok my $Optional_ArrayRef = Optional->parameterize($arrayref), 'Parameterized ArrayRef'; - - ok $Optional_Int->check() => 'Optional is allowed to not exist'; - - ok !$Optional_Int->check(undef) => 'Optional is NOT allowed to be undef'; - ok $Optional_Int->check(199) => 'Correctly validates 199'; - ok !$Optional_Int->check("a") => 'Correctly fails "a"'; - - ok $Optional_ArrayRef->check() => 'Optional is allowed to not exist'; - ok !$Optional_ArrayRef->check(undef) => 'Optional is NOT allowed to be undef'; - ok $Optional_ArrayRef->check([1,2,3]) => 'Correctly validates [1,2,3]'; - ok !$Optional_ArrayRef->check("a") => 'Correctly fails "a"'; - ok !$Optional_ArrayRef->check(["a","b"]) => 'Correctly fails ["a","b"]'; - } + ok my $Optional = Moose::Util::TypeConstraints::find_or_parse_type_constraint('MooseX::Types::Structured::Optional') + => 'Got Optional'; + + isa_ok $Optional + => 'Moose::Meta::TypeConstraint::Parameterizable'; + + ok my $int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Int') + => 'Got Int'; + + ok my $arrayref = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Int]') + => 'Got ArrayRef[Int]'; + + BASIC: { + ok my $Optional_Int = $Optional->parameterize($int), 'Parameterized Int'; + ok my $Optional_ArrayRef = $Optional->parameterize($arrayref), 'Parameterized ArrayRef'; + + ok $Optional_Int->check() => 'Optional is allowed to not exist'; + + ok !$Optional_Int->check(undef) => 'Optional is NOT allowed to be undef'; + ok $Optional_Int->check(199) => 'Correctly validates 199'; + ok !$Optional_Int->check("a") => 'Correctly fails "a"'; + + ok $Optional_ArrayRef->check() => 'Optional is allowed to not exist'; + ok !$Optional_ArrayRef->check(undef) => 'Optional is NOT allowed to be undef'; + ok $Optional_ArrayRef->check([1,2,3]) => 'Correctly validates [1,2,3]'; + ok !$Optional_ArrayRef->check("a") => 'Correctly fails "a"'; + ok !$Optional_ArrayRef->check(["a","b"]) => 'Correctly fails ["a","b"]'; + } + + SUBREF: { + ok my $Optional_Int = Optional->parameterize($int),'Parameterized Int'; + ok my $Optional_ArrayRef = Optional->parameterize($arrayref), 'Parameterized ArrayRef'; + + ok $Optional_Int->check() => 'Optional is allowed to not exist'; + + ok !$Optional_Int->check(undef) => 'Optional is NOT allowed to be undef'; + ok $Optional_Int->check(199) => 'Correctly validates 199'; + ok !$Optional_Int->check("a") => 'Correctly fails "a"'; + + ok $Optional_ArrayRef->check() => 'Optional is allowed to not exist'; + ok !$Optional_ArrayRef->check(undef) => 'Optional is NOT allowed to be undef'; + ok $Optional_ArrayRef->check([1,2,3]) => 'Correctly validates [1,2,3]'; + ok !$Optional_ArrayRef->check("a") => 'Correctly fails "a"'; + ok !$Optional_ArrayRef->check(["a","b"]) => 'Correctly fails ["a","b"]'; + } } OBJECTTEST: { @@ -60,47 +60,47 @@ OBJECTTEST: { use Moose; use MooseX::Types::Structured qw(Dict Tuple Optional); - use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); - use MooseX::Types -declare => [qw( + use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); + use MooseX::Types -declare => [qw( MoreThanFive TupleOptional1 TupleOptional2 Gender DictOptional1 Insane )]; - + subtype MoreThanFive, as Int, where { $_ > 5}; - - enum Gender, - qw/male female transgendered/; - + + enum Gender, + qw/male female transgendered/; + subtype TupleOptional1() => - as Tuple[Int, MoreThanFive, Optional[Str|Object]]; + as Tuple[Int, MoreThanFive, Optional[Str|Object]]; subtype TupleOptional2, - as Tuple[Int, MoreThanFive, Optional[HashRef[Int|Object]]]; - - subtype DictOptional1, - as Dict[name=>Str, age=>Int, gender=>Optional[Gender]]; - - subtype Insane, - as Tuple[ - Int, - Optional[Str|Object], - DictOptional1, - Optional[ArrayRef[Int]] - ]; - + as Tuple[Int, MoreThanFive, Optional[HashRef[Int|Object]]]; + + subtype DictOptional1, + as Dict[name=>Str, age=>Int, gender=>Optional[Gender]]; + + subtype Insane, + as Tuple[ + Int, + Optional[Str|Object], + DictOptional1, + Optional[ArrayRef[Int]] + ]; + has 'TupleOptional1Attr' => (is=>'rw', isa=>TupleOptional1); has 'TupleOptional2Attr' => (is=>'rw', isa=>TupleOptional2); - has 'DictOptional1Attr' => (is=>'rw', isa=>DictOptional1); - has 'InsaneAttr' => (is=>'rw', isa=>Insane); + has 'DictOptional1Attr' => (is=>'rw', isa=>DictOptional1); + has 'InsaneAttr' => (is=>'rw', isa=>Insane); } ok my $obj = Test::MooseX::Meta::TypeConstraint::Structured::Optional->new => 'Instantiated new test class.'; - + isa_ok $obj => 'Test::MooseX::Meta::TypeConstraint::Structured::Optional' => 'Created correct object type.'; - + # Test Insane lives_ok sub { @@ -116,15 +116,15 @@ lives_ok sub { } => 'Set InsaneAttr attribute without error [1,$obj,{name=>"John",age=>39}]'; throws_ok sub { - $obj->InsaneAttr([1,$obj,{name=>"John",age=>39},[qw/a b c/]]); + $obj->InsaneAttr([1,$obj,{name=>"John",age=>39},[qw/a b c/]]); }, qr/Attribute \(InsaneAttr\) does not pass the type constraint/ => q{InsaneAttr correctly fails [1,$obj,{name=>"John",age=>39},[qw/a b c/]]}; throws_ok sub { - $obj->InsaneAttr([1,"hello",{name=>"John",age=>39,gender=>undef},[1,2,3]]); + $obj->InsaneAttr([1,"hello",{name=>"John",age=>39,gender=>undef},[1,2,3]]); }, qr/Attribute \(InsaneAttr\) does not pass the type constraint/ => q{InsaneAttr correctly fails [1,"hello",{name=>"John",age=>39,gender=>undef},[1,2,3]]}; - + # Test TupleOptional1Attr lives_ok sub { @@ -140,15 +140,15 @@ lives_ok sub { } => 'Set TupleOptional1Attr attribute without error [1,10]'; throws_ok sub { - $obj->TupleOptional1Attr([1,10,[1,2,3]]); + $obj->TupleOptional1Attr([1,10,[1,2,3]]); }, qr/Attribute \(TupleOptional1Attr\) does not pass the type constraint/ => q{TupleOptional1Attr correctly fails [1,10,[1,2,3]]}; throws_ok sub { - $obj->TupleOptional1Attr([1,10,undef]); + $obj->TupleOptional1Attr([1,10,undef]); }, qr/Attribute \(TupleOptional1Attr\) does not pass the type constraint/ => q{TupleOptional1Attr correctly fails [1,10,undef]}; - + # Test TupleOptional2Attr lives_ok sub { @@ -157,20 +157,20 @@ lives_ok sub { lives_ok sub { $obj->TupleOptional2Attr([1,10]); -} => 'Set TupleOptional2Attr attribute without error [1,10]'; +} => 'Set TupleOptional2Attr attribute without error [1,10]'; throws_ok sub { - $obj->TupleOptional2Attr([1,10,[1,2,3]]); + $obj->TupleOptional2Attr([1,10,[1,2,3]]); }, qr/Attribute \(TupleOptional2Attr\) does not pass the type constraint/ => q{TupleOptional2Attr correctly fails [1,10,[1,2,3]]}; throws_ok sub { - $obj->TupleOptional2Attr([1,10,undef]); + $obj->TupleOptional2Attr([1,10,undef]); }, qr/Attribute \(TupleOptional2Attr\) does not pass the type constraint/ => q{TupleOptional2Attr correctly fails [1,10,undef]}; - + # Test DictOptional1Attr: Dict[name=>Str, age=>Int, gender=>Optional[Gender]]; - + lives_ok sub { $obj->DictOptional1Attr({name=>"John",age=>39,gender=>"male"}); } => 'Set DictOptional1Attr attribute without error {name=>"John",age=>39,gender=>"male"}'; @@ -180,11 +180,11 @@ lives_ok sub { } => 'Set DictOptional1Attr attribute without error {name=>"Vanessa",age=>34}'; throws_ok sub { - $obj->DictOptional1Attr({name=>"John",age=>39,gender=>undef}); + $obj->DictOptional1Attr({name=>"John",age=>39,gender=>undef}); }, qr/Attribute \(DictOptional1Attr\) does not pass the type constraint/ => q{TupleOptional2Attr correctly fails {name=>"John",age=>39,gender=>undef}}; throws_ok sub { - $obj->DictOptional1Attr({name=>"John",age=>39,gender=>"aaa"}); + $obj->DictOptional1Attr({name=>"John",age=>39,gender=>"aaa"}); }, qr/Attribute \(DictOptional1Attr\) does not pass the type constraint/ => q{TupleOptional2Attr correctly fails {name=>"John",age=>39,gender=>"aaa"}}; diff --git a/t/12-error.t b/t/12-error.t index fcd29dc..fbdfcb4 100644 --- a/t/12-error.t +++ b/t/12-error.t @@ -1,7 +1,7 @@ BEGIN { - use strict; - use warnings; - use Test::More tests=>25; + use strict; + use warnings; + use Test::More tests=>25; } use Moose::Util::TypeConstraints; diff --git a/t/regressions/01-is_type_of.t b/t/regressions/01-is_type_of.t index 3ead56f..c9f6c49 100644 --- a/t/regressions/01-is_type_of.t +++ b/t/regressions/01-is_type_of.t @@ -1,7 +1,7 @@ BEGIN { - use strict; - use warnings; - use Test::More tests=>11; + use strict; + use warnings; + use Test::More tests=>11; } {