|
Packit |
e6c7a3 |
#!/usr/bin/perl
|
|
Packit |
e6c7a3 |
#
|
|
Packit |
e6c7a3 |
# Test setting color aliases via the function interface.
|
|
Packit |
e6c7a3 |
#
|
|
Packit |
e6c7a3 |
# Copyright 2012 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 => 23;
|
|
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(color colored colorvalid uncolor coloralias));
|
|
Packit |
e6c7a3 |
}
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Confirm our test alias doesn't exist.
|
|
Packit |
e6c7a3 |
my $output = eval { color('alert') };
|
|
Packit |
e6c7a3 |
ok(!$output, 'alert color not recognized');
|
|
Packit |
e6c7a3 |
like(
|
|
Packit |
e6c7a3 |
$@,
|
|
Packit |
e6c7a3 |
qr{ \A Invalid [ ] attribute [ ] name [ ] alert [ ] at [ ] }xms,
|
|
Packit |
e6c7a3 |
'...with the right error'
|
|
Packit |
e6c7a3 |
);
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Basic alias functionality.
|
|
Packit |
e6c7a3 |
is(coloralias('alert', 'red'), 'red', 'coloralias works and returns color');
|
|
Packit |
e6c7a3 |
is(color('alert'), color('red'), 'alert now works as a color');
|
|
Packit |
e6c7a3 |
is(colored('test', 'alert'), "\e[31mtest\e[0m", '..and colored works');
|
|
Packit |
e6c7a3 |
ok(colorvalid('alert'), '...and alert is now a valid color');
|
|
Packit |
e6c7a3 |
is(coloralias('alert'), 'red', 'coloralias with one arg returns value');
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# The alias can be changed.
|
|
Packit |
e6c7a3 |
is(coloralias('alert', 'green'), 'green', 'changing the alias works');
|
|
Packit |
e6c7a3 |
is(coloralias('alert'), 'green', '...and changed the mapping');
|
|
Packit |
e6c7a3 |
is(color('alert'), color('green'), '...and now returns its new value');
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# uncolor ignores aliases.
|
|
Packit |
e6c7a3 |
is_deeply([uncolor("\e[32m")], ['green'], 'uncolor ignores aliases');
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Asking for the value of an unknown alias returns undef.
|
|
Packit |
e6c7a3 |
is(coloralias('warning'), undef, 'coloralias on unknown alias returns undef');
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Invalid alias names.
|
|
Packit |
e6c7a3 |
$output = eval { coloralias('foo;bar', 'green') };
|
|
Packit |
e6c7a3 |
ok(!$output, 'invalid alias name rejected');
|
|
Packit |
e6c7a3 |
like(
|
|
Packit |
e6c7a3 |
$@,
|
|
Packit |
e6c7a3 |
qr{ \A Invalid [ ] alias [ ] name [ ] "foo;bar" [ ] at [ ] }xms,
|
|
Packit |
e6c7a3 |
'...with the right error'
|
|
Packit |
e6c7a3 |
);
|
|
Packit |
e6c7a3 |
$output = eval { coloralias(q{}, 'green') };
|
|
Packit |
e6c7a3 |
ok(!$output, 'empty alias name rejected');
|
|
Packit |
e6c7a3 |
like(
|
|
Packit |
e6c7a3 |
$@,
|
|
Packit |
e6c7a3 |
qr{ \A Invalid [ ] alias [ ] name [ ] "" [ ] at [ ] }xms,
|
|
Packit |
e6c7a3 |
'...with the right error'
|
|
Packit |
e6c7a3 |
);
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Aliasing an existing color.
|
|
Packit |
e6c7a3 |
$output = eval { coloralias('red', 'green') };
|
|
Packit |
e6c7a3 |
ok(!$output, 'aliasing an existing color rejected');
|
|
Packit |
e6c7a3 |
like(
|
|
Packit |
e6c7a3 |
$@,
|
|
Packit |
e6c7a3 |
qr{ \A Cannot [ ] alias [ ] standard [ ] color [ ] "red" [ ] at [ ] }xms,
|
|
Packit |
e6c7a3 |
'...with the right error'
|
|
Packit |
e6c7a3 |
);
|
|
Packit |
e6c7a3 |
|
|
Packit |
e6c7a3 |
# Aliasing to a color that doesn't exist, or to another alias.
|
|
Packit |
e6c7a3 |
$output = eval { coloralias('warning', 'chartreuse') };
|
|
Packit |
e6c7a3 |
ok(!$output, 'aliasing to an unknown color rejected');
|
|
Packit |
e6c7a3 |
like(
|
|
Packit |
e6c7a3 |
$@,
|
|
Packit |
e6c7a3 |
qr{ \A Invalid [ ] attribute [ ] name [ ] "chartreuse" [ ] at [ ] }xms,
|
|
Packit |
e6c7a3 |
'...with the right error'
|
|
Packit |
e6c7a3 |
);
|
|
Packit |
e6c7a3 |
$output = eval { coloralias('warning', 'alert') };
|
|
Packit |
e6c7a3 |
ok(!$output, 'aliasing to an alias rejected');
|
|
Packit |
e6c7a3 |
like(
|
|
Packit |
e6c7a3 |
$@,
|
|
Packit |
e6c7a3 |
qr{ \A Invalid [ ] attribute [ ] name [ ] "alert" [ ] at [ ] }xms,
|
|
Packit |
e6c7a3 |
'...with the right error'
|
|
Packit |
e6c7a3 |
);
|