#! perl


use Test2::V0;
use Test2::Tools::Exports;
use PGPLOTx::Constants ();
use JSON::PP;
use Path::Tiny;
use experimental 'declared_refs', 'refaliasing';

## no critic ( NoStrict StringyEval )


my \%documented = decode_json( path( 't/constants.json' )->slurp );

for my $cat ( sort keys %documented ) {

    subtest "Category: $cat" => sub {
        my $names_sub = "${cat}_NAMES";
        my $tag       = q{-} . lc $cat;

        my @documented_names = @{ $documented{$cat} };

        subtest 'import functions individually' => sub {
            my $pkg = "My::Test::Helpers::$cat";

            {
                no strict 'refs';
                eval <<~"EOT" or die $@;
                package $pkg;
                use Test2::V0;
                use Test2::Tools::Exports;
                use PGPLOTx::Constants qw( $cat $names_sub @documented_names );

                # ensure imports
                subtest 'helper funcs' => sub {
                    imported_ok( qw( $cat $names_sub ) );
                    is( [ $names_sub()],
                        bag { item \$_ for qw( @documented_names ); end; },
                        '$names_sub values' );

                    # can't directly compare values (well could, but
                    # that would mean embedding them here), so check count
                    is( 0+@{[$cat]}, @{[ 0+ @documented_names]} , "$cat returns expected count" );

                    };

                subtest 'constants' => sub {
                    imported_ok( qw( @documented_names ) );
                };

                1;
               EOT
            }

        };


        subtest "Tag import: $tag" => sub {
            my $pkg = "My::Test::Tag::$cat";

            {
                no strict 'refs';
                eval <<~"EOT" or die $@;
                package $pkg;
                use Test2::V0;
                use Test2::Tools::Exports;
                use PGPLOTx::Constants '$tag';

                subtest 'constants' => sub {
                    imported_ok( qw( @documented_names ) );
                };

                1;
                EOT
            }

        };
    };
}

subtest 'global as => lc' => sub {

    {
        package My::Test::GlobalAs;
        use Test2::V0;
        use Test2::Tools::Exports;
        use PGPLOTx::Constants 'COLORS_NAMES';
        use PGPLOTx::Constants { as => 'lc' }, '-colors';

        subtest 'constants' => sub {
            imported_ok( map { lc } COLORS_NAMES );
        };
    }

};

subtest 'tag -as => lc' => sub {

    {
        package My::Test::SymbolAs;
        use Test2::V0;
        use Test2::Tools::Exports;
        use PGPLOTx::Constants 'COLORS_NAMES';
        use PGPLOTx::Constants -colors, { -as => 'lc' };

        subtest 'constants' => sub {
            imported_ok( map { lc } COLORS_NAMES );
        };
    }

};

done_testing;
