Skip to content

Commit 9f33ab1

Browse files
Merge pull request #111 from exercism/i110
Do not assume default values
2 parents 4437154 + 2582a16 commit 9f33ab1

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

sum-of-multiples/Example.pm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use List::Util 'sum';
66

77
sub new {
88
my ($class, @multiples_of) = @_;
9-
@multiples_of = (3, 5) unless @multiples_of;
109

1110
bless { multiples_of => [@multiples_of] } => $class;
1211
}

sum-of-multiples/sum_of_multiples.t

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,70 @@ use Test::More;
55

66
my $module = $ENV{EXERCISM} ? 'Example' : 'SumOfMultiples';
77

8-
plan tests => 11;
8+
my @cases = (
9+
{
10+
factors => [3, 5],
11+
limit => 1,
12+
expected => 0
13+
},
14+
{
15+
factors => [3, 5],
16+
limit => 4,
17+
expected => 3
18+
},
19+
{
20+
factors => [3, 5],
21+
limit => 10,
22+
expected => 23
23+
},
24+
{
25+
factors => [3, 5],
26+
limit => 100,
27+
expected => 2318
28+
},
29+
{
30+
factors => [3, 5],
31+
limit => 1000,
32+
expected => 233168
33+
},
34+
{
35+
factors => [7, 13, 17],
36+
limit => 20,
37+
expected => 51
38+
},
39+
{
40+
factors => [4, 6],
41+
limit => 15,
42+
expected => 30
43+
},
44+
{
45+
factors => [5, 6, 8],
46+
limit => 150,
47+
expected => 4419
48+
},
49+
{
50+
factors => [5, 25],
51+
limit => 51,
52+
expected => 275
53+
},
54+
{
55+
factors => [43, 47],
56+
limit => 10000,
57+
expected => 2203160
58+
},
59+
{
60+
factors => [1],
61+
limit => 100,
62+
expected => 4950
63+
},
64+
{
65+
factors => [],
66+
limit => 10000,
67+
expected => 0
68+
}
69+
);
70+
71+
plan tests => 4 + scalar @cases;
972

1073
ok -e "$module.pm", "Missing $module.pm"
1174
or BAIL_OUT "You need to create file: $module.pm";
@@ -19,10 +82,13 @@ can_ok $module, "new"
1982
can_ok $module, "to"
2083
or BAIL_OUT "Missing package $module; or missing sub to()";
2184

22-
is $module->new->to(1), 0, "No multiples of 3 or 5 equals zero";
23-
is $module->new->to(4), 3, "One multiple of 3";
24-
is $module->new->to(10), 23, "Multiples of 3 and 5";
25-
is $module->new->to(100), 2_318, "Multiples of 3 and 5 to 100";
26-
is $module->new->to(1000), 233_168, "A lot of multiples of 3 and 5";
27-
is $module->new(7, 13, 17)->to(20), 51, "Multiples of 7, 13, 17";
28-
is $module->new(43, 47)->to(10_000), 2_203_160, "Multiples of 43, 47";
85+
86+
for my $case (@cases) {
87+
my @factors = @{$case->{factors}};
88+
my $desc = sprintf "Multiples of %s up to %s equals %s",
89+
(scalar @factors ? (join ' and ', @factors) : 'nothing'),
90+
$case->{limit}, $case->{expected};
91+
92+
is $module->new(@factors)->to($case->{limit}), $case->{expected}, $desc;
93+
}
94+

0 commit comments

Comments
 (0)