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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#!perl -T
use strict;
use warnings;
=head1 TEST PURPOSE
These tests exercise that the polymorphic exporter-builder used when
Sub::Exporter's -import group is invoked.
They use Test::SubExporter::DashSetup, bundled in ./t/lib, which uses this
calling style.
=cut
use Test::More tests => 40;
BEGIN { use_ok('Sub::Exporter'); }
our $exporting_class = 'Test::SubExporter::DashSetup';
use lib 't/lib';
for my $iteration (1..2) {
{
package Test::SubExporter::SETUP;
use Sub::Exporter -setup => [ qw(X) ];
sub X { return "desired" }
package Test::SubExporter::SETUP::CONSUMER;
Test::SubExporter::SETUP->import(':all');
main::is(X(), "desired", "constructed importer (via -setup [LIST]) worked");
}
{
package Test::SubExporter::EXPORT_MISSING;
use Sub::Exporter -setup => [ qw(X) ];
package Test::SubExporter::SETUP::CONSUMER_OF_MISSING;
eval { Test::SubExporter::EXPORT_MISSING->import(':all') };
main::like(
$@,
qr/can't locate export/,
"croak if we're configured to export something that can't be found",
);
}
{
package Test::SubExporter::SETUPFAILURE;
eval { Sub::Exporter->import( -setup => sub { 1 }) };
main::like($@, qr/-setup failed validation/, "only [],{} ok for -setup");
}
package Test::SubExporter::DEFAULT;
main::use_ok($exporting_class);
use subs qw(xyzzy hello_sailor);
main::is(
xyzzy,
"Nothing happens.",
"DEFAULT: default export xyzzy works as expected"
);
main::is(
hello_sailor,
"Nothing happens yet.",
"DEFAULT: default export hello_sailor works as expected"
);
package Test::SubExporter::RENAME;
main::use_ok($exporting_class, xyzzy => { -as => 'plugh' });
use subs qw(plugh);
main::is(
plugh,
"Nothing happens.",
"RENAME: default export xyzzy=>plugh works as expected"
);
package Test::SubExporter::SAILOR;
main::use_ok($exporting_class, ':sailor');;
use subs qw(xyzzy hs_works hs_fails);
main::is(
xyzzy,
"Nothing happens.",
"SAILOR: default export xyzzy works as expected"
);
main::is(
hs_works,
"Something happens!",
"SAILOR: hs_works export works as expected"
);
main::is(
hs_fails,
"Nothing happens yet.",
"SAILOR: hs_fails export works as expected"
);
package Test::SubExporter::Z3;
main::use_ok($exporting_class, hello_sailor => { game => 'zork3' });
use subs qw(hello_sailor);
main::is(
hello_sailor,
"Something happens!",
"Z3: custom hello_sailor works as expected"
);
package Test::SubExporter::FROTZ_SAILOR;
main::use_ok($exporting_class, -sailor => { -prefix => 'frotz_' });
use subs map { "frotz_$_" }qw(xyzzy hs_works hs_fails);
main::is(
frotz_xyzzy,
"Nothing happens.",
"FROTZ_SAILOR: default export xyzzy works as expected"
);
main::is(
frotz_hs_works,
"Something happens!",
"FROTZ_SAILOR: hs_works export works as expected"
);
main::is(
frotz_hs_fails,
"Nothing happens yet.",
"FROTZ_SAILOR: hs_fails export works as expected"
);
}
{
package Test::SubExporter::SETUPALT;
use Sub::Exporter -setup => {
-as => 'alternimport',
exports => [ qw(Y) ],
};
sub X { return "desired" }
sub Y { return "other" }
package Test::SubExporter::SETUP::ALTCONSUMER;
Test::SubExporter::SETUPALT->import(':all');
eval { X() };
main::like($@, qr/undefined subroutine/i, "X didn't get imported");
eval { Y() };
main::like($@, qr/undefined subroutine/i, "Y didn't get imported");
Test::SubExporter::SETUPALT->alternimport(':all');
main::is(Y(), "other", "other importer (via -setup { -as ...}) worked");
}
|