1
exercism fetch perl5 say

say.t

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More;
use FindBin;
my $dir;
BEGIN { $dir = $FindBin::Bin . '/' };
use lib $dir;

my @cases = (
    {
        input    => 0,
        expected => 'zero',
    },
    {
        input    => 1,
        expected => 'one',
    },
    {
        input    => 14,
        expected => 'fourteen',
    },
    {
        input    => 20,
        expected => 'twenty',
    },
    {
        input    => 22,
        expected => 'twenty-two',
    },
    {
        input    => 120,
        expected => 'one hundred twenty',
    },
    {
        input    => 123,
        expected => 'one hundred twenty-three',
    },
    {
        input    => 1000,
        expected => 'one thousand',
    },
    {
        input    => 1234,
        expected => 'one thousand two hundred thirty-four',
    },
    {
        input    => 1_000_000,
        expected => 'one million',
    },
    {
        input    => 1_000_002,
        expected => 'one million two',
    },
    {
        input    => 1_002_345,
        expected => 'one million two thousand three hundred forty-five',
    },
    {
        input    => 1_000_000_000,
        expected => 'one billion',
    },
    {
        input    => 987_654_321_123,
        expected => 'nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three',
    },
    {
        input     => -1,
        exception => "ArgumentError",
        name      => "Number too small",
    },
    {
        input     => 1_000_000_000_000,
        exception => "ArgumentError",
        name      => "Number too big"
    }
);

my $module = $ENV{EXERCISM} ? 'Example' : 'Say';

plan tests => 3 + @cases;

ok -e "${dir}${module}.pm", "Missing $module.pm" or BAIL_OUT "You need to create file $module.pm";

eval "use $module";
ok !$@, "Cannot load $module" or BAIL_OUT "Does $module.pm compile? Does it end with 1;?";

can_ok $module, "in_english" or BAIL_OUT "Missing package $module; or missing sub in_english()";

foreach my $c (@cases) {
    my $say;
    eval {
        $say = $module->new($c->{input});
    };

    if ($@) {
        no warnings 'uninitialized';
        like $@, qr/$c->{exception}/, $c->{name};
    } else {
        is $say->in_english(), $c->{expected}, "test " . $c->{expected};
    }
}