From: John Napiorkowski Date: Fri, 12 Sep 2008 21:26:05 +0000 (+0000) Subject: added a method to normalize incoming type constraint names and changed how union... X-Git-Tag: 0.58~37^2~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=eb4c4e821e3636e607b09919b1403d52ddbee092;p=gitmo%2FMoose.git added a method to normalize incoming type constraint names and changed how union types are named to match this, updated tests to match --- diff --git a/lib/Moose/Meta/TypeConstraint/Union.pm b/lib/Moose/Meta/TypeConstraint/Union.pm index f754834..1473198 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 '|' => 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 05df468..0888884 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -101,7 +101,7 @@ sub create_type_constraint_union (@) { else { @type_constraint_names = @_; } - + (scalar @type_constraint_names >= 2) || Moose->throw_error("You must pass in at least 2 type names to make a union"); @@ -223,7 +223,7 @@ sub find_or_create_does_type_constraint ($) { } sub find_or_parse_type_constraint ($) { - my $type_constraint_name = shift; + my $type_constraint_name = normalize_type_constraint_name(shift); my $constraint; if ($constraint = find_type_constraint($type_constraint_name)) { @@ -240,6 +240,12 @@ sub find_or_parse_type_constraint ($) { return $constraint; } +sub normalize_type_constraint_name { + my $type_constraint_name = shift @_; + $type_constraint_name =~s/\s//g; + return $type_constraint_name; +} + ## -------------------------------------------------------- ## exported functions ... ## -------------------------------------------------------- @@ -926,6 +932,11 @@ This is just sugar for the type coercion construction syntax. =over 4 +=item B + +Given a string that is expected to match a type constraint, will normalize the +string so that extra whitespace and newlines are removed. + =item B Given string with C<$pipe_seperated_types> or a list of C<@type_constraint_names>, diff --git a/t/030_roles/017_extending_role_attrs.t b/t/030_roles/017_extending_role_attrs.t index 9b46f59..7c6dcc6 100644 --- a/t/030_roles/017_extending_role_attrs.t +++ b/t/030_roles/017_extending_role_attrs.t @@ -99,7 +99,7 @@ is($baz->baz, 99, '... got the extended attribute'); $baz->baz('Foo'); is($baz->baz, 'Foo', "... can change the attribute's value to a ClassName"); -throws_ok { $baz->baz("zonk") } qr/^Attribute \(baz\) does not pass the type constraint because: Validation failed for 'ClassName \| Int' failed with value zonk at /; +throws_ok { $baz->baz("zonk") } qr/^Attribute \(baz\) does not pass the type constraint because: Validation failed for 'ClassName\|Int' failed with value zonk at /; is_deeply($baz->baz, 'Foo', "... still has the old ClassName value"); @@ -137,10 +137,10 @@ is($quux->quux, 100, "... can change the attribute's value to an Int"); $quux->quux(["hi"]); is_deeply($quux->quux, ["hi"], "... can change the attribute's value to an ArrayRef"); -throws_ok { $quux->quux("quux") } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'ArrayRef \| Positive' failed with value quux at /; +throws_ok { $quux->quux("quux") } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'ArrayRef\|Positive' failed with value quux at /; is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value"); -throws_ok { $quux->quux({a => 1}) } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'ArrayRef \| Positive' failed with value HASH\(\w+\) at /; +throws_ok { $quux->quux({a => 1}) } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'ArrayRef\|Positive' failed with value HASH\(\w+\) at /; is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value"); diff --git a/t/040_type_constraints/008_union_types.t b/t/040_type_constraints/008_union_types.t index c548a6d..16227bc 100644 --- a/t/040_type_constraints/008_union_types.t +++ b/t/040_type_constraints/008_union_types.t @@ -64,14 +64,14 @@ ok(!defined($HashOrArray->validate([])), '... (ArrayRef | HashRef) can accept [] ok(!defined($HashOrArray->validate({})), '... (ArrayRef | HashRef) can accept {}'); like($HashOrArray->validate(\(my $var2)), -qr/Validation failed for \'ArrayRef\' failed with value SCALAR\(0x.+?\) and Validation failed for \'HashRef\' failed with value SCALAR\(0x.+?\) in \(ArrayRef \| HashRef\)/, +qr/Validation failed for \'ArrayRef\' failed with value SCALAR\(0x.+?\) and Validation failed for \'HashRef\' failed with value SCALAR\(0x.+?\) in \(ArrayRef\|HashRef\)/, '... (ArrayRef | HashRef) cannot accept scalar refs'); like($HashOrArray->validate(sub {}), -qr/Validation failed for \'ArrayRef\' failed with value CODE\(0x.+?\) and Validation failed for \'HashRef\' failed with value CODE\(0x.+?\) in \(ArrayRef \| HashRef\)/, +qr/Validation failed for \'ArrayRef\' failed with value CODE\(0x.+?\) and Validation failed for \'HashRef\' failed with value CODE\(0x.+?\) in \(ArrayRef\|HashRef\)/, '... (ArrayRef | HashRef) cannot accept code refs'); is($HashOrArray->validate(50), -'Validation failed for \'ArrayRef\' failed with value 50 and Validation failed for \'HashRef\' failed with value 50 in (ArrayRef | HashRef)', +'Validation failed for \'ArrayRef\' failed with value 50 and Validation failed for \'HashRef\' failed with value 50 in (ArrayRef|HashRef)', '... (ArrayRef | HashRef) cannot accept Numbers'); diff --git a/t/040_type_constraints/017_subtyping_union_types.t b/t/040_type_constraints/017_subtyping_union_types.t index 22dec83..44ffd95 100644 --- a/t/040_type_constraints/017_subtyping_union_types.t +++ b/t/040_type_constraints/017_subtyping_union_types.t @@ -24,7 +24,7 @@ lives_ok { isa_ok($p, 'Moose::Meta::TypeConstraint::Union'); isa_ok($p, 'Moose::Meta::TypeConstraint'); - is($p->name, 'ArrayRef | HashRef', '... parent name is correct'); + is($p->name, 'ArrayRef|HashRef', '... parent name is correct'); ok($t->check([]), '... validated it correctly'); ok($t->check({}), '... validated it correctly'); @@ -33,7 +33,7 @@ lives_ok { lives_ok { subtype 'MyCollectionsExtended' - => as 'ArrayRef | HashRef' + => as 'ArrayRef|HashRef' => where { if (ref($_) eq 'ARRAY') { return if scalar(@$_) < 2; @@ -55,7 +55,7 @@ lives_ok { isa_ok($p, 'Moose::Meta::TypeConstraint::Union'); isa_ok($p, 'Moose::Meta::TypeConstraint'); - is($p->name, 'ArrayRef | HashRef', '... parent name is correct'); + is($p->name, 'ArrayRef|HashRef', '... parent name is correct'); ok(!$t->check([]), '... validated it correctly'); ok($t->check([1, 2]), '... validated it correctly');