File: stringify.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (55 lines) | stat: -rw-r--r-- 1,657 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl
#
# Test suite for stringify interaction.
#
# Copyright 2011 Revilo Reegiles
# Copyright 2011, 2014, 2020 Russ Allbery <rra@cpan.org>
#
# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl

use 5.008;
use strict;
use warnings;

use Test::More tests => 6;

# Create a dummy class that implements stringification.
## no critic (Modules::ProhibitMultiplePackages)
package Test::Stringify;
use overload '""' => 'stringify';
sub new       { return bless({}, 'Test::Stringify') }
sub stringify { return "Foo Bar\n" }

# Back to the main package.
package main;

# Load the module.
BEGIN {
    delete $ENV{ANSI_COLORS_ALIASES};
    delete $ENV{ANSI_COLORS_DISABLED};
    delete $ENV{NO_COLOR};
    use_ok('Term::ANSIColor', qw(colored));
}

# Some basic tests of colored without stringification.
my $result = colored(['blue', 'bold'], 'testing');
is($result, "\e[34;1mtesting\e[0m", 'colored with an array reference');
$result = colored("ok\n", 'bold blue');
is($result, "\e[1;34mok\n\e[0m", 'colored with a following string');

# Create a stringifiable object and repeat the tests.
my $test = Test::Stringify->new;
$result = colored($test . q{}, 'bold blue');
is($result, "\e[1;34mFoo Bar\n\e[0m", 'colored with forced stringification');
$result = colored($test, 'bold blue');
is($result, "\e[1;34mFoo Bar\n\e[0m", 'colored with a non-array reference');

# Create a hash reference and try stringifying it.
## no critic (RegularExpressions::ProhibitEscapedMetacharacters)
my %foo = (foo => 'bar');
$result = colored(\%foo, 'bold blue');
like(
    $result,
    qr{ \e\[1;34m HASH\(.*\) \e\[0m }xms,
    'colored with a hash reference'
);