Skip to content

Instantly share code, notes, and snippets.

@Woody2143
Last active December 22, 2015 07:48
Show Gist options
  • Save Woody2143/6440333 to your computer and use it in GitHub Desktop.
Save Woody2143/6440333 to your computer and use it in GitHub Desktop.
When I use 'use' I get the 'Use of uninitialized value in multiplication (*)' warning in the test; when 'require' is used I do not. Really that appears to be the only difference. Funny thing is the test still passes even with the 'uninitialized value' warning. So it's likely I'm missing something else entirely... Advice and criticisms are welcome.
#!/usr/bin/env perl
use Modern::Perl;
use Test::More;
require Money;
require Dollar;
require Franc;
subtest 'Dollar subtest' => sub {
my $five = new_ok( Dollar => [ amount => 5 ], 'Dollar');
note(explain $five);
is_deeply($five->times(2), Money->Dollar(10), 'product->times(2) equals Dollar obj 10');
is_deeply($five->times(3), Money->Dollar(15), 'product->times(3) equals Dollar obj 15');
};
subtest 'Franc subtest' => sub {
my $five = new_ok( Franc => [ amount => 5 ], 'Franc');
note(explain $five);
is_deeply($five->times(2), Money->Franc(10 ), 'product->times(2) equals Franc obj 10');
is_deeply($five->times(3), Money->Franc(15), 'product->times(3) equals Franc obj 15');
};
done_testing();
package Dollar;
use Modern::Perl;
use Moose;
use MooseX::Method::Signatures;
extends 'Money';
method times (Int $multiplier) {
return Dollar->new( amount => ($self->amount * $multiplier) );
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;
package Franc;
use Moose;
use MooseX::Method::Signatures;
use MooseX::Privacy;
extends 'Money';
method times (Int $multiplier) {
return Franc->new( amount => ($self->amount * ${multiplier}));
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;
package Money;
use Moose;
use MooseX::Method::Signatures;
use MooseX::Privacy;
has 'amount' => (
isa => 'Int',
is => 'ro',
traits => [qw/Protected/],
);
method equals (Money $object) {
return 1 if (($self->amount == $object->amount) && (ref $self eq ref $object));
return 0;
}
#I would use 'method' here but that fails to a validation error when called in the test
sub Dollar {
my $self = shift;
my ($amount) = @_;
use Dollar;
#require Dollar;
return Dollar->new( amount => $amount );
}
#I would use 'method' here but that fails to a validation error when called in the test
sub Franc {
my $self = shift;
my ($amount) = @_;
#use Franc;
require Franc;
return Franc->new( amount => $amount );
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;
$ prove -lv t/01-Multiplication.t
t/01-Multiplication.t ..
ok 1 - Dollar isa Dollar
# bless( {}, 'Dollar' )
Use of uninitialized value in multiplication (*) at /home/bwood/TDD_by_Example/Money/lib/Dollar.pm line 9.
ok 2 - product->times(2) equals Dollar obj 10
Use of uninitialized value in multiplication (*) at /home/bwood/TDD_by_Example/Money/lib/Dollar.pm line 9.
ok 3 - product->times(3) equals Dollar obj 15
1..3
ok 1 - Dollar subtest
ok 1 - Franc isa Franc
# bless( {
# 'amount' => 5
# }, 'Franc' )
ok 2 - product->times(2) equals Franc obj 10
ok 3 - product->times(3) equals Franc obj 15
1..3
ok 2 - Franc subtest
1..2
ok
All tests successful.
Files=1, Tests=2, 1 wallclock secs ( 0.02 usr 0.01 sys + 0.72 cusr 0.02 csys = 0.77 CPU)
Result: PASS
@Woody2143
Copy link
Author

I was just trying out 'subtests'; I'm not sure I like the look of the output...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment