今回はequals()メソッドを定義してDollarオブジェクト同士の等値性を比較できるようにしています。
あと、書き忘れていましたが1回目でis()のactualとexpectedが逆だったので合わせて修正しました。
lib/Money.pm
package Money; use strict; use warnings; use version; our $VERSION = qv('0.0.3'); 1; package Dollar; use strict; use warnings; sub new { my ($class, $opts) =(@_); return bless $opts, $class; } sub times { my ($self, $multiplier) = (@_); return new Dollar({amount => $self->{amount} * $multiplier}); } sub equals { my ($self, $dollar) = (@_); return $self->{amount} == $dollar->{amount}; } 1;
t/01money.t
test_equality()のassert文がごちゃっとしていて気に入らないです。
use strict; use warnings; use Test::Class; Test::Class->runtests; package TestMoney; use strict; use warnings; use base 'Test::Class'; use Money; use Test::More; sub test_multiplication : Test(2) { my $five = new Dollar({amount => 5}); my $product = $five->times(2); is($product->{amount}, 10); $product = $five->times(3); is($product->{amount}, 15); } sub test_equality : Test(2) { ok( new Dollar({amount => 5})->equals(new Dollar({amount => 5}))); ok(not new Dollar({amount => 5})->equals(new Dollar({amount => 6}))); } 1;