add travis config
[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 @versioncaller;
18 my $version;
19 BEGIN {
20   package CheckFile;
21   $INC{"CheckFile.pm"} = __FILE__;
22
23   sub import {
24     @importcaller = caller;
25   }
26   sub VERSION {
27     $version = $_[1];
28     @versioncaller = caller;
29   }
30 }
31
32 BEGIN {
33   package MultiExporter;
34   $INC{"MultiExporter.pm"} = __FILE__;
35
36   use Import::Into;
37
38   sub import {
39     my $target = caller;
40     warnings->import::into($target);
41     MyExporter->import::into($target, 'thing');
42     CheckFile->import::into(1);
43   }
44 }
45
46 eval q{
47   package TestPackage;
48
49   no warnings FATAL => 'all';
50
51 #line 1 "import_into_inline.pl"
52   use MultiExporter;
53
54   sub test {
55     thing . undef
56   }
57   1;
58 } or die $@;
59
60 my @w;
61
62 is(do {
63   local $SIG{__WARN__} = sub { push @w, @_; };
64   TestPackage::test();
65 }, 'thing', 'returned thing ok');
66
67 is(scalar @w, 1, 'Only one entry in @w');
68
69 like($w[0], qr/uninitialized/, 'Correct warning');
70
71 is $importcaller[0], 'TestPackage',
72   'import by level has correct package';
73 is $importcaller[1], 'import_into_inline.pl',
74   'import by level has correct file';
75 is $importcaller[2], 1,
76   'import by level has correct line';
77 is scalar @versioncaller, 0, 'VERSION not called when not specified';
78
79 @importcaller = ();
80 @versioncaller = ();
81 $version = undef;
82 CheckFile->import::into({
83   package  => 'ExplicitPackage',
84   filename => 'explicit-file.pl',
85   line     => 42,
86   version  => 219,
87 });
88
89 is $importcaller[0], 'ExplicitPackage',
90   'import with hash has correct package';
91 is $importcaller[1], 'explicit-file.pl',
92   'import with hash has correct file';
93 is $importcaller[2], 42,
94   'import with hash has correct line';
95 is $versioncaller[0], 'ExplicitPackage',
96   'VERSION with hash has correct package';
97 is $versioncaller[1], 'explicit-file.pl',
98   'VERSION with hash has correct file';
99 is $versioncaller[2], 42,
100   'VERSION with hash has correct line';
101 is $version, 219,
102   'import with hash has correct version';
103
104 BEGIN {
105   package LevelExporter;
106   $INC{'LevelExporter.pm'} = __FILE__;
107
108   sub import {
109     CheckFile->import::into({
110       level    => 1,
111       version  => 219,
112     });
113   }
114 }
115
116 @importcaller = ();
117 @versioncaller = ();
118 $version = undef;
119 eval q{
120   package ExplicitLevel;
121
122 #line 42 "explicit-level.pl"
123   use LevelExporter;
124   1;
125 } or die $@;
126
127 is $importcaller[0], 'ExplicitLevel',
128   'import with level in hash has correct package';
129 is $importcaller[1], 'explicit-level.pl',
130   'import with level in hash has correct file';
131 is $importcaller[2], 42,
132   'import with level in hash has correct line';
133 is $versioncaller[0], 'ExplicitLevel',
134   'VERSION with level in hash has correct package';
135 is $versioncaller[1], 'explicit-level.pl',
136   'VERSION with level in hash has correct file';
137 is $versioncaller[2], 42,
138   'VERSION with level in hash has correct line';
139 is $version, 219,
140   'import with level in hash has correct version';
141
142 ok( !IPC::Open3->can("open3"), "IPC::Open3 is unloaded" );
143 IPC::Open3->import::into("TestPackage");
144 ok( TestPackage->can("open3"), "IPC::Open3 was use'd and import::into'd" );