refactor test for clarity
[p5sagit/Import-Into.git] / t / import_into.t
CommitLineData
2afb5246 1use strict;
2use warnings FATAL => 'all';
1615d435 3use Test::More qw(no_plan);
2afb5246 4
5BEGIN {
2afb5246 6 package MyExporter;
a6e3c44c 7 $INC{"MyExporter.pm"} = __FILE__;
2afb5246 8
9 use base qw(Exporter);
10
11 our @EXPORT_OK = qw(thing);
12
13 sub thing { 'thing' }
a6e3c44c 14}
15
16my @importcaller;
17my $version;
18BEGIN {
19 package CheckFile;
20 $INC{"CheckFile.pm"} = __FILE__;
2afb5246 21
a6e3c44c 22 sub import {
23 @importcaller = caller;
24 }
25 sub VERSION {
26 $version = $_[1];
27 }
28}
cc087979 29
a6e3c44c 30BEGIN {
2afb5246 31 package MultiExporter;
a6e3c44c 32 $INC{"MultiExporter.pm"} = __FILE__;
2afb5246 33
34 use Import::Into;
35
36 sub import {
37 my $target = caller;
38 warnings->import::into($target);
39 MyExporter->import::into($target, 'thing');
e5f9555d 40 CheckFile->import::into(1);
2afb5246 41 }
e5f9555d 42}
43
8afc1658 44eval q{
2afb5246 45 package TestPackage;
46
b95fe8b9 47 no warnings FATAL => 'all';
2afb5246 48
8afc1658 49#line 1 "import_into_inline.pl"
2afb5246 50 use MultiExporter;
51
52 sub test {
53 thing . undef
54 }
8afc1658 55 1;
56} or die $@;
2afb5246 57
58my @w;
59
60is(do {
61 local $SIG{__WARN__} = sub { push @w, @_; };
8afc1658 62 TestPackage::test();
2afb5246 63}, 'thing', 'returned thing ok');
64
65is(scalar @w, 1, 'Only one entry in @w');
66
67like($w[0], qr/uninitialized/, 'Correct warning');
e5f9555d 68
a6e3c44c 69is $importcaller[0], 'TestPackage',
70 'import by level has correct package';
71is $importcaller[1], 'import_into_inline.pl',
72 'import by level has correct file';
73is $importcaller[2], 1,
74 'import by level has correct line';
568eef3e 75
a6e3c44c 76@importcaller = ();
77$version = undef;
568eef3e 78CheckFile->import::into({
79 package => 'ExplicitPackage',
80 filename => 'explicit-file.pl',
81 line => 42,
82 version => 219,
83});
84
a6e3c44c 85is $importcaller[0], 'ExplicitPackage',
86 'import with hash has correct package';
87is $importcaller[1], 'explicit-file.pl',
88 'import with hash has correct file';
89is $importcaller[2], 42,
90 'import with hash has correct line';
91is $version, 219,
92 'import with hash has correct version';
93
94BEGIN {
95 package LevelExporter;
96 $INC{'LevelExporter.pm'} = __FILE__;
97
98 sub import {
99 CheckFile->import::into({
100 level => 1,
101 version => 219,
102 });
103 }
104}
cc087979 105
106ok( !IPC::Open3->can("open3"), "IPC::Open3 is unloaded" );
107IPC::Open3->import::into("TestPackage");
108ok( TestPackage->can("open3"), "IPC::Open3 was use'd and import::into'd" );