From c9661496fe66a6d8855720992ff790f324d5e224 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 28 Feb 2008 00:24:31 +0000 Subject: [PATCH] begin conversion of ShipDriver to use exceptions, in code and in test --- lib/WebGUI/Shop/ShipDriver.pm | 7 ++++-- t/Shop/ShipDriver.t | 46 ++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/lib/WebGUI/Shop/ShipDriver.pm b/lib/WebGUI/Shop/ShipDriver.pm index ce587b6b1..b968456b1 100644 --- a/lib/WebGUI/Shop/ShipDriver.pm +++ b/lib/WebGUI/Shop/ShipDriver.pm @@ -7,6 +7,7 @@ use Carp qw(croak); use Tie::IxHash; use WebGUI::International; use WebGUI::HTMLForm; +use WebGUI::Exception::Shop; use JSON; =head1 NAME @@ -102,8 +103,10 @@ A list of properties to assign to this ShipperDriver. See C for det sub create { my $class = shift; my $session = shift; + WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) + unless ref $session eq 'WebGUI::Session'; my $options = shift; - croak "You must pass a hashref of options to create a new ShipDriver object" + WebGUI::Error::InvalidParam->throw(error => 'Must pass in a hashref of params to create a new ShipDriver object') unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options }; my $shipperId = $session->id->generate; my $self = WebGUI::Shop::ShipDriver->_buildObj($session, $class, $shipperId, $options); @@ -127,7 +130,7 @@ the user. sub definition { my $class = shift; my $session = shift; - croak "Definition requires a session object" + WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) unless ref $session eq 'WebGUI::Session'; my $definition = shift || []; my $i18n = WebGUI::International->new($session, 'ShipDriver'); diff --git a/t/Shop/ShipDriver.t b/t/Shop/ShipDriver.t index 8cc66638c..6720b8999 100644 --- a/t/Shop/ShipDriver.t +++ b/t/Shop/ShipDriver.t @@ -31,12 +31,14 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 29; +my $tests = 34; plan tests => 1 + $tests; #---------------------------------------------------------------------------- # put your tests here +my $e; + my $loaded = use_ok('WebGUI::Shop::ShipDriver'); my $storage; @@ -54,7 +56,15 @@ skip 'Unable to load module WebGUI::Shop::ShipDriver', $tests unless $loaded; my $definition; eval { $definition = WebGUI::Shop::ShipDriver->definition(); }; -like ($@, qr/^Definition requires a session object/, 'definition croaks without a session object'); +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidParam', 'definition takes an exception to not giving it a session variable'); +cmp_deeply( + $e, + methods( + error => 'Must provide a session variable', + ), + 'definition: requires a session variable', +); $definition = WebGUI::Shop::ShipDriver->definition($session); @@ -106,11 +116,39 @@ cmp_deeply( my $driver; +eval { $driver = WebGUI::Shop::ShipDriver->create(); }; +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a session object'); +cmp_deeply( + $e, + methods( + error => 'Must provide a session variable', + ), + 'create takes exception to not giving it a session object', +); + eval { $driver = WebGUI::Shop::ShipDriver->create($session); }; -like ($@, qr/You must pass a hashref of options to create a new ShipDriver object/, 'create croaks without a hashref of options'); +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a hashref of options'); +cmp_deeply( + $e, + methods( + error => 'Must pass in a hashref of params to create a new ShipDriver object', + ), + 'create takes exception to not giving it a hashref of options', +); + eval { $driver = WebGUI::Shop::ShipDriver->create($session, {}); }; -like ($@, qr/You must pass a hashref of options to create a new ShipDriver object/, 'create croaks with an empty hashref of options'); +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it an empty hashref of options'); +cmp_deeply( + $e, + methods( + error => 'Must pass in a hashref of params to create a new ShipDriver object', + ), + 'create takes exception to not giving it an empty hashref of options', +); my $options = { label => 'Slow and dangerous',