File: Util.t

package info (click to toggle)
perl 5.28.1-6%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 115,032 kB
  • sloc: perl: 599,825; ansic: 593,575; sh: 68,835; pascal: 8,034; cpp: 4,103; makefile: 2,506; xml: 2,410; yacc: 1,120; sed: 6; lisp: 1
file content (88 lines) | stat: -rw-r--r-- 2,012 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use strict;
use warnings;

use Config qw/%Config/;

use Test2::Tools::Tiny;
use Test2::Util qw/
    try

    get_tid USE_THREADS

    pkg_to_file

    CAN_FORK
    CAN_THREAD
    CAN_REALLY_FORK

    CAN_SIGSYS

    IS_WIN32

    clone_io
/;

BEGIN {
    if ($] lt "5.008") {
        require Test::Builder::IO::Scalar;
    }
}

{
    for my $try (\&try, Test2::Util->can('_manual_try'), Test2::Util->can('_local_try')) {
        my ($ok, $err) = $try->(sub { die "xxx" });
        ok(!$ok, "cought exception");
        like($err, qr/xxx/, "expected exception");

        ($ok, $err) = $try->(sub { 0 });
        ok($ok,   "Success");
        ok(!$err, "no error");
    }
}

is(pkg_to_file('A::Package::Name'), 'A/Package/Name.pm', "Converted package to file");

# Make sure running them does not die
# We cannot really do much to test these.
CAN_THREAD();
CAN_FORK();
CAN_REALLY_FORK();
IS_WIN32();

is(IS_WIN32(), ($^O eq 'MSWin32') ? 1 : 0, "IS_WIN32 is correct ($^O)");

my %sigs = map {$_ => 1} split /\s+/, $Config{sig_name};
if ($sigs{SYS}) {
    ok(CAN_SIGSYS, "System has SIGSYS");
}
else {
    ok(!CAN_SIGSYS, "System lacks SIGSYS");
}

my $check_for_sig_sys = Test2::Util->can('_check_for_sig_sys');
ok($check_for_sig_sys->("FOO SYS BAR"), "Found SIGSYS in the middle");
ok($check_for_sig_sys->("SYS FOO BAR"), "Found SIGSYS at start");
ok($check_for_sig_sys->("FOO BAR SYS"), "Found SIGSYS at end");
ok(!$check_for_sig_sys->("FOO SYSX BAR"), "SYSX is not SYS");
ok(!$check_for_sig_sys->("FOO XSYS BAR"), "XSYS is not SYS");

my $io = clone_io(\*STDOUT);
ok($io, "Cloned the filehandle");
close($io);

my $fh;
my $out = '';
if ($] ge "5.008") {
    open($fh, '>', \$out) or die "Could not open filehandle";
} else {
    $fh = Test::Builder::IO::Scalar->new(\$out) or die "Could not open filehandle";
}

$io = clone_io($fh);
is($io, $fh, "For a scalar handle we simply return the original handle, no other choice");
print $io "Test\n";

is($out, "Test\n", "wrote to the scalar handle");


done_testing;