From: John Napiorkowski Date: Fri, 12 Sep 2008 20:36:33 +0000 (+0000) Subject: more tests to flex type constraint name normalization X-Git-Tag: 0.58~37^2~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fa92d70556d0a2f43e2ed5ab62e30a14acc1c60e;p=gitmo%2FMoose.git more tests to flex type constraint name normalization --- diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 8658c82..05df468 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -105,13 +105,13 @@ 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 = sort {$a->name cmp $b->name} map { find_or_parse_type_constraint($_) || - Moose->throw_error("Could not locate type constraint ($_) for the union") + Moose->throw_error("Could not locate type constraint ($_) for the union"); } @type_constraint_names; return Moose::Meta::TypeConstraint::Union->new( - type_constraints => [@type_constraints] + type_constraints => \@type_constraints ); } diff --git a/t/040_type_constraints/026_normalize_type_name.t b/t/040_type_constraints/026_normalize_type_name.t new file mode 100644 index 0000000..1f458ee --- /dev/null +++ b/t/040_type_constraints/026_normalize_type_name.t @@ -0,0 +1,153 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 39; +use Test::Exception; + +BEGIN { + use_ok('Moose::Util::TypeConstraints'); +} + +## First, we check that the new regex parsing works + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('ArrayRef[Str]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('ArrayRef[Str]')], + ["ArrayRef", "Str"] + => 'Correctly parsed ArrayRef[Str]'; + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('ArrayRef[Str ]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('ArrayRef[Str ]')], + ["ArrayRef", "Str"] + => 'Correctly parsed ArrayRef[Str ]'; + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('ArrayRef[ Str]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('ArrayRef[ Str]')], + ["ArrayRef", "Str"] + => 'Correctly parsed ArrayRef[ Str]'; + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('ArrayRef[ Str ]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('ArrayRef[ Str ]')], + ["ArrayRef", "Str"] + => 'Correctly parsed ArrayRef[ Str ]'; + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('ArrayRef[ HashRef[Int] ]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('ArrayRef[ HashRef[Int] ]')], + ["ArrayRef", "HashRef[Int]"] + => 'Correctly parsed ArrayRef[ HashRef[Int] ]'; + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('ArrayRef[ HashRef[Int ] ]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('ArrayRef[ HashRef[Int ] ]')], + ["ArrayRef", "HashRef[Int ]"] + => 'Correctly parsed ArrayRef[ HashRef[Int ] ]'; + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('ArrayRef[Int,Str]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('ArrayRef[Int,Str]')], + ["ArrayRef", "Int", "Str"] + => 'Correctly parsed ArrayRef[Int,Str]'; + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('ArrayRef[ArrayRef[Int],Str]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('ArrayRef[ArrayRef[Int],Str]')], + ["ArrayRef", "ArrayRef[Int]", "Str"] + => 'Correctly parsed ArrayRef[ArrayRef[Int],Str]'; + +ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]') + => 'detected correctly'; + +is_deeply + [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')], + ["HashRef", "key1", "Int", "key2", "Int", "key3", "ArrayRef[Int]"] + => 'Correctly parsed HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]'; + +## creating names via subtype + +ok my $r = Moose::Util::TypeConstraints->get_type_constraint_registry + => 'Got registry object'; + +ok my $subtype_a1 = subtype('subtype_a1' => as 'HashRef[Int]'), + => 'created subtype_a1'; + +ok my $subtype_a2 = subtype('subtype_a2' => as 'HashRef[ Int]'), + => 'created subtype_a2'; + +ok my $subtype_a3 = subtype('subtype_a2' => as 'HashRef[Int ]'), + => 'created subtype_a2'; + +ok my $subtype_a4 = subtype('subtype_a2' => as 'HashRef[ Int ]'), + => 'created subtype_a2'; + +is $subtype_a1->parent->name, $subtype_a2->parent->name + =>'names match'; + +is $subtype_a1->parent->name, $subtype_a3->parent->name + =>'names match'; + +is $subtype_a1->parent->name, $subtype_a4->parent->name + =>'names match'; + +ok my $subtype_b1 = subtype('subtype_b1' => as 'HashRef[Int|Str]'), + => 'created subtype_b1'; + +ok my $subtype_b2 = subtype('subtype_b2' => as 'HashRef[Int | Str]'), + => 'created subtype_b2'; + +ok my $subtype_b3 = subtype('subtype_b2' => as 'HashRef[Str|Int]'), + => 'created subtype_b3'; + +is $subtype_b1->parent->name, $subtype_b2->parent->name + =>'names match'; + +is $subtype_b1->parent->name, $subtype_b3->parent->name + =>'names match'; + +is $subtype_b2->parent->name, $subtype_b3->parent->name + =>'names match'; + +## testing via add_constraint + +ok my $union1 = Moose::Util::TypeConstraints::create_type_constraint_union( + 'ArrayRef[Int|Str] | ArrayRef[Int | HashRef]') => 'Created Union1'; + +ok my $union2 = Moose::Util::TypeConstraints::create_type_constraint_union( + 'ArrayRef[ Int|Str] | ArrayRef[Int | HashRef]') => 'Created Union2'; + +ok my $union3 = Moose::Util::TypeConstraints::create_type_constraint_union( + 'ArrayRef[Int |Str ] | ArrayRef[Int | HashRef ]') => 'Created Union3'; + +is $union1->name, $union2->name, + 'names match'; + +is $union1->name, $union3->name, + 'names match'; + +is $union2->name, $union3->name, + 'names match'; + + + + \ No newline at end of file diff --git a/t/040_type_constraints/027_parameterize_from.t b/t/040_type_constraints/027_parameterize_from.t new file mode 100644 index 0000000..31d301f --- /dev/null +++ b/t/040_type_constraints/027_parameterize_from.t @@ -0,0 +1,80 @@ +#!/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';