Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Last active August 12, 2020 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j1n3l0/2117fd7a9b34ee76da3b70e7f8d70ed5 to your computer and use it in GitHub Desktop.
Save j1n3l0/2117fd7a9b34ee76da3b70e7f8d70ed5 to your computer and use it in GitHub Desktop.
Investigating some interesting usage of Maybe[`a] observed
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