summaryrefslogtreecommitdiffstats
path: root/t/014-substrings.t
blob: 542ad644341d5c0463999aebea8818d5c41d5cc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!perl -T
use strict;
use warnings;
use Test::More;
use Test::Deep;
use List::Util qw/sum/;
use Games::Word qw/is_substring all_substrings/;

my %is_substring_tests = (
    ""      => [""],
    "abc",  => ["", "abc", "ab", "ac"],
    "aaba"  => ["a", "aa", "aaa", "aab", "aba"],
    "abcba" => ["aa", "bb", "c", "abc", "cba", "abba"],
);
my %isnt_substring_tests = (
    ""      => ["a"],
    "abc"   => ["z", "ba", "baz", "abz"],
    "aaba"  => ["c", "abaa"],
);
my %all_substrings_tests = (
    ""    => [''],
    "a"   => ['', "a"],
    "ab"  => ['', "a", "b", "ab"],
    "aab" => ['', "a", "a", "b", "aa", "ab", "ab", "aab"],
    "abc" => ['', "a", "b", "c", "ab", "ac", "bc", "abc"],
);
plan tests => (sum map { scalar @$_ } values %is_substring_tests,
                                      values %isnt_substring_tests) +
              keys %all_substrings_tests;

while (my ($word, $substrings) = each %is_substring_tests) {
    ok(is_substring($_, $word), "is '$_' a substring of '$word'?")
        for @$substrings;
}
while (my ($word, $substrings) = each %isnt_substring_tests) {
    ok(!is_substring($_, $word), "is '$_' not a substring of '$word'?")
        for @$substrings;
}
while (my ($word, $substrings) = each %all_substrings_tests) {
    cmp_deeply([all_substrings($word)], bag(@$substrings),
               "do we get all of the substrings of '$word'?");
}