I don't think trying to bracket the hires time with lores
[p5sagit/p5-mst-13.2.git] / lib / sigtrap.t
CommitLineData
d1b75174 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use strict;
9use Config;
10
3a33ea64 11use Test::More tests => 15;
d1b75174 12
13use_ok( 'sigtrap' );
14
15package main;
16local %SIG;
17
18# use a version of sigtrap.pm somewhat too high
19eval{ sigtrap->import(99999) };
20like( $@, qr/version 99999 required,/, 'import excessive version number' );
21
22# use an invalid signal name
23eval{ sigtrap->import('abadsignal') };
24like( $@, qr/^Unrecognized argument abadsignal/, 'send bad signame to import' );
25
26eval{ sigtrap->import('handler') };
27like( $@, qr/^No argument specified/, 'send handler without subref' );
28
29sigtrap->import('AFAKE');
30is( $SIG{AFAKE}, \&sigtrap::handler_traceback, 'install normal handler' );
31
32sigtrap->import('die', 'AFAKE', 'stack-trace', 'FAKE2');
33is( $SIG{AFAKE}, \&sigtrap::handler_die, 'install the die handler' );
34is( $SIG{FAKE2}, \&sigtrap::handler_traceback, 'install traceback handler' );
35
36my @normal = qw( HUP INT PIPE TERM );
37@SIG{@normal} = 1 x @normal;
38sigtrap->import('normal-signals');
39is( (grep { ref $_ } @SIG{@normal}), @normal, 'check normal-signals set' );
40
41my @error = qw( ABRT BUS EMT FPE ILL QUIT SEGV SYS TRAP );
42@SIG{@error} = 1 x @error;
43sigtrap->import('error-signals');
44is( (grep { ref $_ } @SIG{@error}), @error, 'check error-signals set' );
45
46my @old = qw( ABRT BUS EMT FPE ILL PIPE QUIT SEGV SYS TERM TRAP );
47@SIG{@old} = 1 x @old;
48sigtrap->import('old-interface-signals');
49is( (grep { ref $_ } @SIG{@old}), @old, 'check old-interface-signals set' );
50
51my $handler = sub {};
52sigtrap->import(handler => $handler, 'FAKE3');
53is( $SIG{FAKE3}, $handler, 'install custom handler' );
54
55$SIG{FAKE} = 'IGNORE';
56sigtrap->import('untrapped', 'FAKE');
57is( $SIG{FAKE}, 'IGNORE', 'respect existing handler set to IGNORE' );
58
59my $out = tie *STDOUT, 'TieOut';
60$SIG{FAKE} = 'DEFAULT';
61$sigtrap::Verbose = 1;
62sigtrap->import('any', 'FAKE');
63is( $SIG{FAKE}, \&sigtrap::handler_traceback, 'should set default handler' );
64like( $out->read, qr/^Installing handler/, 'does it talk with $Verbose set?' );
65
66# handler_die croaks with first argument
67eval { sigtrap::handler_die('FAKE') };
68like( $@, qr/^Caught a SIGFAKE/, 'does handler_die() croak?' );
69
d1b75174 70package TieOut;
71
72sub TIEHANDLE {
73 bless(\(my $scalar), $_[0]);
74}
75
76sub PRINT {
77 my $self = shift;
78 $$self .= join '', @_;
79}
80
81sub WRITE {
82 my ($self, $msg, $length) = @_;
83 $$self .= $msg;
84}
85
86sub read {
87 my $self = shift;
88 substr($$self, 0, length($$self), '');
89}