From: Dave Rolsky Date: Sun, 12 Oct 2008 17:42:23 +0000 (+0000) Subject: Reverted the type constraint bits that caused breakage on Win32 and X-Git-Tag: 0.59~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=84a9c64c562ec926d73ecec464b5a0463d6aacef;p=gitmo%2FMoose.git Reverted the type constraint bits that caused breakage on Win32 and BSD, but kept the parts that normalize names. --- diff --git a/Changes b/Changes index 15a9b06..7a9abc6 100644 --- a/Changes +++ b/Changes @@ -5,12 +5,15 @@ Revision history for Perl extension Moose - Add abridged documentation for builder/default/initializer/ predicate, and link to more details sections in Class::MOP::Attribute. (t0m) + * Moose::Util::TypeConstraints - removed prototypes from all but the &-based stuff (mst) + * Moose::Util::TypeConstraints - Creating a anonymous subtype with both a constraint and a message failed with a very unhelpful error, but should just work. Reported by t0m. (Dave Rolsky) + * Tests - Some tests that used Test::Warn if it was available failed with older versions of Test::Warn. Reported by Fayland. (Dave @@ -19,17 +22,29 @@ Revision history for Perl extension Moose lazy_build. (t0m) - Test behavior of equals/is_a_type_of/is_a_subtype_of for all kinds of supported type. (t0m) + * Moose::Meta::Class - In create(), do not pass "roles" option to the superclass - added related test that creates an anon metaclass with a required attribute + * Moose::Meta::TypeConstraint::Class * Moose::Meta::TypeConstraint::Role - Unify behavior of equals/is_a_type_of/is_a_subtype_of with other types (as per change in 0.55_02). (t0m) + * Moose::Meta::TypeConstraint::Registry - Fix warning when dealing with unknown type names (t0m) + * Moose::Util::TypeConstraints + - Reverted changes from 0.58 related to handle parameterized + types. This caused random failures on BSD and Win32 systems, + apparently related to the regex engine. This means that Moose + can no longer parse structured type constraints like + ArrayRef[Int,Int] or HashRef[name=>Str]. This will be + supported in a slightly different way via MooseX::Types some + time in the future. (Dave Rolsky) + 0.58 Sat September 20, 2008 !! This release has an incompatible change regarding !! !! how roles add methods to a class !! diff --git a/MANIFEST b/MANIFEST index 8cecfe9..218cf15 100644 --- a/MANIFEST +++ b/MANIFEST @@ -193,7 +193,7 @@ t/040_type_constraints/023_types_and_undef.t t/040_type_constraints/024_role_type_constraint.t t/040_type_constraints/025_type_coersion_on_lazy_attributes.t t/040_type_constraints/026_normalize_type_name.t -t/040_type_constraints/027_parameterize_from.t +t/040_type_constraints/029_define_type_twice_throws.t t/050_metaclasses/001_custom_attr_meta_with_roles.t t/050_metaclasses/002_custom_attr_meta_as_role.t t/050_metaclasses/003_moose_w_metaclass.t diff --git a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm index 15d3f11..95193eb 100644 --- a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm +++ b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm @@ -9,8 +9,6 @@ $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; use base 'Moose::Meta::TypeConstraint'; -use Moose::Meta::TypeConstraint::Parameterized; -use Moose::Util::TypeConstraints (); __PACKAGE__->meta->add_attribute('constraint_generator' => ( accessor => 'constraint_generator', @@ -42,29 +40,6 @@ sub _can_coerce_constraint_from { }; } -sub _parse_type_parameter { - my ($self, $type_parameter) = @_; - return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter); -} - -sub parameterize { - my ($self, $type_parameter) = @_; - - my $contained_tc = $self->_parse_type_parameter($type_parameter); - - if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) { - my $tc_name = $self->name . '[' . $contained_tc->name . ']'; - return Moose::Meta::TypeConstraint::Parameterized->new( - name => $tc_name, - parent => $self, - type_parameter => $contained_tc, - ); - } - else { - Moose->throw_error("The type parameter must be a Moose meta type"); - } -} - 1; @@ -87,11 +62,6 @@ Moose::Meta::TypeConstraint::Parameterizable - Higher Order type constraints for =item B -=item B - -Given a single type constraint string, this method parses the string -and parameterizes the type based on the parsed string. - =item B =back diff --git a/lib/Moose/Meta/TypeConstraint/Union.pm b/lib/Moose/Meta/TypeConstraint/Union.pm index 1473198..aea91e0 100644 --- a/lib/Moose/Meta/TypeConstraint/Union.pm +++ b/lib/Moose/Meta/TypeConstraint/Union.pm @@ -21,7 +21,7 @@ __PACKAGE__->meta->add_attribute('type_constraints' => ( sub new { my ($class, %options) = @_; my $self = $class->SUPER::new( - name => (join '|' => map { $_->name } @{$options{type_constraints}}), + name => (join '|' => sort map { $_->name } @{$options{type_constraints}}), parent => undef, message => undef, hand_optimized_type_constraint => undef, diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index f12c26b..eb1abc1 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -86,11 +86,11 @@ sub create_type_constraint_union { (scalar @type_constraint_names >= 2) || Moose->throw_error("You must pass in at least 2 type names to make a union"); - my @type_constraints = sort {$a->name cmp $b->name} map { + my @type_constraints = map { find_or_parse_type_constraint($_) || Moose->throw_error("Could not locate type constraint ($_) for the union"); } @type_constraint_names; - + return Moose::Meta::TypeConstraint::Union->new( type_constraints => \@type_constraints ); @@ -103,30 +103,20 @@ sub create_parameterized_type_constraint { (defined $base_type && defined $type_parameter) || Moose->throw_error("Could not parse type name ($type_constraint_name) correctly"); - if ($REGISTRY->has_type_constraint($base_type)) { - my $base_type_tc = $REGISTRY->get_type_constraint($base_type); - return _create_parameterized_type_constraint( - $base_type_tc, - $type_parameter, - ); - } else { - Moose->throw_error("Could not locate the base type ($base_type)"); - } -} - -sub _create_parameterized_type_constraint { - my ( $base_type_tc, $type_parameter ) = @_; - if ( $base_type_tc->can('parameterize') ) { - return $base_type_tc->parameterize($type_parameter); - } - else { - return Moose::Meta::TypeConstraint::Parameterized->new( - name => $base_type_tc->name . '[' . $type_parameter . ']', - parent => $base_type_tc, - type_parameter => - find_or_create_isa_type_constraint($type_parameter), - ); - } + # We need to get the relevant type constraints and use them to + # create the name to ensure that we end up with the fully + # normalized name, because the user could've passed something like + # HashRef[Str|Int] and we want to make that HashRef[Int|Str]. + my $base_type_tc = $REGISTRY->get_type_constraint($base_type) + || Moose->throw_error("Could not locate the base type ($base_type)"); + my $parameter_tc = find_or_create_isa_type_constraint($type_parameter) + || Moose->throw_error("Could not locate the parameter type ($type_parameter)"); + + return Moose::Meta::TypeConstraint::Parameterized->new( + name => $base_type_tc->name . '[' . $parameter_tc->name . ']', + parent => $base_type_tc, + type_parameter => $parameter_tc, + ); } #should we also support optimized checks? @@ -223,7 +213,7 @@ sub find_or_parse_type_constraint { } sub normalize_type_constraint_name { - my $type_constraint_name = shift @_; + my $type_constraint_name = shift; $type_constraint_name =~ s/\s//g; return $type_constraint_name; } @@ -457,19 +447,12 @@ sub _install_type_coercions ($$) { my $op_union = qr{ \s* \| \s* }x; my $union = qr{ $type (?: $op_union $type )+ }x; - ## New Stuff for structured types. - my $comma = qr{,}; - my $indirection = qr{=>}; - my $divider_ops = qr{ $comma | $indirection }x; - my $structure_divider = qr{\s* $divider_ops \s*}x; - my $structure_elements = qr{ ($type $structure_divider*)+ }x; - - $any = qr{ $type | $union | $structure_elements }x; + $any = qr{ $type | $union }x; sub _parse_parameterized_type_constraint { { no warnings 'void'; $any; } # force capture of interpolated lexical - my($base, $elements) = ($_[0] =~ m{ $type_capture_parts }x); - return ($base,$elements); + $_[0] =~ m{ $type_capture_parts }x; + return ($1, $2); } sub _detect_parameterized_type_constraint { diff --git a/t/040_type_constraints/027_parameterize_from.t b/t/040_type_constraints/027_parameterize_from.t deleted file mode 100644 index f917e28..0000000 --- a/t/040_type_constraints/027_parameterize_from.t +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -use Test::More tests => 12; -use Test::Exception; - -BEGIN { - use_ok('Moose::Util::TypeConstraints'); -} - -# testing the parameterize method - -{ - my $parameterizable = subtype 'parameterizable_hashref', as 'HashRef'; - - my $parameterized = subtype 'parameterized_hashref', as 'HashRef[Int]'; - - my $int = Moose::Util::TypeConstraints::find_type_constraint('Int'); - - my $from_parameterizable = $parameterizable->parameterize($int); - - isa_ok $parameterizable, - 'Moose::Meta::TypeConstraint::Parameterizable', => - 'Got expected type instance'; - - package Test::Moose::Meta::TypeConstraint::Parameterizable; - use Moose; - - has parameterizable => ( is => 'rw', isa => $parameterizable ); - has parameterized => ( is => 'rw', isa => $parameterized ); - has from_parameterizable => ( is => 'rw', isa => $from_parameterizable ); -} - -# Create and check a dummy object - -ok my $params = Test::Moose::Meta::TypeConstraint::Parameterizable->new() => - 'Create Dummy object for testing'; - -isa_ok $params, 'Test::Moose::Meta::TypeConstraint::Parameterizable' => - 'isa correct type'; - -# test parameterizable - -lives_ok sub { - $params->parameterizable( { a => 'Hello', b => 'World' } ); -} => 'No problem setting parameterizable'; - -is_deeply $params->parameterizable, - { a => 'Hello', b => 'World' } => 'Got expected values'; - -# test parameterized - -lives_ok sub { - $params->parameterized( { a => 1, b => 2 } ); -} => 'No problem setting parameterized'; - -is_deeply $params->parameterized, { a => 1, b => 2 } => 'Got expected values'; - -throws_ok sub { - $params->parameterized( { a => 'Hello', b => 'World' } ); - }, qr/Attribute \(parameterized\) does not pass the type constraint/ => - 'parameterized throws expected error'; - -# test from_parameterizable - -lives_ok sub { - $params->from_parameterizable( { a => 1, b => 2 } ); -} => 'No problem setting from_parameterizable'; - -is_deeply $params->from_parameterizable, - { a => 1, b => 2 } => 'Got expected values'; - -throws_ok sub { - $params->from_parameterizable( { a => 'Hello', b => 'World' } ); - }, - qr/Attribute \(from_parameterizable\) does not pass the type constraint/ - => 'from_parameterizable throws expected error';