|
Packit |
e6c7a3 |
#!/usr/bin/perl
|
|
Packit |
e6c7a3 |
#
|
|
Packit |
e6c7a3 |
# Test suite for stringify interaction.
|
|
Packit |
e6c7a3 |
#
|
|
Packit |
e6c7a3 |
# Copyright 2011 Revilo Reegiles
|
|
Packit |
e6c7a3 |
# Copyright 2011, 2014 Russ Allbery <rra@cpan.org>
|
|
Packit |
e6c7a3 |
#
|
|
Packit |
e6c7a3 |
# This program is free software; you may redistribute it and/or modify it
|
|
Packit |
e6c7a3 |
# under the same terms as Perl itself.
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
use strict;
|
|
Packit |
e6c7a3 |
use warnings;
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
use Test::More tests => 6;
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Create a dummy class that implements stringification.
|
|
Packit |
e6c7a3 |
## no critic (Modules::ProhibitMultiplePackages)
|
|
Packit |
e6c7a3 |
package Test::Stringify;
|
|
Packit |
e6c7a3 |
use overload '""' => 'stringify';
|
|
Packit |
e6c7a3 |
sub new { return bless({}, 'Test::Stringify') }
|
|
Packit |
e6c7a3 |
sub stringify { return "Foo Bar\n" }
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Back to the main package.
|
|
Packit |
e6c7a3 |
package main;
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Load the module.
|
|
Packit |
e6c7a3 |
BEGIN {
|
|
Packit |
e6c7a3 |
delete $ENV{ANSI_COLORS_ALIASES};
|
|
Packit |
e6c7a3 |
delete $ENV{ANSI_COLORS_DISABLED};
|
|
Packit |
e6c7a3 |
use_ok('Term::ANSIColor', qw(colored));
|
|
Packit |
e6c7a3 |
}
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Some basic tests of colored without stringification.
|
|
Packit |
e6c7a3 |
my $result = colored(['blue', 'bold'], 'testing');
|
|
Packit |
e6c7a3 |
is($result, "\e[34;1mtesting\e[0m", 'colored with an array reference');
|
|
Packit |
e6c7a3 |
$result = colored("ok\n", 'bold blue');
|
|
Packit |
e6c7a3 |
is($result, "\e[1;34mok\n\e[0m", 'colored with a following string');
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Create a stringifiable object and repeat the tests.
|
|
Packit |
e6c7a3 |
my $test = Test::Stringify->new;
|
|
Packit |
e6c7a3 |
$result = colored($test . q{}, 'bold blue');
|
|
Packit |
e6c7a3 |
is($result, "\e[1;34mFoo Bar\n\e[0m", 'colored with forced stringification');
|
|
Packit |
e6c7a3 |
$result = colored($test, 'bold blue');
|
|
Packit |
e6c7a3 |
is($result, "\e[1;34mFoo Bar\n\e[0m", 'colored with a non-array reference');
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Create a hash reference and try stringifying it.
|
|
Packit |
e6c7a3 |
## no critic (RegularExpressions::ProhibitEscapedMetacharacters)
|
|
Packit |
e6c7a3 |
my %foo = (foo => 'bar');
|
|
Packit |
e6c7a3 |
$result = colored(\%foo, 'bold blue');
|
|
Packit |
e6c7a3 |
like(
|
|
Packit |
e6c7a3 |
$result,
|
|
Packit |
e6c7a3 |
qr{ \e\[1;34m HASH\(.*\) \e\[0m }xms,
|
|
Packit |
e6c7a3 |
'colored with a hash reference'
|
|
Packit |
e6c7a3 |
);
|