Last active
August 12, 2020 11:44
-
-
Save j1n3l0/2117fd7a9b34ee76da3b70e7f8d70ed5 to your computer and use it in GitHub Desktop.
Investigating some interesting usage of Maybe[`a] observed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Test2::V0; | |
subtest 'Given a class with attribute: Maybe[`a]' => sub { | |
package Job { | |
use Moo; | |
use Types::Standard qw< :types >; | |
has title => ( is => 'ro', isa => Str ); | |
}; | |
package Person { | |
use Moo; | |
use experimental qw< signatures >; | |
use Type::Utils qw< class_type >; | |
use Types::Standard qw< :types >; | |
has job => ( is => 'ro', isa => Maybe [ class_type('Job') ] ); | |
has occupation => ( is => 'lazy', isa => Str ); | |
sub _build_occupation ($self) { | |
$self->job ? $self->job->title : 'Unemployed'; | |
} | |
}; | |
subtest 'When instantiated with: `a' => sub { | |
is( Person->new( job => Job->new( title => 'Developer' ) ), | |
object { call occupation => 'Developer' }, | |
'should return calculated value', | |
); | |
}; | |
subtest 'When instantiated with: `undef' => sub { | |
is( Person->new( job => undef ), | |
object { call occupation => 'Unemployed' }, | |
'should return default value', | |
); | |
}; | |
subtest 'When instantiated with: `' => sub { | |
is( Person->new(), | |
object { call occupation => 'Unemployed' }, | |
'should return default value', | |
); | |
}; | |
}; | |
done_testing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment