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 = (
'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;
=head1 NAME
ValueObject::Geography::Continent - Value object to represent continents
use ValueObject::Geography::Continent;
my $vo = ValueObject::Geography::Continent->new('europe');
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
=head1 SEE ALSO
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, @_);
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Exception;
use FindBin qw($Bin);
use lib "$Bin/../lib";
# Setup
my @valid_continents = (
'north america',
'south america'
my @invalid_continents = (
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) {
isa_ok(ValueObject::Geography::Continent->new($test_valid_continent), 'ValueObject::Geography::Continent');
foreach my $test_invalid_continent (@invalid_continents) {
throws_ok { ValueObject::Geography::Continent->new($test_invalid_continent) }
'Invalid exception for ValueObject::Geography::Continent';
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