Commit 101b3355 authored by Alasdair Keyes's avatar Alasdair Keyes

Add in full_value() and handling of partial money amounts

parent 4b1eea48
......@@ -5,4 +5,9 @@ use warnings;
use Carp;
use base qw/ ValueObject::Base /;
sub full_value {
my $self = shift;
croak "Invalid full_value() for " . (ref($self) || $self);
}
1;
......@@ -10,8 +10,19 @@ sub validate {
my $self = shift;
my $value = shift || '';
croak (ValueObject::Finance::Money::Exception::InvalidComma->new($value))
unless ($value =~ /^\d+,\d{2}$/);
unless ($value =~ /^\d+(\,\d{1,2})?$/);
return 1;
}
sub full_value {
my $self = shift;
my $adjusted_value = $self->value();
$adjusted_value =~ s/,/\./g;
$adjusted_value = sprintf('%.2f', $adjusted_value);
$adjusted_value =~ s/\./,/g;
return $adjusted_value;
}
1;
......@@ -10,8 +10,13 @@ sub validate {
my $self = shift;
my $value = shift || '';
croak (ValueObject::Finance::Money::Exception::InvalidPeriod->new($value))
unless ($value =~ /^\d+\.\d{2}$/);
unless ($value =~ /^\d+(\.\d{1,2})?$/);
return 1;
}
sub full_value {
my $self = shift;
return sprintf('%.2f', $self->value());
}
1;
......@@ -2,7 +2,7 @@
use strict;
use warnings;
use Test::More tests => 23;
use Test::More tests => 27;
use Test::Exception;
use FindBin qw($Bin);
......@@ -59,6 +59,36 @@ ok($currency_code->value() eq $valid_currency_code, "value function for object '
ok($currency_code eq $valid_currency_code, "value function for object '$valid_currency_code'");
## Test difference valid formats
my $valid_pound_amount = '23';
my $valid_pound_full_value = '23.00';
my $valid_pound_object = ValueObject::Finance::Money::Period->new($valid_pound_amount);
ok( $valid_pound_object->full_value() eq $valid_pound_full_value,
"Full value '$valid_pound_full_value' returned for value '$valid_pound_amount'"
);
my $valid_pound_single_pence_amount = '45.6';
my $valid_pound_single_pence_full_value = '45.60';
my $valid_pound_single_pence_object = ValueObject::Finance::Money::Period->new($valid_pound_single_pence_amount);
ok($valid_pound_single_pence_object->full_value() eq $valid_pound_single_pence_full_value,
"Full value '$valid_pound_single_pence_full_value' returned for value '$valid_pound_single_pence_amount'"
);
my $valid_euro_amount = '23';
my $valid_euro_full_value = '23,00';
my $valid_euro_object = ValueObject::Finance::Money::Comma->new($valid_euro_amount);
ok( $valid_euro_object->full_value() eq $valid_euro_full_value,
"Full value '$valid_euro_full_value' returned for value '$valid_euro_amount'"
);
my $valid_euro_single_cent_amount = '45,6';
my $valid_euro_single_cent_full_value = '45,60';
my $valid_euro_single_cent_object = ValueObject::Finance::Money::Comma->new($valid_euro_single_cent_amount);
ok($valid_euro_single_cent_object->full_value() eq $valid_euro_single_cent_full_value,
"Full value '$valid_euro_single_cent_full_value' returned for value '$valid_euro_single_cent_amount'"
);
## Test Exceptions
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment