exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

prime.t (1723B) - raw


      1 #!/usr/bin/env perl
      2 use strict;
      3 use warnings;
      4 
      5 my $module = 'Prime';
      6 
      7 use Test2::Bundle::More;
      8 use JSON::PP qw(decode_json);
      9 use FindBin qw($Bin);
     10 use lib $Bin, "$Bin/local/lib/perl5";
     11 
     12 my $cases;
     13 {
     14   local $/ = undef;
     15   $cases = decode_json scalar <DATA>;
     16 }
     17 
     18 #plan 3 + @$cases;
     19 #diag explain $cases;
     20 
     21 ok -e "$Bin/$module.pm", "missing $module.pm"
     22   or BAIL_OUT(
     23   "You need to create a class called $module.pm with a constructor called factors."
     24   );
     25 
     26 eval "use $module";
     27 ok !$@, "Cannot load $module.pm"
     28   or BAIL_OUT("Does $module.pm compile?  Does it end with 1; ? ($@)");
     29 
     30 can_ok( $module, 'factors' )
     31   or BAIL_OUT("Missing package $module; or missing sub factors()");
     32 
     33 my $sub = $module . '::factors';
     34 
     35 foreach my $c (@$cases) {
     36   no strict 'refs';
     37   is_deeply $sub->( $c->{input} ), $c->{expected}, $c->{name};
     38 }
     39 
     40 done_testing();
     41 
     42 __DATA__
     43 [
     44   {
     45      "input" : 1,
     46      "expected" : [],
     47      "name" : "test_1"
     48   },
     49   {
     50      "input" : 2,
     51      "expected" : [2],
     52      "name" : "test_2"
     53   },
     54   {
     55      "input" : 3,
     56      "expected" : [3],
     57      "name" : "test_3"
     58   },
     59   {
     60     "input" : 4,
     61     "expected" : [2, 2],
     62     "name" : "test_4"
     63   },
     64   {
     65     "input" : 6,
     66     "expected" : [2, 3],
     67     "name" : "test_6"
     68   },
     69   {
     70     "input" : 8,
     71     "expected" : [2, 2, 2],
     72     "name" : "test_8"
     73   },
     74   {
     75     "input" : 9,
     76     "expected" : [3, 3],
     77     "name" : "test_9"
     78   },
     79   {
     80     "input" : 27,
     81     "expected" : [3, 3, 3],
     82     "name" : "test_27"
     83   },
     84   {
     85     "input" : 625,
     86     "expected" : [5, 5, 5, 5],
     87     "name" : "test_625"
     88   },
     89   {
     90     "input" : 901255,
     91     "expected" : [5, 17, 23, 461],
     92     "name" : "test_901255"
     93   },
     94   {
     95     "input" : 93819012551,
     96     "expected" : [11, 9539, 894119],
     97     "name" : "test_93819012551"
     98   }
     99 ]