series.t (2806B) - 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 Series qw<slices>; 10 11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; 12 13 imported_ok qw<slices> or bail_out; 14 15 for my $case (@test_cases) { 16 if ( ref $case->{expected} ne 'HASH' ) { 17 is( slices( $case->{input} ), 18 $case->{expected}, $case->{description}, ); 19 } 20 else { 21 like dies( sub { slices( $case->{input} ) } ), 22 qr/$case->{expected}{error}/, $case->{description}; 23 } 24 } 25 26 done_testing; 27 28 __DATA__ 29 [ 30 { 31 "description": "slices of one from one", 32 "expected": [ 33 "1" 34 ], 35 "input": { 36 "series": "1", 37 "sliceLength": 1 38 }, 39 "property": "slices" 40 }, 41 { 42 "description": "slices of one from two", 43 "expected": [ 44 "1", 45 "2" 46 ], 47 "input": { 48 "series": "12", 49 "sliceLength": 1 50 }, 51 "property": "slices" 52 }, 53 { 54 "description": "slices of two", 55 "expected": [ 56 "35" 57 ], 58 "input": { 59 "series": "35", 60 "sliceLength": 2 61 }, 62 "property": "slices" 63 }, 64 { 65 "description": "slices of two overlap", 66 "expected": [ 67 "91", 68 "14", 69 "42" 70 ], 71 "input": { 72 "series": "9142", 73 "sliceLength": 2 74 }, 75 "property": "slices" 76 }, 77 { 78 "description": "slices can include duplicates", 79 "expected": [ 80 "777", 81 "777", 82 "777", 83 "777" 84 ], 85 "input": { 86 "series": "777777", 87 "sliceLength": 3 88 }, 89 "property": "slices" 90 }, 91 { 92 "description": "slices of a long series", 93 "expected": [ 94 "91849", 95 "18493", 96 "84939", 97 "49390", 98 "93904", 99 "39042", 100 "90424", 101 "04243" 102 ], 103 "input": { 104 "series": "918493904243", 105 "sliceLength": 5 106 }, 107 "property": "slices" 108 }, 109 { 110 "description": "slice length is too large", 111 "expected": { 112 "error": "slice length cannot be greater than series length" 113 }, 114 "input": { 115 "series": "12345", 116 "sliceLength": 6 117 }, 118 "property": "slices" 119 }, 120 { 121 "description": "slice length cannot be zero", 122 "expected": { 123 "error": "slice length cannot be zero" 124 }, 125 "input": { 126 "series": "12345", 127 "sliceLength": 0 128 }, 129 "property": "slices" 130 }, 131 { 132 "description": "slice length cannot be negative", 133 "expected": { 134 "error": "slice length cannot be negative" 135 }, 136 "input": { 137 "series": "123", 138 "sliceLength": -1 139 }, 140 "property": "slices" 141 }, 142 { 143 "description": "empty series is invalid", 144 "expected": { 145 "error": "series cannot be empty" 146 }, 147 "input": { 148 "series": "", 149 "sliceLength": 1 150 }, 151 "property": "slices" 152 } 153 ]