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