From 36610f8548eeabdde065fa40fd8f3dd7ffc7fc56 Mon Sep 17 00:00:00 2001 From: Stevan Little Date: Wed, 10 Oct 2012 09:34:08 -0400 Subject: adding more complex example in the synopsis to illustrate how much more this module does, and adding a test to test that example --- t/more-basic.t | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 t/more-basic.t (limited to 't/more-basic.t') diff --git a/t/more-basic.t b/t/more-basic.t new file mode 100644 index 0000000..ceb904a --- /dev/null +++ b/t/more-basic.t @@ -0,0 +1,117 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use Moose::Util::TypeConstraints 'find_type_constraint'; + +use MooseX::Validation::Doctypes; + +doctype 'Location' => { + id => 'Str', + city => 'Str', + state => 'Str', + country => 'Str', + zipcode => 'Int', +}; + +doctype 'Person' => { + id => 'Str', + name => { + first_name => 'Str', + last_name => 'Str', + }, + title => 'Str', + friends => 'ArrayRef[Person]', + address => 'Maybe[Location]' +}; + + +{ + my $person = find_type_constraint('Person'); + isa_ok($person, 'Moose::Meta::TypeConstraint'); + isa_ok($person, 'MooseX::Meta::TypeConstraint::Doctype'); + + is_deeply( + $person->doctype, + { + id => 'Str', + name => { + first_name => 'Str', + last_name => 'Str', + }, + title => 'Str', + friends => 'ArrayRef[Person]', + address => 'Maybe[Location]' + }, + "got the right doctype" + ); + + { + my $errors = $person->validate({ + id => '17382-QA', + name => { + first_name => 'Bob', + last_name => 'Smith', + }, + title => 'CIO', + friends => [], + address => { + id => 'My House', + city => 'Anytown', + state => 'IL', + country => 'USA', + zipcode => '12345' + } + }); + + is($errors, undef, "no errors"); + } + + { + my $errors = $person->validate({ + id => '17382-QA', + name => { + first_name => 'Bob', + last_name => 'Smith', + }, + title => 'CIO', + friends => [], + favorite_food => 'ice cream', + }); + isa_ok($errors, 'MooseX::Validation::Doctypes::Errors'); + is_deeply( + $errors->extra_data, + { favorite_food => 'ice cream' }, + "got the right extra data" + ); + is($errors->errors, undef, "no errors"); + } + + { + my $errors = $person->validate({ + id => '17382-QA', + name => { + first_name => 'Bob', + last_name => 'Smith', + }, + friends => [], + address => { + id => 'My House', + city => 'Anytown', + state => 'IL', + country => 'USA', + zipcode => '12345' + } + }); + isa_ok($errors, 'MooseX::Validation::Doctypes::Errors'); + is($errors->extra_data, undef, "no extra data"); + is_deeply( + $errors->errors, + { title => "invalid value undef for 'title'" }, + "got the right errors" + ); + } +} + +done_testing; -- cgit v1.2.3