exercism-perl5

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

matrix.t (2289B) - raw


      1 #!/usr/bin/env perl
      2 use Test2::V0;
      3 use JSON::PP;
      4 use constant JSON => JSON::PP->new;
      5 
      6 use FindBin qw<$Bin>;
      7 use lib $Bin, "$Bin/local/lib/perl5";
      8 
      9 use Matrix qw<row column>;
     10 
     11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
     12 
     13 imported_ok qw<row column> or bail_out;
     14 
     15 for my $case (@test_cases) {
     16   my $func;
     17   if ( $case->{property} eq 'row' ) {
     18     $func = \&row;
     19   }
     20   elsif ( $case->{property} eq 'column' ) {
     21     $func = \&column;
     22   }
     23 
     24   is( $func->( $case->{input} ),
     25     $case->{expected}, $case->{description}, );
     26 }
     27 
     28 done_testing;
     29 
     30 __DATA__
     31 [
     32   {
     33     "description": "extract row from one number matrix",
     34     "expected": [
     35       1
     36     ],
     37     "input": {
     38       "index": 1,
     39       "string": "1"
     40     },
     41     "property": "row"
     42   },
     43   {
     44     "description": "can extract row",
     45     "expected": [
     46       3,
     47       4
     48     ],
     49     "input": {
     50       "index": 2,
     51       "string": "1 2\n3 4"
     52     },
     53     "property": "row"
     54   },
     55   {
     56     "description": "extract row where numbers have different widths",
     57     "expected": [
     58       10,
     59       20
     60     ],
     61     "input": {
     62       "index": 2,
     63       "string": "1 2\n10 20"
     64     },
     65     "property": "row"
     66   },
     67   {
     68     "description": "can extract row from non-square matrix with no corresponding column",
     69     "expected": [
     70       8,
     71       7,
     72       6
     73     ],
     74     "input": {
     75       "index": 4,
     76       "string": "1 2 3\n4 5 6\n7 8 9\n8 7 6"
     77     },
     78     "property": "row"
     79   },
     80   {
     81     "description": "extract column from one number matrix",
     82     "expected": [
     83       1
     84     ],
     85     "input": {
     86       "index": 1,
     87       "string": "1"
     88     },
     89     "property": "column"
     90   },
     91   {
     92     "description": "can extract column",
     93     "expected": [
     94       3,
     95       6,
     96       9
     97     ],
     98     "input": {
     99       "index": 3,
    100       "string": "1 2 3\n4 5 6\n7 8 9"
    101     },
    102     "property": "column"
    103   },
    104   {
    105     "description": "can extract column from non-square matrix with no corresponding row",
    106     "expected": [
    107       4,
    108       8,
    109       6
    110     ],
    111     "input": {
    112       "index": 4,
    113       "string": "1 2 3 4\n5 6 7 8\n9 8 7 6"
    114     },
    115     "property": "column"
    116   },
    117   {
    118     "description": "extract column where numbers have different widths",
    119     "expected": [
    120       1903,
    121       3,
    122       4
    123     ],
    124     "input": {
    125       "index": 2,
    126       "string": "89 1903 3\n18 3 1\n9 4 800"
    127     },
    128     "property": "column"
    129   }
    130 ]