eval.t revision 1.2
1#!/usr/bin/perl
2#
3# Test suite for $@ preservation with constants.
4#
5# Earlier versions of Term::ANSIColor would clobber $@ during AUTOLOAD
6# processing and lose its value or leak $@ values to the calling program.
7# This is a regression test to ensure that this problem doesn't return.
8#
9# Copyright 2012-2014, 2020 Russ Allbery <rra@cpan.org>
10#
11# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
12
13use 5.008;
14use strict;
15use warnings;
16
17use Test::More tests => 17;
18
19# We refer to $@ in the test descriptions.
20## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
21
22# Load the module.
23BEGIN {
24    delete $ENV{ANSI_COLORS_ALIASES};
25    delete $ENV{ANSI_COLORS_DISABLED};
26    delete $ENV{NO_COLOR};
27    use_ok('Term::ANSIColor', qw(:constants));
28}
29
30# Ensure that using a constant doesn't leak anything in $@.
31is((BOLD 'test'), "\e[1mtest", 'BOLD works');
32is($@,            q{},         '... and $@ is empty');
33
34# Store something in $@ and ensure it doesn't get clobbered.
35## no critic (BuiltinFunctions::ProhibitStringyEval)
36eval 'sub { syntax';
37is((BLINK 'test'), "\e[5mtest", 'BLINK works after eval failure');
38isnt($@, q{}, '... and $@ still contains something useful');
39
40# Do some additional unnecessary testing so that coverage analysis works
41# properly.  First, check disabled colors.
42local $ENV{ANSI_COLORS_DISABLED} = 1;
43is(BOLD,  q{}, 'ANSI_COLORS_DISABLED works for BOLD');
44is(BLINK, q{}, '...and for BLINK');
45delete $ENV{ANSI_COLORS_DISABLED};
46
47# Now, NO_COLOR.
48local $ENV{NO_COLOR} = 'foo';
49is(BOLD,  q{}, 'NO_COLOR works for BOLD');
50is(BLINK, q{}, '...and for BLINK');
51delete $ENV{NO_COLOR};
52
53# Now, AUTORESET.
54$Term::ANSIColor::AUTORESET = 1;
55is((BOLD 't'),  "\e[1mt\e[0m", 'AUTORESET works for BOLD');
56is((BLINK 't'), "\e[5mt\e[0m", '...and for BLINK');
57is((BOLD),      "\e[1m",       'AUTORESET without text for BOLD');
58is((BLINK),     "\e[5m",       '...and for BLINK');
59$Term::ANSIColor::AUTORESET = 0;
60
61# And, finally, AUTOLOCAL.
62$Term::ANSIColor::AUTOLOCAL = 1;
63is((BOLD 't'),  "\e[1mt\e[0m", 'AUTOLOCAL works for BOLD');
64is((BLINK 't'), "\e[5mt\e[0m", '...and for BLINK');
65is((BOLD),      "\e[1m",       'AUTOLOCAL without text for BOLD');
66is((BLINK),     "\e[5m",       '...and for BLINK');
67$Term::ANSIColor::AUTOLOCAL = 0;
68