exercism-perl5

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

luhn.t (3499B) - 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 Luhn qw<is_luhn_valid>;
     10 
     11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
     12 plan 19;
     13 
     14 imported_ok qw<is_luhn_valid> or bail_out;
     15 
     16 for my $case (@test_cases) {
     17     is( is_luhn_valid( $case->{input}{value} ),
     18         $case->{expected}
     19         ? T
     20         : DF,
     21         $case->{description}
     22     );
     23 }
     24 
     25 __DATA__
     26 [
     27   {
     28     "description": "single digit strings can not be valid",
     29     "expected": false,
     30     "input": {
     31       "value": "1"
     32     },
     33     "property": "valid"
     34   },
     35   {
     36     "description": "a single zero is invalid",
     37     "expected": false,
     38     "input": {
     39       "value": "0"
     40     },
     41     "property": "valid"
     42   },
     43   {
     44     "description": "a simple valid SIN that remains valid if reversed",
     45     "expected": true,
     46     "input": {
     47       "value": "059"
     48     },
     49     "property": "valid"
     50   },
     51   {
     52     "description": "a simple valid SIN that becomes invalid if reversed",
     53     "expected": true,
     54     "input": {
     55       "value": "59"
     56     },
     57     "property": "valid"
     58   },
     59   {
     60     "description": "a valid Canadian SIN",
     61     "expected": true,
     62     "input": {
     63       "value": "055 444 285"
     64     },
     65     "property": "valid"
     66   },
     67   {
     68     "description": "invalid Canadian SIN",
     69     "expected": false,
     70     "input": {
     71       "value": "055 444 286"
     72     },
     73     "property": "valid"
     74   },
     75   {
     76     "description": "invalid credit card",
     77     "expected": false,
     78     "input": {
     79       "value": "8273 1232 7352 0569"
     80     },
     81     "property": "valid"
     82   },
     83   {
     84     "description": "invalid long number with an even remainder",
     85     "expected": false,
     86     "input": {
     87       "value": "1 2345 6789 1234 5678 9012"
     88     },
     89     "property": "valid"
     90   },
     91   {
     92     "description": "valid number with an even number of digits",
     93     "expected": true,
     94     "input": {
     95       "value": "095 245 88"
     96     },
     97     "property": "valid"
     98   },
     99   {
    100     "description": "valid number with an odd number of spaces",
    101     "expected": true,
    102     "input": {
    103       "value": "234 567 891 234"
    104     },
    105     "property": "valid"
    106   },
    107   {
    108     "description": "valid strings with a non-digit added at the end become invalid",
    109     "expected": false,
    110     "input": {
    111       "value": "059a"
    112     },
    113     "property": "valid"
    114   },
    115   {
    116     "description": "valid strings with punctuation included become invalid",
    117     "expected": false,
    118     "input": {
    119       "value": "055-444-285"
    120     },
    121     "property": "valid"
    122   },
    123   {
    124     "description": "valid strings with symbols included become invalid",
    125     "expected": false,
    126     "input": {
    127       "value": "055# 444$ 285"
    128     },
    129     "property": "valid"
    130   },
    131   {
    132     "description": "single zero with space is invalid",
    133     "expected": false,
    134     "input": {
    135       "value": " 0"
    136     },
    137     "property": "valid"
    138   },
    139   {
    140     "description": "more than a single zero is valid",
    141     "expected": true,
    142     "input": {
    143       "value": "0000 0"
    144     },
    145     "property": "valid"
    146   },
    147   {
    148     "description": "input digit 9 is correctly converted to output digit 9",
    149     "expected": true,
    150     "input": {
    151       "value": "091"
    152     },
    153     "property": "valid"
    154   },
    155   {
    156     "description": "using ascii value for non-doubled non-digit isn't allowed",
    157     "expected": false,
    158     "input": {
    159       "value": "055b 444 285"
    160     },
    161     "property": "valid"
    162   },
    163   {
    164     "description": "using ascii value for doubled non-digit isn't allowed",
    165     "expected": false,
    166     "input": {
    167       "value": ":9"
    168     },
    169     "property": "valid"
    170   }
    171 ]