Commit 42e24b16 authored by Alasdair Keyes's avatar Alasdair Keyes

Add in Continent Value Object and tests

parent bd4fc172
package ValueObject::Geography::Continent;
use strict;
use warnings;
use Carp;
use ValueObject::Geography::Continent::Exception::Invalid;
use base qw/ ValueObject::Base /;
my @enum = (
'antarctica',
'africa',
'asia',
'australia',
'europe',
'north america',
'south america'
);
sub validate {
my $self = shift;
my $value = shift || '';
croak (ValueObject::Geography::Continent::Exception::Invalid->new($value))
unless (grep { lc($_) eq lc($value) } @enum);
return 1;
}
1;
=head1 NAME
ValueObject::Geography::Continent - Value object to represent continents
=head1 SYNOPSIS
use ValueObject::Geography::Continent;
my $vo = ValueObject::Geography::Continent->new('europe');
=head1 DESCRIPTION
ValueObject::Geography::Continent provides Value Object for validation of
continents (Case insensitive)
=head1 METHODS
=over 4
=item new($value = '')
Creates a new object
Throws ValueObject::Geography::Continent::Exception::Invalid if $value is invalid
=item validate()
Called during instantiation to validate $value
=item value()
Returns the raw value as passed in at instantiation
=back
=head1 SEE ALSO
L<ValueObject::Base>
=cut
package ValueObject::Geography::Continent::Exception::Invalid;
use strict;
use warnings;
use base qw/ ValueObject::Exception::Base /;
sub new {
my $class = shift;
my $message = 'Invalid continent';
return $class->SUPER::new($message, @_);
}
1;
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Exception;
use FindBin qw($Bin);
use lib "$Bin/../lib";
# Setup
require_ok('ValueObject::Geography::Continent');
my @valid_continents = (
'antarctica',
'africa',
'asia',
'australia',
'europe',
'north america',
'south america'
);
my @invalid_continents = (
undef,
'',
1,
'3',
'-5',
'gadg',
);
my $valid_continent = $valid_continents[0];
my $valid_continent_vo = ValueObject::Geography::Continent->new($valid_continent);
# Instantiation Test
isa_ok($valid_continent_vo, 'ValueObject::Geography::Continent');
## Test return values
ok($valid_continent_vo->value() eq $valid_continent, "value function for object '$valid_continent'");
ok($valid_continent_vo eq $valid_continent, "stringify for object '$valid_continent'");
my $test_count = 4;
# Test valid/invalid examples
foreach my $test_valid_continent (@valid_continents) {
$test_count++;
isa_ok(ValueObject::Geography::Continent->new($test_valid_continent), 'ValueObject::Geography::Continent');
}
foreach my $test_invalid_continent (@invalid_continents) {
$test_count++;
throws_ok { ValueObject::Geography::Continent->new($test_invalid_continent) }
'ValueObject::Geography::Continent::Exception::Invalid',
'Invalid exception for ValueObject::Geography::Continent';
}
done_testing($test_count);
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