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