xref: /openbsd-src/gnu/usr.bin/perl/cpan/Scalar-List-Utils/t/openhan.t (revision b8851fcc53cbe24fd20b090f26dd149e353f6174)
1#!./perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 21;
7use Scalar::Util qw(openhandle);
8
9ok(defined &openhandle, 'defined');
10
11{
12    my $fh = \*STDERR;
13    is(openhandle($fh), $fh, 'STDERR');
14
15    is(fileno(openhandle(*STDERR)), fileno(STDERR), 'fileno(STDERR)');
16}
17
18{
19    use vars qw(*CLOSED);
20    is(openhandle(*CLOSED), undef, 'closed');
21}
22
23SKIP: {
24    skip "3-arg open only on 5.6 or later", 1 if $]<5.006;
25
26    open my $fh, "<", $0;
27    skip "could not open $0 for reading: $!", 2 unless $fh;
28    is(openhandle($fh), $fh, "works with indirect filehandles");
29    close($fh);
30    is(openhandle($fh), undef, "works with indirect filehandles");
31}
32
33SKIP: {
34    skip "in-memory files only on 5.8 or later", 2 if $]<5.008;
35
36    open my $fh, "<", \"in-memory file";
37    skip "could not open in-memory file: $!", 2 unless $fh;
38    is(openhandle($fh), $fh, "works with in-memory files");
39    close($fh);
40    is(openhandle($fh), undef, "works with in-memory files");
41}
42
43ok(openhandle(\*DATA), "works for \*DATA");
44ok(openhandle(*DATA), "works for *DATA");
45ok(openhandle(*DATA{IO}), "works for *DATA{IO}");
46
47{
48    require IO::Handle;
49    my $fh = IO::Handle->new_from_fd(fileno(*STDERR), 'w');
50    skip "new_from_fd(fileno(*STDERR)) failed", 2 unless $fh;
51    ok(openhandle($fh), "works for IO::Handle objects");
52
53    ok(!openhandle(IO::Handle->new), "unopened IO::Handle");
54}
55
56{
57    require IO::File;
58    my $fh = IO::File->new;
59    $fh->open("< $0")
60        or skip "could not open $0: $!", 3;
61    ok(openhandle($fh), "works for IO::File objects");
62    close($fh);
63    ok(!openhandle($fh), "works for IO::File objects");
64
65    ok(!openhandle(IO::File->new), "unopened IO::File" );
66}
67
68SKIP: {
69    skip( "Tied handles only on 5.8 or later", 2) if $]<5.008;
70
71    use vars qw(*H);
72
73    package My::Tie;
74    require Tie::Handle;
75    @My::Tie::ISA = qw(Tie::Handle);
76    sub TIEHANDLE { bless {} }
77
78    package main;
79    tie *H, 'My::Tie';
80    ok(openhandle(*H), "tied handles are always ok");
81    ok(openhandle(\*H), "tied handle refs are always ok");
82}
83
84ok !openhandle(undef),   "undef is not a filehandle";
85ok !openhandle("STDIN"), "strings are not filehandles";
86ok !openhandle(0),       "integers are not filehandles";
87
88
89__DATA__
90