exercism-perl5

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

word-count.t (3570B) - 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 WordCount qw<count_words>;
     10 
     11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
     12 
     13 imported_ok qw<count_words> or bail_out;
     14 
     15 for my $case (@test_cases) {
     16   is( count_words( $case->{input}{sentence} ),
     17     $case->{expected}, $case->{description}, );
     18 }
     19 
     20 done_testing;
     21 
     22 __DATA__
     23 [
     24   {
     25     "description": "count one word",
     26     "expected": {
     27       "word": 1
     28     },
     29     "input": {
     30       "sentence": "word"
     31     },
     32     "property": "countWords"
     33   },
     34   {
     35     "description": "count one of each word",
     36     "expected": {
     37       "each": 1,
     38       "of": 1,
     39       "one": 1
     40     },
     41     "input": {
     42       "sentence": "one of each"
     43     },
     44     "property": "countWords"
     45   },
     46   {
     47     "description": "multiple occurrences of a word",
     48     "expected": {
     49       "blue": 1,
     50       "fish": 4,
     51       "one": 1,
     52       "red": 1,
     53       "two": 1
     54     },
     55     "input": {
     56       "sentence": "one fish two fish red fish blue fish"
     57     },
     58     "property": "countWords"
     59   },
     60   {
     61     "description": "handles cramped lists",
     62     "expected": {
     63       "one": 1,
     64       "three": 1,
     65       "two": 1
     66     },
     67     "input": {
     68       "sentence": "one,two,three"
     69     },
     70     "property": "countWords"
     71   },
     72   {
     73     "description": "handles expanded lists",
     74     "expected": {
     75       "one": 1,
     76       "three": 1,
     77       "two": 1
     78     },
     79     "input": {
     80       "sentence": "one,\ntwo,\nthree"
     81     },
     82     "property": "countWords"
     83   },
     84   {
     85     "description": "ignore punctuation",
     86     "expected": {
     87       "as": 1,
     88       "car": 1,
     89       "carpet": 1,
     90       "java": 1,
     91       "javascript": 1
     92     },
     93     "input": {
     94       "sentence": "car: carpet as java: javascript!!&@$%^&"
     95     },
     96     "property": "countWords"
     97   },
     98   {
     99     "description": "include numbers",
    100     "expected": {
    101       "1": 1,
    102       "2": 1,
    103       "testing": 2
    104     },
    105     "input": {
    106       "sentence": "testing, 1, 2 testing"
    107     },
    108     "property": "countWords"
    109   },
    110   {
    111     "description": "normalize case",
    112     "expected": {
    113       "go": 3,
    114       "stop": 2
    115     },
    116     "input": {
    117       "sentence": "go Go GO Stop stop"
    118     },
    119     "property": "countWords"
    120   },
    121   {
    122     "description": "with apostrophes",
    123     "expected": {
    124       "cry": 1,
    125       "don't": 2,
    126       "first": 1,
    127       "laugh": 1,
    128       "then": 1
    129     },
    130     "input": {
    131       "sentence": "First: don't laugh. Then: don't cry."
    132     },
    133     "property": "countWords"
    134   },
    135   {
    136     "description": "with quotations",
    137     "expected": {
    138       "and": 1,
    139       "between": 1,
    140       "can't": 1,
    141       "joe": 1,
    142       "large": 2,
    143       "tell": 1
    144     },
    145     "input": {
    146       "sentence": "Joe can't tell between 'large' and large."
    147     },
    148     "property": "countWords"
    149   },
    150   {
    151     "description": "substrings from the beginning",
    152     "expected": {
    153       "a": 1,
    154       "and": 1,
    155       "app": 1,
    156       "apple": 1,
    157       "between": 1,
    158       "can't": 1,
    159       "joe": 1,
    160       "tell": 1
    161     },
    162     "input": {
    163       "sentence": "Joe can't tell between app, apple and a."
    164     },
    165     "property": "countWords"
    166   },
    167   {
    168     "description": "multiple spaces not detected as a word",
    169     "expected": {
    170       "multiple": 1,
    171       "whitespaces": 1
    172     },
    173     "input": {
    174       "sentence": " multiple   whitespaces"
    175     },
    176     "property": "countWords"
    177   },
    178   {
    179     "description": "alternating word separators not detected as a word",
    180     "expected": {
    181       "one": 1,
    182       "three": 1,
    183       "two": 1
    184     },
    185     "input": {
    186       "sentence": ",\n,one,\n ,two \n 'three'"
    187     },
    188     "property": "countWords"
    189   }
    190 ]