### ----------------------------------------------------
### -- Documentable
### -- Licenses: Artistic-2.0
### -- Authors: @moritz, @finanalyst, @tbrowder, @AlexDaniel, @antoniogamiz, @JJ
### -- File: t/305-search.t
### ----------------------------------------------------
use Test;

use Documentable::Registry;
use Documentable::Search;
use Documentable::Utils::IO;

constant TOPDIR = "t/test-doc";

# delete-cache-for(TOPDIR);

my $registry = Documentable::Registry.new(
    :topdir(TOPDIR),
    :dirs(["Language"]),
    :!verbose
);

$registry.compose;

my $search-generator = Documentable::Search.new;

my @expected = [
    category => "Language", value => "Operators", url => "language/operators",
    category => "Language", value => "Perl 6 by example", url => "language/101-basics",
    category => "Language", value => "Terms", url => "language/terms"
];

my @index = $search-generator.generate-entries($registry);

subtest "search index generation" => {
    for @index Z @expected -> $entry {
        subtest "{$entry[0]} detection" => {
                test-index-entry($entry[0], /{$entry[1].<category>}/, "category");
                test-index-entry($entry[0], /{$entry[1].<category>}/, "value"   );
                test-index-entry($entry[0], /{$entry[1].<category>}/, "url"     );
        }
    }
}

subtest "Final dot entry" => {
    my $entry = $search-generator.search-entry(:category("routine"), :value("infix ."), :url("/routine/."));
    ok $entry ~~ /"/routine/..html"/, "Add .html to urls ending in dot";
}

sub test-index-entry($index-entry, $regex, $msg) {
    is so $index-entry ~~ $regex, True, $msg;
}

done-testing;


