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