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