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,
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");
}
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)) {
return $constraint;
}
+sub normalize_type_constraint_name {
+ my $type_constraint_name = shift @_;
+ $type_constraint_name =~s/\s//g;
+ return $type_constraint_name;
+}
+
## --------------------------------------------------------
## exported functions ...
## --------------------------------------------------------
=over 4
+=item B<normalize_type_constraint_name ($type_constraint_name)>
+
+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<create_type_constraint_union ($pipe_seperated_types | @type_constraint_names)>
Given string with C<$pipe_seperated_types> or a list of C<@type_constraint_names>,
$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");
$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");
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');
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');
lives_ok {
subtype 'MyCollectionsExtended'
- => as 'ArrayRef | HashRef'
+ => as 'ArrayRef|HashRef'
=> where {
if (ref($_) eq 'ARRAY') {
return if scalar(@$_) < 2;
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');