Commit | Line | Data |
2ee1d506 |
1 | #!perl -w |
94dd7035 |
2 | |
3 | BEGIN { |
2ee1d506 |
4 | if( $ENV{PERL_CORE} ) { |
5 | chdir 't' if -d 't'; |
6 | @INC = '../lib'; |
7 | } |
94dd7035 |
8 | } |
9 | |
9ebf09bc |
10 | # Can't use Test::Simple/More, they depend on Exporter. |
452617c3 |
11 | my $test; |
94dd7035 |
12 | sub ok ($;$) { |
9ebf09bc |
13 | my($ok, $name) = @_; |
14 | |
15 | # You have to do it this way or VMS will get confused. |
16 | printf "%sok %d%s\n", ($ok ? '' : 'not '), $test, |
17 | (defined $name ? " - $name" : ''); |
18 | |
19 | printf "# Failed test at line %d\n", (caller)[2] unless $ok; |
20 | |
21 | $test++; |
22 | return $ok; |
94dd7035 |
23 | } |
24 | |
25 | |
452617c3 |
26 | BEGIN { |
27 | $test = 1; |
28 | print "1..28\n"; |
29 | require Exporter; |
30 | ok( 1, 'Exporter compiled' ); |
31 | } |
94dd7035 |
32 | |
33 | |
34 | BEGIN { |
35 | # Methods which Exporter says it implements. |
36 | @Exporter_Methods = qw(import |
37 | export_to_level |
38 | require_version |
39 | export_fail |
40 | ); |
41 | } |
42 | |
94dd7035 |
43 | |
44 | package Testing; |
45 | require Exporter; |
46 | @ISA = qw(Exporter); |
47 | |
48 | # Make sure Testing can do everything its supposed to. |
49 | foreach my $meth (@::Exporter_Methods) { |
50 | ::ok( Testing->can($meth), "subclass can $meth()" ); |
51 | } |
52 | |
53 | %EXPORT_TAGS = ( |
54 | This => [qw(stuff %left)], |
55 | That => [qw(Above the @wailing)], |
56 | tray => [qw(Fasten $seatbelt)], |
57 | ); |
d7341064 |
58 | @EXPORT = qw(lifejacket is); |
94dd7035 |
59 | @EXPORT_OK = qw(under &your $seat); |
60 | $VERSION = '1.05'; |
61 | |
62 | ::ok( Testing->require_version(1.05), 'require_version()' ); |
63 | eval { Testing->require_version(1.11); 1 }; |
64 | ::ok( $@, 'require_version() fail' ); |
65 | ::ok( Testing->require_version(0), 'require_version(0)' ); |
66 | |
67 | sub lifejacket { 'lifejacket' } |
68 | sub stuff { 'stuff' } |
69 | sub Above { 'Above' } |
70 | sub the { 'the' } |
71 | sub Fasten { 'Fasten' } |
72 | sub your { 'your' } |
73 | sub under { 'under' } |
74 | use vars qw($seatbelt $seat @wailing %left); |
75 | $seatbelt = 'seatbelt'; |
76 | $seat = 'seat'; |
77 | @wailing = qw(AHHHHHH); |
78 | %left = ( left => "right" ); |
79 | |
d7341064 |
80 | BEGIN {*is = \&Is}; |
81 | sub Is { 'Is' }; |
94dd7035 |
82 | |
09e96b99 |
83 | Exporter::export_ok_tags(); |
94dd7035 |
84 | |
85 | my %tags = map { $_ => 1 } map { @$_ } values %EXPORT_TAGS; |
86 | my %exportok = map { $_ => 1 } @EXPORT_OK; |
87 | my $ok = 1; |
88 | foreach my $tag (keys %tags) { |
89 | $ok = exists $exportok{$tag}; |
90 | } |
91 | ::ok( $ok, 'export_ok_tags()' ); |
92 | |
93 | |
94 | package Foo; |
95 | Testing->import; |
96 | |
97 | ::ok( defined &lifejacket, 'simple import' ); |
98 | |
d7341064 |
99 | my $got = eval {&lifejacket}; |
100 | ::ok ( $@ eq "", 'check we can call the imported subroutine') |
101 | or print STDERR "# \$\@ is $@\n"; |
102 | ::ok ( $got eq 'lifejacket', 'and that it gave the correct result') |
103 | or print STDERR "# expected 'lifejacket', got " . |
104 | (defined $got ? "'$got'" : "undef") . "\n"; |
105 | |
106 | # The string eval is important. It stops $Foo::{is} existing when |
107 | # Testing->import is called. |
108 | ::ok( eval "defined &is", |
109 | "Import a subroutine where exporter must create the typeglob" ); |
57ddaf2a |
110 | $got = eval "&is"; |
d7341064 |
111 | ::ok ( $@ eq "", 'check we can call the imported autoloaded subroutine') |
112 | or chomp ($@), print STDERR "# \$\@ is $@\n"; |
113 | ::ok ( $got eq 'Is', 'and that it gave the correct result') |
114 | or print STDERR "# expected 'Is', got " . |
115 | (defined $got ? "'$got'" : "undef") . "\n"; |
116 | |
94dd7035 |
117 | |
118 | package Bar; |
119 | my @imports = qw($seatbelt &Above stuff @wailing %left); |
120 | Testing->import(@imports); |
121 | |
122 | ::ok( (!grep { eval "!defined $_" } map({ /^\w/ ? "&$_" : $_ } @imports)), |
123 | 'import by symbols' ); |
124 | |
125 | |
126 | package Yar; |
127 | my @tags = qw(:This :tray); |
128 | Testing->import(@tags); |
129 | |
130 | ::ok( (!grep { eval "!defined $_" } map { /^\w/ ? "&$_" : $_ } |
131 | map { @$_ } @{$Testing::EXPORT_TAGS{@tags}}), |
132 | 'import by tags' ); |
133 | |
134 | |
135 | package Arrr; |
136 | Testing->import(qw(!lifejacket)); |
137 | |
138 | ::ok( !defined &lifejacket, 'deny import by !' ); |
139 | |
140 | |
141 | package Mars; |
142 | Testing->import('/e/'); |
143 | |
144 | ::ok( (!grep { eval "!defined $_" } map { /^\w/ ? "&$_" : $_ } |
145 | grep { /e/ } @Testing::EXPORT, @Testing::EXPORT_OK), |
146 | 'import by regex'); |
147 | |
148 | |
149 | package Venus; |
150 | Testing->import('!/e/'); |
151 | |
152 | ::ok( (!grep { eval "defined $_" } map { /^\w/ ? "&$_" : $_ } |
153 | grep { /e/ } @Testing::EXPORT, @Testing::EXPORT_OK), |
154 | 'deny import by regex'); |
155 | ::ok( !defined &lifejacket, 'further denial' ); |
156 | |
157 | |
158 | package More::Testing; |
159 | @ISA = qw(Exporter); |
160 | $VERSION = 0; |
161 | eval { More::Testing->require_version(0); 1 }; |
162 | ::ok(!$@, 'require_version(0) and $VERSION = 0'); |
163 | |
164 | |
165 | package Yet::More::Testing; |
166 | @ISA = qw(Exporter); |
167 | $VERSION = 0; |
168 | eval { Yet::More::Testing->require_version(10); 1 }; |
169 | ::ok($@ !~ /\(undef\)/, 'require_version(10) and $VERSION = 0'); |
9ebf09bc |
170 | |
171 | |
172 | my $warnings; |
173 | BEGIN { |
d6235ae5 |
174 | local $SIG{__WARN__} = sub { $warnings = join '', @_ }; |
9ebf09bc |
175 | package Testing::Unused::Vars; |
176 | @ISA = qw(Exporter); |
177 | @EXPORT = qw(this $TODO that); |
178 | |
179 | package Foo; |
180 | Testing::Unused::Vars->import; |
181 | } |
182 | |
183 | ::ok( !$warnings, 'Unused variables can be exported without warning' ) || |
184 | print "# $warnings\n"; |
185 | |
364e1267 |
186 | package Moving::Target; |
187 | @ISA = qw(Exporter); |
188 | @EXPORT_OK = qw (foo); |
189 | |
57ddaf2a |
190 | sub foo {"This is foo"}; |
191 | sub bar {"This is bar"}; |
364e1267 |
192 | |
193 | package Moving::Target::Test; |
194 | |
57ddaf2a |
195 | Moving::Target->import ('foo'); |
364e1267 |
196 | |
57ddaf2a |
197 | ::ok (foo() eq "This is foo", "imported foo before EXPORT_OK changed"); |
364e1267 |
198 | |
199 | push @Moving::Target::EXPORT_OK, 'bar'; |
200 | |
57ddaf2a |
201 | Moving::Target->import ('bar'); |
364e1267 |
202 | |
57ddaf2a |
203 | ::ok (bar() eq "This is bar", "imported bar after EXPORT_OK changed"); |
fe43f860 |
204 | |
205 | package The::Import; |
206 | |
207 | use Exporter 'import'; |
208 | |
fe43f860 |
209 | ::ok(\&import == \&Exporter::import, "imported the import routine"); |
210 | |
211 | @EXPORT = qw( wibble ); |
212 | sub wibble {return "wobble"}; |
213 | |
214 | package Use::The::Import; |
215 | |
216 | The::Import->import; |
217 | |
218 | my $val = eval { wibble() }; |
219 | ::ok($val eq "wobble", "exported importer worked"); |
220 | |