3 # Can't use Test::Simple/More, they depend on Exporter.
8 # You have to do it this way or VMS will get confused.
9 printf "%sok %d%s\n", ($ok ? '' : 'not '), $test,
10 (defined $name ? " - $name" : '');
12 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
23 ok( 1, 'Exporter compiled' );
28 # Methods which Exporter says it implements.
29 @Exporter_Methods = qw(import
41 # Make sure Testing can do everything its supposed to.
42 foreach my $meth (@::Exporter_Methods) {
43 ::ok( Testing->can($meth), "subclass can $meth()" );
47 This => [qw(stuff %left)],
48 That => [qw(Above the @wailing)],
49 tray => [qw(Fasten $seatbelt)],
51 @EXPORT = qw(lifejacket is);
52 @EXPORT_OK = qw(under &your $seat);
55 ::ok( Testing->require_version(1.05), 'require_version()' );
56 eval { Testing->require_version(1.11); 1 };
57 ::ok( $@, 'require_version() fail' );
58 ::ok( Testing->require_version(0), 'require_version(0)' );
60 sub lifejacket { 'lifejacket' }
64 sub Fasten { 'Fasten' }
67 use vars qw($seatbelt $seat @wailing %left);
68 $seatbelt = 'seatbelt';
70 @wailing = qw(AHHHHHH);
71 %left = ( left => "right" );
76 Exporter::export_ok_tags();
78 my %tags = map { $_ => 1 } map { @$_ } values %EXPORT_TAGS;
79 my %exportok = map { $_ => 1 } @EXPORT_OK;
81 foreach my $tag (keys %tags) {
82 $ok = exists $exportok{$tag};
84 ::ok( $ok, 'export_ok_tags()' );
90 ::ok( defined &lifejacket, 'simple import' );
92 my $got = eval {&lifejacket};
93 ::ok ( $@ eq "", 'check we can call the imported subroutine')
94 or print STDERR "# \$\@ is $@\n";
95 ::ok ( $got eq 'lifejacket', 'and that it gave the correct result')
96 or print STDERR "# expected 'lifejacket', got " .
97 (defined $got ? "'$got'" : "undef") . "\n";
99 # The string eval is important. It stops $Foo::{is} existing when
100 # Testing->import is called.
101 ::ok( eval "defined &is",
102 "Import a subroutine where exporter must create the typeglob" );
104 ::ok ( $@ eq "", 'check we can call the imported autoloaded subroutine')
105 or chomp ($@), print STDERR "# \$\@ is $@\n";
106 ::ok ( $got eq 'Is', 'and that it gave the correct result')
107 or print STDERR "# expected 'Is', got " .
108 (defined $got ? "'$got'" : "undef") . "\n";
112 my @imports = qw($seatbelt &Above stuff @wailing %left);
113 Testing->import(@imports);
115 ::ok( (! grep { my ($s, $n) = @$_; eval "\\$s$n != \\${s}Testing::$n" }
116 map { /^(\W)(\w+)/ ? [$1, $2] : ['&', $_] }
118 'import by symbols' );
122 my @tags = qw(:This :tray);
123 Testing->import(@tags);
125 ::ok( (! grep { my ($s, $n) = @$_; eval "\\$s$n != \\${s}Testing::$n" }
126 map { /^(\W)(\w+)/ ? [$1, $2] : ['&', $_] }
128 @{$Testing::EXPORT_TAGS{@tags}}),
133 Testing->import(qw(!lifejacket));
135 ::ok( !defined &lifejacket, 'deny import by !' );
139 Testing->import('/e/');
141 ::ok( (! grep { my ($s, $n) = @$_; eval "\\$s$n != \\${s}Testing::$n" }
142 map { /^(\W)(\w+)/ ? [$1, $2] : ['&', $_] }
144 @Testing::EXPORT, @Testing::EXPORT_OK),
149 Testing->import('!/e/');
151 ::ok( (! grep { my ($s, $n) = @$_; eval "\\$s$n == \\${s}Testing::$n" }
152 map { /^(\W)(\w+)/ ? [$1, $2] : ['&', $_] }
154 @Testing::EXPORT, @Testing::EXPORT_OK),
155 'deny import by regex');
157 ::ok( !defined &lifejacket, 'further denial' );
160 package More::Testing;
163 eval { More::Testing->require_version(0); 1 };
164 ::ok(!$@, 'require_version(0) and $VERSION = 0');
167 package Yet::More::Testing;
170 eval { Yet::More::Testing->require_version(10); 1 };
171 ::ok($@ !~ /\(undef\)/, 'require_version(10) and $VERSION = 0');
176 local $SIG{__WARN__} = sub { $warnings = join '', @_ };
177 package Testing::Unused::Vars;
179 @EXPORT = qw(this $TODO that);
182 Testing::Unused::Vars->import;
185 ::ok( !$warnings, 'Unused variables can be exported without warning' ) ||
186 print "# $warnings\n";
188 package Moving::Target;
190 @EXPORT_OK = qw (foo);
192 sub foo {"This is foo"};
193 sub bar {"This is bar"};
195 package Moving::Target::Test;
197 Moving::Target->import ('foo');
199 ::ok (foo() eq "This is foo", "imported foo before EXPORT_OK changed");
201 push @Moving::Target::EXPORT_OK, 'bar';
203 Moving::Target->import ('bar');
205 ::ok (bar() eq "This is bar", "imported bar after EXPORT_OK changed");
209 use Exporter 'import';
211 ::ok(\&import == \&Exporter::import, "imported the import routine");
213 @EXPORT = qw( wibble );
214 sub wibble {return "wobble"};
216 package Use::The::Import;
220 my $val = eval { wibble() };
221 ::ok($val eq "wobble", "exported importer worked");
223 # Check that Carp recognizes Exporter as internal to Perl
225 eval { Carp::croak() };
226 ::ok($Carp::Internal{Exporter}, "Carp recognizes Exporter");
227 ::ok($Carp::Internal{'Exporter::Heavy'}, "Carp recognizes Exporter::Heavy");
229 package Exporter::for::Tied::_;
236 sub TIESCALAR{bless[]}
240 tie my $t, __PACKAGE__;
241 for($t) { # $_ is now tied
242 import Exporter::for::Tied::_;
245 ::ok(1, 'import with tied $_');