3 # Test suite for stringify interaction.
5 # Copyright 2011 Revilo Reegiles
6 # Copyright 2011, 2014 Russ Allbery <rra@cpan.org>
8 # This program is free software; you may redistribute it and/or modify it
9 # under the same terms as Perl itself.
14 use Test::More tests => 6;
16 # Create a dummy class that implements stringification.
17 ## no critic (Modules::ProhibitMultiplePackages)
18 package Test::Stringify;
19 use overload '""' => 'stringify';
20 sub new { return bless({}, 'Test::Stringify') }
21 sub stringify { return "Foo Bar\n" }
23 # Back to the main package.
28 delete $ENV{ANSI_COLORS_ALIASES};
29 delete $ENV{ANSI_COLORS_DISABLED};
30 use_ok('Term::ANSIColor', qw(colored));
33 # Some basic tests of colored without stringification.
34 my $result = colored(['blue', 'bold'], 'testing');
35 is($result, "\e[34;1mtesting\e[0m", 'colored with an array reference');
36 $result = colored("ok\n", 'bold blue');
37 is($result, "\e[1;34mok\n\e[0m", 'colored with a following string');
39 # Create a stringifiable object and repeat the tests.
40 my $test = Test::Stringify->new;
41 $result = colored($test . q{}, 'bold blue');
42 is($result, "\e[1;34mFoo Bar\n\e[0m", 'colored with forced stringification');
43 $result = colored($test, 'bold blue');
44 is($result, "\e[1;34mFoo Bar\n\e[0m", 'colored with a non-array reference');
46 # Create a hash reference and try stringifying it.
47 ## no critic (RegularExpressions::ProhibitEscapedMetacharacters)
48 my %foo = (foo => 'bar');
49 $result = colored(\%foo, 'bold blue');
52 qr{ \e\[1;34m HASH\(.*\) \e\[0m }xms,
53 'colored with a hash reference'