proverb.t (3263B) - raw
1 #!/usr/bin/env perl 2 use strict; 3 use warnings; 4 5 use Test2::Bundle::More; 6 use JSON::PP qw(decode_json); 7 use FindBin qw($Bin); 8 use lib $Bin, "$Bin/local/lib/perl5"; 9 10 my $module = 'Proverb'; 11 my $sub = 'proverb'; 12 13 my $cases; 14 { 15 local $/ = undef; 16 $cases = decode_json scalar <DATA>; 17 } 18 19 plan 3 + @$cases; 20 21 ok -e "$Bin/$module.pm", "missing $module.pm" 22 or BAIL_OUT("You need to create a class called $module.pm"); 23 24 eval "use $module"; 25 ok !$@, "Cannot load $module.pm" 26 or BAIL_OUT("Does $module.pm compile? Does it end with 1; ?"); 27 28 can_ok( $module, 'proverb' ) 29 or BAIL_OUT("Missing package $module; or missing sub proverb()"); 30 31 $sub = "${module}::proverb"; 32 33 foreach my $c (@$cases) { 34 no strict 'refs'; 35 my $expected = join "" => @{ $c->{expected} }; 36 is $sub->( $c->{param}, $c->{qualifier} || "" ), $expected, $c->{name}; 37 } 38 39 __DATA__ 40 [ 41 { 42 "param" : ["nail", "shoe"], 43 "expected" : ["For want of a nail the shoe was lost.\n", 44 "And all for the want of a nail."], 45 "name" : "one consequence" 46 }, 47 { 48 "param" : ["nail", "shoe", "horse"], 49 "expected" : ["For want of a nail the shoe was lost.\n", 50 "For want of a shoe the horse was lost.\n", 51 "And all for the want of a nail."], 52 "name" : "two consequences" 53 }, 54 { 55 "param" : ["nail", "shoe", "horse", "rider"], 56 "expected" : ["For want of a nail the shoe was lost.\n", 57 "For want of a shoe the horse was lost.\n", 58 "For want of a horse the rider was lost.\n", 59 "And all for the want of a nail."], 60 "name" : "three consequences" 61 }, 62 { 63 "param" : ["key", "value"], 64 "expected" : ["For want of a key the value was lost.\n", 65 "And all for the want of a key."], 66 "name" : "one consequence, new items" 67 }, 68 { 69 "param" : ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], 70 "expected" : ["For want of a nail the shoe was lost.\n", 71 "For want of a shoe the horse was lost.\n", 72 "For want of a horse the rider was lost.\n", 73 "For want of a rider the message was lost.\n", 74 "For want of a message the battle was lost.\n", 75 "For want of a battle the kingdom was lost.\n", 76 "And all for the want of a nail."], 77 "name" : "whole proverb" 78 }, 79 { 80 "param" : ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], 81 "qualifier" : "horseshoe", 82 "expected" : ["For want of a nail the shoe was lost.\n", 83 "For want of a shoe the horse was lost.\n", 84 "For want of a horse the rider was lost.\n", 85 "For want of a rider the message was lost.\n", 86 "For want of a message the battle was lost.\n", 87 "For want of a battle the kingdom was lost.\n", 88 "And all for the want of a horseshoe nail."], 89 "name" : "whole proverb with qualifier" 90 } 91 ]