From 23b1cb47c5a1088b0d43357a932dc65caaf561bf Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sat, 10 Oct 2009 16:27:33 -0700 Subject: [PATCH] Parse tests, handling spaces. --- lib/WebGUI/Shop/ShipDriver/USPS.pm | 6 ++++- t/Shop/ShipDriver/USPS.t | 36 +++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/WebGUI/Shop/ShipDriver/USPS.pm b/lib/WebGUI/Shop/ShipDriver/USPS.pm index 8fe717c5d..50a2f3fc9 100644 --- a/lib/WebGUI/Shop/ShipDriver/USPS.pm +++ b/lib/WebGUI/Shop/ShipDriver/USPS.pm @@ -261,7 +261,11 @@ shipment, a colon, and the cost of insuring a shipment of that value. sub _parseInsuranceRates { my $rates = shift; - my @lines = split /\r?\n/, $rates; + $rates =~ tr/\r//d; + my $number = qr/\d+(?:\.\d+)?/; + my $rate = qr{ \s* $number \s* : \s* $number \s* }x; + return () if ($rates !~ m{ \A (?: $rate \r?\n )* $rate (?:\r\n)? \Z }x); + my @lines = split /\n/, $rates; my @table = (); foreach my $line (@lines) { $line =~ s/\s+//g; diff --git a/t/Shop/ShipDriver/USPS.t b/t/Shop/ShipDriver/USPS.t index b54229474..65bd7149e 100644 --- a/t/Shop/ShipDriver/USPS.t +++ b/t/Shop/ShipDriver/USPS.t @@ -24,7 +24,7 @@ use Data::Dumper; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; -plan tests => 53; +plan tests => 64; use_ok('WebGUI::Shop::ShipDriver::USPS') or die 'Unable to load module WebGUI::Shop::ShipDriver::USPS'; @@ -492,7 +492,7 @@ is($cost, 12.25, '_calculateFromXML calculates shipping cost correctly for 2 ite $bibleItem->setQuantity(2); @shippableUnits = $driver->_getShippableUnits($cart); -is(calculateInsurance($driver), 51, '_calculateInsurance: two items in cart with quantity=2, calculates insurance'); +is(calculateInsurance($driver), 8, '_calculateInsurance: two items in cart with quantity=2, calculates insurance'); $cost = $driver->_calculateFromXML({ Package => [ @@ -517,7 +517,7 @@ is($cost, 19.25, '_calculateFromXML calculates shipping cost correctly for 2 ite $rockHammer2 = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer)); $rockHammer2->update({shippingAddressId => $wucAddress->getId}); @shippableUnits = $driver->_getShippableUnits($cart); -is(calculateInsurance($driver), 54, '_calculateInsurance: calculates insurance'); +is(calculateInsurance($driver), 12, '_calculateInsurance: calculates insurance'); $xml = $driver->buildXML($cart, @shippableUnits); $xmlData = XMLin( $xml, @@ -818,6 +818,36 @@ $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $gospels)); @shippableUnits = $driver->_getShippableUnits($cart); is(calculateInsurance($driver), 1, '_calculateInsurance: calculates insurance using the first bin'); +####################################################################### +# +# _parseInsuranceRates +# +####################################################################### + +my @rates; +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates(""); +cmp_deeply(\@rates, [], '_parseInsuranceRates: empty string returns empty array'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates(); +cmp_deeply(\@rates, [], '_parseInsuranceRates: undef returns empty array'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2"); +cmp_deeply(\@rates, [], '... bad rates #1'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates(":2"); +cmp_deeply(\@rates, [], '... bad rates #2'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("a:b"); +cmp_deeply(\@rates, [], '... bad rates #3'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2:2"); +cmp_deeply(\@rates, [ ['2', '2'] ], '... one line of good rates'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2.0:2.0"); +cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with decimal points'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2.0:2.0\n"); +cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with newline'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2.0:2.0\r\n"); +cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with cr/newline'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2.0 : 2.0\r\n"); +cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with cr/newline and spaces'); +@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates(" 2.0 : 2.0 \r\n"); +cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with cr/newline and more spaces'); + #---------------------------------------------------------------------------- # Cleanup END {