#!perl
use strict;
use warnings;
use Test::More;

plan skip_all => 'Set DBIX_FAST_TEST=1 for POD syntax+coverage tests'
    unless $ENV{'DBIX_FAST_TEST'};

eval "use Test::Pod 1.52";
plan skip_all => "Test::Pod 1.52 required for testing POD" if $@;

eval "use Test::Pod::Coverage 1.10";
plan skip_all => "Test::Pod::Coverage 1.10 required for testing POD coverage" if $@;

eval "use Pod::Coverage 0.23";
plan skip_all => "Pod::Coverage 0.23 required for testing POD coverage" if $@;

# Object::Pad auto-generates DOES, META, new and accessor methods.
# Exclude them from coverage checks along with private methods.
my $trustme = { trustme => [qr/^(DOES|META|new|DESTROY)$/] };

# Also trust accessor/reader methods generated by :accessor/:reader
my %also_private = (
    # Query/CRUD methods and the statement-state accessors are composed in
    # from the DBIx::Fast::SQL role and documented in that module's POD.
    'DBIx::Fast' => {
        trustme => [qr/^(DOES|META|new|DESTROY|extra_args|q|all|flat|hash|val|array|query|count|exec|execute|insert|update|up|delete|upsert|insert_many|upsert_many|make_sen|execute_prepare|sql|p|results|last_id|last_sql)$/],
        also_private => [qr/^_/],
    },
    'DBIx::Fast::SQL' => {
        trustme => [qr/^(DOES|META|new|DESTROY|extra_args|sql|p|results|last_id|last_sql)$/],
        also_private => [qr/^_/],
    },
    'DBIx::Fast::Base' => $trustme,
    'DBIx::Fast::Connector' => $trustme,
    'DBIx::Fast::Schema' => {
        trustme => [qr/^(DOES|META|new|DESTROY|get_indexs)$/],
    },
    'DBIx::Fast::Transaction' => {
        trustme => [qr/^(DOES|META|new|DESTROY|level|savepoints|start_time|stats)$/],
    },
    'DBIx::Fast::Profiler' => {
        trustme => [qr/^(DOES|META|new|DESTROY|auto_print|enabled|max_queries|queries|slow_query|output|output_fh|output_is_text|out|outf|emit)$/],
    },
    # output/out/outf/emit are composed in from the DBIx::Fast::Output role,
    # documented in that module's POD.
    'DBIx::Fast::Output' => {
        trustme => [qr/^(DOES|META|new|DESTROY|out|outf|output_is_text)$/],
    },
    'DBIx::Fast::Profile::Base' => {
        trustme => [qr/^(DOES|META|new|DESTROY|dbix|enabled|slow_query)$/],
    },
    'DBIx::Fast::Profile::MariaDB' => {
        trustme => [qr/^(DOES|META|new|DESTROY|get_connection_status|get_connection_usage|get_connection_variables|get_current_connections|get_processes)$/],
    },
    'DBIx::Fast::Profile::SQLite' => {
        trustme => [qr/^(DOES|META|new|DESTROY|init|get_slow_queries|get_query_profile|get_stats|print_stats|explain_query|get_table_stats|get_index_stats|analyze_indexes|print_index_analysis|get_index_metrics|monitor_connections|get_current_connections|get_processes|print_connection_monitor|start_connection_monitor)$/],
    },
    'DBIx::Fast::Profile::Pg' => {
        trustme => [qr/^(DOES|META|new|DESTROY|init)$/],
    },
    # Inherits Profile::MariaDB wholesale; its one override is documented in
    # the DESCRIPTION prose.
    'DBIx::Fast::Profile::mysql' => {
        trustme => [qr/^(DOES|META|new|DESTROY|explain_query)$/],
    },
    'DBIx::Fast::Result' => {
        trustme => [qr/^(DOES|META|new|DESTROY|sth)$/],
    },
    'DBIx::Fast::Cache' => {
        trustme => [qr/^(DOES|META|new|DESTROY|backend|default_ttl|max_size)$/],
    },
    'DBIx::Fast::Cached' => {
        trustme => [qr/^(DOES|META|new|DESTROY|dbix)$/],
    },
);

# The driver-diagnostics interface is documented as a group in Profile::Base
# (see its "Driver diagnostics interface" section), not per =head2; the
# output|out|outf|emit methods are composed in from DBIx::Fast::Output.
$also_private{'DBIx::Fast::Profile::Base'} = {
    trustme => [qr/^(DOES|META|new|DESTROY|dbix|enabled|slow_query|add_query|get_query_profile|explain_query|get_table_stats|get_index_stats|analyze_indexes|print_index_analysis|get_index_metrics|monitor_connections|get_current_connections|get_processes|print_connection_monitor|start_connection_monitor|output|output_fh|output_is_text|out|outf|emit)$/],
};

# POD syntax on every file in lib/ (pod_file_ok per file: all_pod_files_ok
# would set its own plan and clash with done_testing)
pod_file_ok($_) for all_pod_files('lib');

for my $module (sort keys %also_private) {
    pod_coverage_ok($module, $also_private{$module}, "Pod coverage on $module");
}

done_testing();
