This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Better handling for error-checking
[perl5.git] / ext / DynaLoader / t / DynaLoader.t
CommitLineData
51254d33
AT
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;
41b994da 12use Errno qw(ENOENT);
51254d33
AT
13use Test::More;
14my %modules;
15
16%modules = (
17 # ModuleName => q| code to check that it was loaded |,
18 'Cwd' => q| ::is( ref Cwd->can('fastcwd'),'CODE' ) |, # 5.7 ?
19 'File::Glob' => q| ::is( ref File::Glob->can('doglob'),'CODE' ) |, # 5.6
20 'SDBM_File' => q| ::is( ref SDBM_File->can('TIEHASH'), 'CODE' ) |, # 5.0
21 'Socket' => q| ::is( ref Socket->can('inet_aton'),'CODE' ) |, # 5.0
22 'Time::HiRes'=> q| ::is( ref Time::HiRes->can('usleep'),'CODE' ) |, # 5.7.3
23);
24
41b994da 25plan tests => 28 + keys(%modules) * 2;
51254d33
AT
26
27
28# Try to load the module
29use_ok( 'DynaLoader' );
30
31
32# Check functions
33can_ok( 'DynaLoader' => 'bootstrap' ); # defined in Perl section
34can_ok( 'DynaLoader' => 'dl_error' ); # defined in XS section
35can_ok( 'DynaLoader' => 'dl_find_symbol' ); # defined in XS section
36can_ok( 'DynaLoader' => 'dl_install_xsub' ); # defined in XS section
37can_ok( 'DynaLoader' => 'dl_load_file' ); # defined in XS section
38can_ok( 'DynaLoader' => 'dl_load_flags' ); # defined in Perl section
39can_ok( 'DynaLoader' => 'dl_undef_symbols' ); # defined in XS section
40can_ok( 'DynaLoader' => 'dl_unload_file' ); # defined in XS section
41
42TODO: {
43local $TODO = "Test::More::can_ok() seems to have trouble dealing with AutoLoaded functions";
44can_ok( 'DynaLoader' => 'dl_expandspec' ); # defined in AutoLoaded section
45can_ok( 'DynaLoader' => 'dl_findfile' ); # defined in AutoLoaded section
46can_ok( 'DynaLoader' => 'dl_find_symbol_anywhere' ); # defined in AutoLoaded section
47}
48
49
50# Check error messages
51# .. for bootstrap()
52eval { DynaLoader::bootstrap() };
53like( $@, q{/^Usage: DynaLoader::bootstrap\(module\)/},
54 "calling DynaLoader::bootstrap() with no argument" );
55
56eval { package egg_bacon_sausage_and_spam; DynaLoader::bootstrap("egg_bacon_sausage_and_spam") };
57like( $@, q{/^Can't locate loadable object for module egg_bacon_sausage_and_spam/},
58 "calling DynaLoader::bootstrap() with a package without binary object" );
59
60# .. for dl_load_file()
61eval { DynaLoader::dl_load_file() };
62like( $@, q{/^Usage: DynaLoader::dl_load_file\(filename, flags=0\)/},
63 "calling DynaLoader::dl_load_file() with no argument" );
64
69c6e315 65eval { no warnings 'uninitialized'; DynaLoader::dl_load_file(undef) };
51254d33
AT
66is( $@, '', "calling DynaLoader::dl_load_file() with undefined argument" ); # is this expected ?
67
68eval { DynaLoader::dl_load_file("egg_bacon_sausage_and_spam") };
41b994da
AT
69is( $@, '', "calling DynaLoader::dl_load_file() with a package without binary object" );
70is( 0+$!, ENOENT, "checking errno value" );
71like( DynaLoader::dl_error(), "/$!/", "checking error message returned by dl_error()" );
51254d33
AT
72
73# ... dl_findfile()
74my @files = ();
75eval { @files = DynaLoader::dl_findfile("c") };
76is( $@, '', "calling dl_findfile()" );
77cmp_ok( scalar @files, '>=', 1, " - array should contain one result result or more: libc => (@files)" );
78
79
80# Now try to load well known XS modules
81my $extensions = $Config{'extensions'};
82$extensions =~ s|/|::|g;
83
84for my $module (sort keys %modules) {
85 SKIP: {
86 skip "$module not available", 2 if $extensions !~ /\b$module\b/;
87 eval "use $module";
88 is( $@, '', "loading $module" );
89 }
90}
91
92# checking internal consistency
93is( scalar @DynaLoader::dl_librefs, scalar keys %modules, "checking number of items in \@dl_librefs" );
94is( scalar @DynaLoader::dl_modules, scalar keys %modules, "checking number of items in \@dl_modules" );
95
96my @loaded_modules = @DynaLoader::dl_modules;
97for my $libref (reverse @DynaLoader::dl_librefs) {
98 my $module = pop @loaded_modules;
99 my $r = eval { DynaLoader::dl_unload_file($libref) };
100 is( $@, '', "calling dl_unload_file() for $module" );
101 is( $r, 1, " - unload was successful" );
102}
103