1
exercism fetch perl5 pascals-triangle

triangle.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
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More;
use JSON qw(from_json);
use FindBin;
my $dir;
BEGIN { $dir = $FindBin::Bin . '/' };
use lib $dir;

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

my $cases_file = "${dir}cases.json";
my $cases;
if (open my $fh, '<', $cases_file) {
    local $/ = undef;
    $cases = from_json scalar <$fh>;
} else {
    die "Could not open '$cases_file' $!";
}

plan tests => 5 + @$cases;

ok -e "${dir}${module}.pm", "missing $module.pm"
    or BAIL_OUT("You need to create a class called $module.pm with a constructor called new.");

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

foreach my $sub (qw|triangle is_triangle row|) {
    can_ok($module, $sub) or BAIL_OUT("Missing package $module; or missing sub $sub()");
}

foreach my $c (@$cases) {
    no strict 'refs';
    my $sub = "${module}::" . $c->{sub};
    if ($c->{sub} eq "triangle") {
        is_deeply $sub->($c->{param}), $c->{expected}, $c->{name};
    } else {
        is $sub->($c->{param}), $c->{expected}, $c->{name};
    }
}