exercism-perl5

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

wordy.t (4661B) - 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 Wordy qw<answer>;
     10 
     11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
     12 
     13 imported_ok qw<answer> or bail_out;
     14 
     15 for my $case (@test_cases) {
     16   if ( !ref $case->{expected} ) {
     17     is( answer( $case->{input}{question} ),
     18       $case->{expected}, $case->{description}, );
     19   }
     20   else {
     21     like dies( sub { answer( $case->{input}{question} ) } ),
     22       qr/$case->{expected}{error}/, $case->{description};
     23   }
     24 }
     25 
     26 done_testing;
     27 
     28 __DATA__
     29 [
     30   {
     31     "description": "just a number",
     32     "expected": 5,
     33     "input": {
     34       "question": "What is 5?"
     35     },
     36     "property": "answer"
     37   },
     38   {
     39     "description": "addition",
     40     "expected": 2,
     41     "input": {
     42       "question": "What is 1 plus 1?"
     43     },
     44     "property": "answer"
     45   },
     46   {
     47     "description": "more addition",
     48     "expected": 55,
     49     "input": {
     50       "question": "What is 53 plus 2?"
     51     },
     52     "property": "answer"
     53   },
     54   {
     55     "description": "addition with negative numbers",
     56     "expected": -11,
     57     "input": {
     58       "question": "What is -1 plus -10?"
     59     },
     60     "property": "answer"
     61   },
     62   {
     63     "description": "large addition",
     64     "expected": 45801,
     65     "input": {
     66       "question": "What is 123 plus 45678?"
     67     },
     68     "property": "answer"
     69   },
     70   {
     71     "description": "subtraction",
     72     "expected": 16,
     73     "input": {
     74       "question": "What is 4 minus -12?"
     75     },
     76     "property": "answer"
     77   },
     78   {
     79     "description": "multiplication",
     80     "expected": -75,
     81     "input": {
     82       "question": "What is -3 multiplied by 25?"
     83     },
     84     "property": "answer"
     85   },
     86   {
     87     "description": "division",
     88     "expected": -11,
     89     "input": {
     90       "question": "What is 33 divided by -3?"
     91     },
     92     "property": "answer"
     93   },
     94   {
     95     "description": "multiple additions",
     96     "expected": 3,
     97     "input": {
     98       "question": "What is 1 plus 1 plus 1?"
     99     },
    100     "property": "answer"
    101   },
    102   {
    103     "description": "addition and subtraction",
    104     "expected": 8,
    105     "input": {
    106       "question": "What is 1 plus 5 minus -2?"
    107     },
    108     "property": "answer"
    109   },
    110   {
    111     "description": "multiple subtraction",
    112     "expected": 3,
    113     "input": {
    114       "question": "What is 20 minus 4 minus 13?"
    115     },
    116     "property": "answer"
    117   },
    118   {
    119     "description": "subtraction then addition",
    120     "expected": 14,
    121     "input": {
    122       "question": "What is 17 minus 6 plus 3?"
    123     },
    124     "property": "answer"
    125   },
    126   {
    127     "description": "multiple multiplication",
    128     "expected": -12,
    129     "input": {
    130       "question": "What is 2 multiplied by -2 multiplied by 3?"
    131     },
    132     "property": "answer"
    133   },
    134   {
    135     "description": "addition and multiplication",
    136     "expected": -8,
    137     "input": {
    138       "question": "What is -3 plus 7 multiplied by -2?"
    139     },
    140     "property": "answer"
    141   },
    142   {
    143     "description": "multiple division",
    144     "expected": 2,
    145     "input": {
    146       "question": "What is -12 divided by 2 divided by -3?"
    147     },
    148     "property": "answer"
    149   },
    150   {
    151     "description": "unknown operation",
    152     "expected": {
    153       "error": "unknown operation"
    154     },
    155     "input": {
    156       "question": "What is 52 cubed?"
    157     },
    158     "property": "answer"
    159   },
    160   {
    161     "description": "Non math question",
    162     "expected": {
    163       "error": "unknown operation"
    164     },
    165     "input": {
    166       "question": "Who is the President of the United States?"
    167     },
    168     "property": "answer"
    169   },
    170   {
    171     "description": "reject problem missing an operand",
    172     "expected": {
    173       "error": "syntax error"
    174     },
    175     "input": {
    176       "question": "What is 1 plus?"
    177     },
    178     "property": "answer"
    179   },
    180   {
    181     "description": "reject problem with no operands or operators",
    182     "expected": {
    183       "error": "syntax error"
    184     },
    185     "input": {
    186       "question": "What is?"
    187     },
    188     "property": "answer"
    189   },
    190   {
    191     "description": "reject two operations in a row",
    192     "expected": {
    193       "error": "syntax error"
    194     },
    195     "input": {
    196       "question": "What is 1 plus plus 2?"
    197     },
    198     "property": "answer"
    199   },
    200   {
    201     "description": "reject two numbers in a row",
    202     "expected": {
    203       "error": "syntax error"
    204     },
    205     "input": {
    206       "question": "What is 1 plus 2 1?"
    207     },
    208     "property": "answer"
    209   },
    210   {
    211     "description": "reject postfix notation",
    212     "expected": {
    213       "error": "syntax error"
    214     },
    215     "input": {
    216       "question": "What is 1 2 plus?"
    217     },
    218     "property": "answer"
    219   },
    220   {
    221     "description": "reject prefix notation",
    222     "expected": {
    223       "error": "syntax error"
    224     },
    225     "input": {
    226       "question": "What is plus 1 2?"
    227     },
    228     "property": "answer"
    229   }
    230 ]