-
Notifications
You must be signed in to change notification settings - Fork 20
/
money.pl
70 lines (53 loc) · 1.36 KB
/
money.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/perl -w
# Rip-off of the example given in the JUnit tutorial at
# http://junit.sourceforge.net/doc/testinfected/testing.htm
use strict;
use warnings;
package Money;
sub new {
my $class = shift;
my ($currency, $amount) = @_;
my $self = {currency => $currency, amount => $amount};
return bless $self, $class;
};
sub amount {
my $self = shift;
return($self->{amount});
};
sub currency {
my $self = shift;
return($self->{currency});
};
sub add {
my ($o1, $o2) = @_;
my $currency = $o1->currency;
my $result = $o1->amount + $o2->amount;
return(Money->new($currency => $result));
};
sub equals {
my ($o1, $o2) = @_;
return(0) unless UNIVERSAL::isa ($o2, ref($o1));
return ($o1->currency eq $o2->currency && $o1->amount eq $o2->amount);
};
package Local::Test::Money;
use base qw(Test::Class);
use Test::More;
my ($o1, $o2);
sub setup : Test(setup) {
$o1 = Money->new(USD => 12);
$o2 = Money->new(USD => 14);
};
sub add : Test(2) {
my $o3 = $o1->add($o2);
is($o3->amount, 26, 'add amount matches');
is($o3->currency, 'USD', 'add currency matches');
};
sub equals : Test(4) {
my $o3 = Money->new(USD => 12);
ok(! $o1->equals(undef), 'does not equal undef');
ok($o1->equals($o1), 'equals self');
ok($o1->equals($o3), 'equals copy of self');
ok(! $o1->equals($o2), 'does not equal different object');
}
package main;
Local::Test::Money->new->runtests;