X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F12-error.t;h=404716d214786e64d7afbc7e8d172eb42ca3ebae;hb=5ac93dbb32df5bdf286efab5cca11423083b8a57;hp=2c3ca141323d5bb4a8c4fa614d87c6f06bf7d6f1;hpb=d716430a1f595e7bd54039e440a0286102fc87f1;p=gitmo%2FMooseX-Types-Structured.git diff --git a/t/12-error.t b/t/12-error.t index 2c3ca14..404716d 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=>27; } use Moose::Util::TypeConstraints; @@ -23,19 +23,19 @@ ok !$simple_dict->check({name=>$simple_dict,age=>'hello'}), "simple_dict fails: ## Let's check all the expected validation errors for tuple like $simple_tuple->validate({a=>1,b=>2}), - qr/Validation failed for 'simple_tuple' failed with value { a => 1, b => 2 }/, + qr/Validation failed for 'simple_tuple' with value { a: 1, b: 2 }/, 'Wrong basic type'; like $simple_tuple->validate(['a','b']), - qr/failed for 'simple_tuple' failed with value \[ "a", "b" \]/, + qr/failed for 'simple_tuple' with value \[ "a", "b" \]/, 'Correctly failed due to "a" not an Int'; - + like $simple_tuple->validate([1,$simple_tuple]), - qr/Validation failed for 'simple_tuple' failed with value \[ 1, MooseX::Meta::TypeConstraint::Structured/, + qr/Validation failed for 'simple_tuple' with value \[ 1, MooseX::Meta::TypeConstraint::Structured/, 'Correctly failed due to object not a Str'; like $simple_tuple->validate([1]), - qr/Validation failed for 'Str' failed with value NULL/, + qr/Validation failed for 'Str' with value NULL/, 'Not enought values'; like $simple_tuple->validate([1,'hello','too many']), @@ -45,40 +45,40 @@ like $simple_tuple->validate([1,'hello','too many']), ## And the same thing for dicts [name=>Str,age=>Int] like $simple_dict->validate([1,2]), - qr/ failed with value \[ 1, 2 \]/, + qr/ with value \[ 1, 2 \]/, 'Wrong basic type'; - + like $simple_dict->validate({name=>'John',age=>'a'}), - qr/failed for 'Int' failed with value a/, + qr/failed for 'Int' with value a/, 'Correctly failed due to age not an Int'; - + like $simple_dict->validate({name=>$simple_dict,age=>1}), - qr/failed with value { age => 1, name => MooseX:/, + qr/with value { age: 1, name: MooseX:/, 'Correctly failed due to object not a Str'; like $simple_dict->validate({name=>'John'}), - qr/failed for 'Int' failed with value NULL/, + qr/failed for 'Int' with value NULL/, 'Not enought values'; like $simple_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}), qr/More values than Type Constraints!/, 'Too Many values'; - + ## TODO some with Optional (or Maybe) and slurpy - + my $optional_tuple = subtype 'optional_tuple', as Tuple[Int,Optional[Str]]; my $optional_dict = subtype 'optional_dict', as Dict[name=>Str,age=>Optional[Int]]; - + like $optional_tuple->validate({a=>1,b=>2}), - qr/Validation failed for 'optional_tuple' failed with value { a => 1, b => 2 }/, + qr/Validation failed for 'optional_tuple' with value { a: 1, b: 2 }/, 'Wrong basic type'; like $optional_tuple->validate(['a','b']), - qr/failed for 'Int' failed with value a/, + qr/failed for 'Int' with value a/, 'Correctly failed due to "a" not an Int'; - + like $optional_tuple->validate([1,$simple_tuple]), - qr/failed for 'MooseX::Types::Structured::Optional\[Str\]' failed with value MooseX/, + qr/failed for 'MooseX::Types::Structured::Optional\[Str\]' with value MooseX/, 'Correctly failed due to object not a Str'; like $optional_tuple->validate([1,'hello','too many']), @@ -86,21 +86,21 @@ like $optional_tuple->validate([1,'hello','too many']), 'Too Many values'; like $optional_dict->validate([1,2]), - qr/ failed with value \[ 1, 2 \]/, + qr/ with value \[ 1, 2 \]/, 'Wrong basic type'; - + like $optional_dict->validate({name=>'John',age=>'a'}), - qr/Validation failed for 'MooseX::Types::Structured::Optional\[Int\]' failed with value a/, + qr/Validation failed for 'MooseX::Types::Structured::Optional\[Int\]' with value a/, 'Correctly failed due to age not an Int'; - + like $optional_dict->validate({name=>$simple_dict,age=>1}), - qr/failed with value { age => 1, name => MooseX:/, + qr/with value { age: 1, name: MooseX:/, 'Correctly failed due to object not a Str'; like $optional_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}), qr/More values than Type Constraints!/, 'Too Many values'; - + ## Deeper constraints my $deep_tuple = subtype 'deep_tuple', @@ -112,14 +112,58 @@ my $deep_tuple = subtype 'deep_tuple', age=>Int, ], ]; - + ok $deep_tuple->check([1,{a=>2},{name=>'Vincent',age=>15}]), 'Good Constraint'; - -like $deep_tuple->validate([1,{a=>2},{name=>'Vincent',age=>'Hello'}]), - qr/Error is: Validation failed for 'MooseX::Types::Structured::Dict\[name,Str,age,Int\]'/, - 'Example deeper error'; - + +{ + my $message = $deep_tuple->validate([1,{a=>2},{name=>'Vincent',age=>'Hello'}]); + like $message, + qr/Validation failed for 'MooseX::Types::Structured::Dict\[name,Str,age,Int\]'/, + 'Example deeper error'; +} + +like $simple_tuple->validate(["aaa","bbb"]), + qr/'Int' with value aaa/, + 'correct deeper error'; + +like $deep_tuple->validate([1,{a=>2},{name=>'Vincent1',age=>'Hello1'}]), + qr/'Int' with value Hello1/, + 'correct deeper error'; + ## Success Tests... ok !$deep_tuple->validate([1,{a=>2},{name=>'John',age=>40}]), 'Validates ok'; + +## Deeper Tests... + +my $deeper_tc = subtype + as Dict[ + a => Tuple[ + Dict[ + a1a => Tuple[Int], + a1b => Tuple[Int], + ], + Dict[ + a2a => Tuple[Int], + a2b => Tuple[Int], + ], + ], + b => Tuple[ + Dict[ + b1a => Tuple[Int], + b1b => Tuple[Int], + ], + Dict[ + b2a => Tuple[Int], + b2b => Tuple[Int], + ], + ], + ]; + +{ + my $message = $deeper_tc->validate({a=>[{a1a=>[1],a1b=>[2]},{a2a=>[3],a2b=>[4]}],b=>[{b1a=>[5],b1b=>['AA']},{b2a=>[7],b2b=>[8]}]}); + warn $message; +} + +