stringify.t revision 1.2
1#!/usr/bin/perl
2#
3# Test suite for stringify interaction.
4#
5# Copyright 2011 Revilo Reegiles
6# Copyright 2011, 2014, 2020 Russ Allbery <rra@cpan.org>
7#
8# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
9
10use 5.008;
11use strict;
12use warnings;
13
14use Test::More tests => 6;
15
16# Create a dummy class that implements stringification.
17## no critic (Modules::ProhibitMultiplePackages)
18package Test::Stringify;
19use overload '""' => 'stringify';
20sub new       { return bless({}, 'Test::Stringify') }
21sub stringify { return "Foo Bar\n" }
22
23# Back to the main package.
24package main;
25
26# Load the module.
27BEGIN {
28    delete $ENV{ANSI_COLORS_ALIASES};
29    delete $ENV{ANSI_COLORS_DISABLED};
30    delete $ENV{NO_COLOR};
31    use_ok('Term::ANSIColor', qw(colored));
32}
33
34# Some basic tests of colored without stringification.
35my $result = colored(['blue', 'bold'], 'testing');
36is($result, "\e[34;1mtesting\e[0m", 'colored with an array reference');
37$result = colored("ok\n", 'bold blue');
38is($result, "\e[1;34mok\n\e[0m", 'colored with a following string');
39
40# Create a stringifiable object and repeat the tests.
41my $test = Test::Stringify->new;
42$result = colored($test . q{}, 'bold blue');
43is($result, "\e[1;34mFoo Bar\n\e[0m", 'colored with forced stringification');
44$result = colored($test, 'bold blue');
45is($result, "\e[1;34mFoo Bar\n\e[0m", 'colored with a non-array reference');
46
47# Create a hash reference and try stringifying it.
48## no critic (RegularExpressions::ProhibitEscapedMetacharacters)
49my %foo = (foo => 'bar');
50$result = colored(\%foo, 'bold blue');
51like(
52    $result,
53    qr{ \e\[1;34m HASH\(.*\) \e\[0m }xms,
54    'colored with a hash reference'
55);
56