use strict;
use warnings;
use File::Slurp;

my $text = join '', <DATA>;

# Different ways to load Errno (or not)
my %how = (
    none     => '',
    use      => 'use Errno;',
    evaluse  => 'eval "use Errno"; die $@ if $@;',
    implicit => 'my @implicitly_load_Errno_pm = keys(%!);',
);

while ( my ($name, $how) = each %how ) {
    $_ = $text;
    s/---name---/$how/;
    s/---UseErrno---/$how/ or die;
    write_file "t/in-pm-files-$name.t", $_;
}

__DATA__
# Automatically generated by Errno::AnyString 0.50 lib/t_in-pm-files.t.PL
# Check that the effects of loading Errno::AnyString materialize in other
# already-loaded modules.  Errno.pm loading = ---name---

use strict;
use warnings;

---UseErrno---

use Test::More;
use Test::NoWarnings;

use lib 't';
use Foo1;
use Foo2;
use Foo3;
use Foo4;
use Errno::AnyString qw/CUSTOM_ERRSTR_ERRNO custom_errstr/;

my @pkgs = map {"Foo$_"} (1 .. 4);
plan tests => @pkgs*6 + 1;

foreach my $pkg (@pkgs) {
    my $x = $pkg->new;

    $! = custom_errstr "string1";
    is $x->errstr, "string1", "$pkg special errstr set";
    is $x->errno, CUSTOM_ERRSTR_ERRNO, "$pkg special errno set";

    my $save = $!;

    $! = 0;
    isnt $x->errstr, "string1", "$pkg special errstr unset";
    isnt $x->errno, CUSTOM_ERRSTR_ERRNO, "$pkg special errno unset";

    $! = $save;
    is $x->errstr, "string1", "$pkg special errstr restored";
    is $x->errno, CUSTOM_ERRSTR_ERRNO, "$pkg special errno restored";
}

