diff --git a/lib/Interchange6/Cart.pm b/lib/Interchange6/Cart.pm index b47b4e3..ccaf4c7 100644 --- a/lib/Interchange6/Cart.pm +++ b/lib/Interchange6/Cart.pm @@ -470,8 +470,7 @@ sub update { die "sku not defined in arg to update" unless defined $sku; - PositiveOrZeroInt->check($qty) - or die "quantity argument to update must be positive integer or zero"; + defined($qty) or die "quantity argument to update must be defined"; unless ( $product = $self->find($sku) ) { die "Product for $sku not found in cart."; diff --git a/lib/Interchange6/Cart/Product.pm b/lib/Interchange6/Cart/Product.pm index d8e3bb4..641bf78 100644 --- a/lib/Interchange6/Cart/Product.pm +++ b/lib/Interchange6/Cart/Product.pm @@ -150,8 +150,17 @@ than zero. Default for quantity is 1. =cut has quantity => ( - is => 'ro', - isa => PositiveInt, + is => 'ro', + # https://github.com/interchange/Interchange6/issues/28 + # Tupe::Tiny::XS sometimes incorrectly passes Int assertion for + # non-integer values such as 2.3 so we can't just do: + # isa => PositiveInt + # but if we stringify the value then things work as expected. Huh? + # Also prevent uninitialized warning in case value is undef. + isa => sub { + no warnings 'uninitialized'; + PositiveInt->assert_valid("$_[0]"); + }, default => 1, writer => 'set_quantity', ); diff --git a/t/unit/cart.t b/t/unit/cart.t index fa594b3..4e25863 100644 --- a/t/unit/cart.t +++ b/t/unit/cart.t @@ -339,17 +339,20 @@ throws_ok { $cart->update( "badsku" => 1 ) } qr/badsku not found in cart/, "fail update with bad sku"; throws_ok { $cart->update("SKU01") } -qr/quantity argument to update must be positive integer or zero/, - "fail update no quantity"; +qr/quantity argument to update must be defined/, "fail update no quantity"; throws_ok { $cart->update(undef) } qr/sku not defined/, "fail update sku undef"; -throws_ok { $cart->update( SKU01 => 2.3 ) } -qr/quantity argument to update must be positive integer or zero/, +# NOTE: this is the test which fails if we simply use 'isa => PositiveInt' +# for Cart::Products's quantity attribute. +# https://github.com/interchange/Interchange6/issues/28 +throws_ok { $cart->update( SKU01 => 2.3 ) } qr/Must be a positive integer/, "fail update with non-integer quantity" - or diag "SKU01 quantity set 2.3 is: " - . $cart->product_get( $cart->product_index( sub { $_->sku eq 'SKU01' } ) ) - ->quantity; + or diag( + "SKU01 quantity set 2.3 is: ", + $cart->product_get( $cart->product_index( sub { $_->sku eq 'SKU01' } ) ) + ->quantity + ); lives_ok { @products = $cart->update( SKU01 => 3 ) } "set SKU01 qty to what it already is in cart";