File: NonFatal.pm

package info (click to toggle)
libnet-dns-perl 1.29-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,608 kB
  • sloc: perl: 19,379; makefile: 9
file content (76 lines) | stat: -rw-r--r-- 1,878 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
# $Id: NonFatal.pm 1823 2020-11-16 16:29:45Z willem $	-*-perl-*-

# Test::More calls functions from Test::Builder. Those functions all eventually
# call Test::Builder::ok (on a builder instance) for reporting the status.
# Here we define a new builder inherited from Test::Builder, with a redefined
# ok method that always reports the test to have completed successfully.
#
# The functions NonFatalBegin and NonFatalEnd re-bless the builder in use by
# Test::More (Test::More->builder) to be of type NonFatal and Test::Builder
# respectively. Tests that are between those functions will thus always appear
# to succeed. The failure report itself is not suppressed.
#
# Note that the builder is only re-blessed when the file 't/online.nonfatal'
# exists.
#
# This is just a quick hack to allow for non-fatal unit tests. It has many
# problems such as for example that blocks marked by the NonFatalBegin and
# NonFatalEnd subroutines may not be nested.
#

package NonFatal;

use strict;
use warnings;
use base qw(Test::Builder);

my @failed;

sub ok {
	my ( $self, $test, @name ) = @_;

	return $self->SUPER::ok( 1, @name ) if $test;

	$self->SUPER::ok( 1, "NOT OK (tolerating failure)  @name" );

	push @failed, join( "\t", $self->current_test, @name );
	return $test;
}


sub diag {
	my @annotation = @_;
	return Test::More->builder->diag(@annotation);
}


END {
	my $n = scalar(@failed);
	my $s = $n > 1 ? 's' : '';
	bless Test::More->builder, qw(Test::Builder);
	diag( join "\n\t", "\tDisregarding $n failed sub-test$s", @failed ) if $n;
	return;
}


package main;				## no critic ProhibitMultiplePackages

require Test::More;

use constant NONFATAL => eval { -e 't/online.nonfatal' };

sub NonFatalBegin {
	bless Test::More->builder, qw(NonFatal) if NONFATAL;
	return;
}

sub NonFatalEnd {
	bless Test::More->builder, qw(Test::Builder) if NONFATAL;
	return;
}


1;

__END__