Update to Encode 2.32
[p5sagit/p5-mst-13.2.git] / ext / DynaLoader / t / DynaLoader.t
CommitLineData
51254d33 1#!/usr/bin/perl -wT
2
3BEGIN {
4 if( $ENV{PERL_CORE} ) {
5 chdir 't';
6 @INC = '../lib';
7 }
8}
9
10use strict;
11use Config;
12use Test::More;
13my %modules;
14
494364e0 15my $db_file;
16BEGIN {
17 use Config;
18 foreach (qw/SDBM_File GDBM_File ODBM_File NDBM_File DB_File/) {
19 if ($Config{extensions} =~ /\b$_\b/) {
20 $db_file = $_;
21 last;
22 }
23 }
24}
25
51254d33 26%modules = (
27 # ModuleName => q| code to check that it was loaded |,
05a4b9b1 28 'List::Util' => q| ::is( ref List::Util->can('first'), 'CODE' ) |, # 5.7.2
51254d33 29 'Cwd' => q| ::is( ref Cwd->can('fastcwd'),'CODE' ) |, # 5.7 ?
30 'File::Glob' => q| ::is( ref File::Glob->can('doglob'),'CODE' ) |, # 5.6
494364e0 31 $db_file => q| ::is( ref $db_file->can('TIEHASH'), 'CODE' ) |, # 5.0
51254d33 32 'Socket' => q| ::is( ref Socket->can('inet_aton'),'CODE' ) |, # 5.0
33 'Time::HiRes'=> q| ::is( ref Time::HiRes->can('usleep'),'CODE' ) |, # 5.7.3
34);
35
05a4b9b1 36plan tests => 22 + keys(%modules) * 3;
51254d33 37
38
39# Try to load the module
40use_ok( 'DynaLoader' );
41
42
43# Check functions
44can_ok( 'DynaLoader' => 'bootstrap' ); # defined in Perl section
51254d33 45can_ok( 'DynaLoader' => 'dl_load_flags' ); # defined in Perl section
389a661a 46can_ok( 'DynaLoader' => 'dl_error' ); # defined in XS section
47if ($Config{usedl}) {
48 can_ok( 'DynaLoader' => 'dl_find_symbol' ); # defined in XS section
49 can_ok( 'DynaLoader' => 'dl_install_xsub' ); # defined in XS section
50 can_ok( 'DynaLoader' => 'dl_load_file' ); # defined in XS section
51 can_ok( 'DynaLoader' => 'dl_undef_symbols' ); # defined in XS section
52 SKIP: {
53 skip "unloading unsupported on $^O", 1 if ($^O eq 'VMS' || $^O eq 'darwin');
54 can_ok( 'DynaLoader' => 'dl_unload_file' ); # defined in XS section
55 }
56} else {
57 foreach my $symbol (qw(dl_find_symbol dl_install_sub dl_load_file
58 dl_undef_symbols dl_unload_file)) {
59 is(DynaLoader->can($symbol), undef,
60 "Without dynamic loading, DynaLoader should not have $symbol");
61 }
a555bf5f 62}
51254d33 63
64TODO: {
65local $TODO = "Test::More::can_ok() seems to have trouble dealing with AutoLoaded functions";
66can_ok( 'DynaLoader' => 'dl_expandspec' ); # defined in AutoLoaded section
67can_ok( 'DynaLoader' => 'dl_findfile' ); # defined in AutoLoaded section
68can_ok( 'DynaLoader' => 'dl_find_symbol_anywhere' ); # defined in AutoLoaded section
69}
70
71
72# Check error messages
73# .. for bootstrap()
74eval { DynaLoader::bootstrap() };
75like( $@, q{/^Usage: DynaLoader::bootstrap\(module\)/},
76 "calling DynaLoader::bootstrap() with no argument" );
77
78eval { package egg_bacon_sausage_and_spam; DynaLoader::bootstrap("egg_bacon_sausage_and_spam") };
389a661a 79if ($Config{usedl}) {
80 like( $@, q{/^Can't locate loadable object for module egg_bacon_sausage_and_spam/},
51254d33 81 "calling DynaLoader::bootstrap() with a package without binary object" );
389a661a 82} else {
83 like( $@, q{/^Can't load module egg_bacon_sausage_and_spam/},
84 "calling DynaLoader::bootstrap() with a package without binary object" );
85}
51254d33 86
87# .. for dl_load_file()
389a661a 88SKIP: {
89 skip "no dl_load_file with dl_none.xs", 2 unless $Config{usedl};
90 eval { DynaLoader::dl_load_file() };
91 like( $@, q{/^Usage: DynaLoader::dl_load_file\(filename, flags=0\)/},
92 "calling DynaLoader::dl_load_file() with no argument" );
51254d33 93
389a661a 94 eval { no warnings 'uninitialized'; DynaLoader::dl_load_file(undef) };
95 is( $@, '', "calling DynaLoader::dl_load_file() with undefined argument" ); # is this expected ?
96}
51254d33 97
f3c90b36 98my ($dlhandle, $dlerr);
99eval { $dlhandle = DynaLoader::dl_load_file("egg_bacon_sausage_and_spam") };
c3026122 100$dlerr = DynaLoader::dl_error();
a555bf5f 101SKIP: {
102 skip "dl_load_file() does not attempt to load file on VMS (and thus does not fail) when \@dl_require_symbols is empty", 1 if $^O eq 'VMS';
103 ok( !$dlhandle, "calling DynaLoader::dl_load_file() without an existing library should fail" );
104}
f3c90b36 105ok( defined $dlerr, "dl_error() returning an error message: '$dlerr'" );
51254d33 106
f3c90b36 107# Checking for any particular error messages or numeric codes
108# is very unportable, please do not try to do that. A failing
109# dl_load_file() is not even guaranteed to set the $! or the $^E.
51254d33 110
c3026122 111# ... dl_findfile()
112SKIP: {
113 my @files = ();
114 eval { @files = DynaLoader::dl_findfile("c") };
115 is( $@, '', "calling dl_findfile()" );
f3c90b36 116 # Some platforms are known to not have a "libc"
117 # (not at least by that name) that the dl_findfile()
118 # could find.
119 skip "dl_findfile test not appropriate on $^O", 1
d53b420d 120 if $^O =~ /(win32|vms|openbsd|cygwin)/i;
f3c90b36 121 # Play safe and only try this test if this system
122 # looks pretty much Unix-like.
123 skip "dl_findfile test not appropriate on $^O", 1
124 unless -d '/usr' && -f '/bin/ls';
125 cmp_ok( scalar @files, '>=', 1, "array should contain one result result or more: libc => (@files)" );
c3026122 126}
51254d33 127
128# Now try to load well known XS modules
3fa583e0 129my $extensions = $Config{'dynamic_ext'};
51254d33 130$extensions =~ s|/|::|g;
131
132for my $module (sort keys %modules) {
133 SKIP: {
0aad8255 134 if ($extensions !~ /\b$module\b/) {
135 delete($modules{$module});
136 skip "$module not available", 3;
137 }
51254d33 138 eval "use $module";
139 is( $@, '', "loading $module" );
140 }
141}
142
143# checking internal consistency
144is( scalar @DynaLoader::dl_librefs, scalar keys %modules, "checking number of items in \@dl_librefs" );
145is( scalar @DynaLoader::dl_modules, scalar keys %modules, "checking number of items in \@dl_modules" );
146
147my @loaded_modules = @DynaLoader::dl_modules;
148for my $libref (reverse @DynaLoader::dl_librefs) {
a555bf5f 149 SKIP: {
ce12ed19 150 skip "unloading unsupported on $^O", 2 if ($^O eq 'VMS' || $^O eq 'darwin');
51254d33 151 my $module = pop @loaded_modules;
152 my $r = eval { DynaLoader::dl_unload_file($libref) };
153 is( $@, '', "calling dl_unload_file() for $module" );
154 is( $r, 1, " - unload was successful" );
a555bf5f 155 }
51254d33 156}
157