1#!/usr/bin/perl
2#
3# Test setting color aliases via the function interface.
4#
5# Copyright 2012, 2020 Russ Allbery <rra@cpan.org>
6#
7# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
8
9use 5.008;
10use strict;
11use warnings;
12
13use Test::More tests => 30;
14
15# Load the module.
16BEGIN {
17    delete $ENV{ANSI_COLORS_ALIASES};
18    delete $ENV{ANSI_COLORS_DISABLED};
19    delete $ENV{NO_COLOR};
20    use_ok('Term::ANSIColor', qw(color colored colorvalid uncolor coloralias));
21}
22
23# Confirm our test alias doesn't exist.
24my $output = eval { color('alert') };
25ok(!$output, 'alert color not recognized');
26like(
27    $@,
28    qr{ \A Invalid [ ] attribute [ ] name [ ] alert [ ] at [ ] }xms,
29    '...with the right error'
30);
31
32# Basic alias functionality.
33is(coloralias('alert', 'red'), 'red', 'coloralias works and returns color');
34is(color('alert'),           color('red'),      'alert now works as a color');
35is(colored('test', 'alert'), "\e[31mtest\e[0m", '..and colored works');
36ok(colorvalid('alert'), '...and alert is now a valid color');
37is(coloralias('alert'), 'red', 'coloralias with one arg returns value');
38
39# The alias can be changed.
40is(coloralias('alert', 'green'), 'green', 'changing the alias works');
41is(coloralias('alert'),          'green', '...and changed the mapping');
42is(color('alert'), color('green'), '...and now returns its new value');
43
44# Aliasing to an alias expands the underlying alias.
45is(coloralias('warning', 'alert'), 'green', 'aliasing to an alias works');
46is(color('warning'), color('green'), '...and returns the right value');
47
48# An alias can map to multiple attributes.
49is(
50    coloralias('multiple', 'blue on_green', 'bold'),
51    'blue on_green bold',
52    'aliasing to multiple attributes works'
53);
54is(color('multiple'), color('blue on_green bold'), '...and works with color');
55is(colored('foo', 'multiple'), "\e[34;42;1mfoo\e[0m", '...and colored works');
56ok(colorvalid('multiple'), '...and colorvalid works');
57
58# Those can include other aliases.
59is(
60    coloralias('multiple', 'on_blue alert blink'),
61    'on_blue green blink',
62    'aliasing to multiple attributes including aliases'
63);
64is(color('multiple'), color('on_blue green blink'), '...and works with color');
65
66# color supports aliases among multiple attributes.
67is(
68    color('bold warning'),
69    color('bold', 'green'),
70    'color supports aliases with multiple attributes'
71);
72
73# uncolor ignores aliases.
74is_deeply([uncolor("\e[32m")], ['green'], 'uncolor ignores aliases');
75
76# Asking for the value of an unknown alias returns undef.
77is(coloralias('foo'), undef, 'coloralias on unknown alias returns undef');
78
79# Invalid alias names.
80$output = eval { coloralias('foo;bar', 'green') };
81ok(!$output, 'invalid alias name rejected');
82like(
83    $@,
84    qr{ \A Invalid [ ] alias [ ] name [ ] "foo;bar" [ ] at [ ] }xms,
85    '...with the right error'
86);
87$output = eval { coloralias(q{}, 'green') };
88ok(!$output, 'empty alias name rejected');
89like(
90    $@,
91    qr{ \A Invalid [ ] alias [ ] name [ ] "" [ ] at [ ] }xms,
92    '...with the right error'
93);
94
95# Aliasing an existing color.
96$output = eval { coloralias('red', 'green') };
97ok(!$output, 'aliasing an existing color rejected');
98like(
99    $@,
100    qr{ \A Cannot [ ] alias [ ] standard [ ] color [ ] "red" [ ] at [ ] }xms,
101    '...with the right error'
102);
103
104# Aliasing to a color that doesn't exist.
105$output = eval { coloralias('warning', 'chartreuse') };
106ok(!$output, 'aliasing to an unknown color rejected');
107like(
108    $@,
109    qr{ \A Invalid [ ] attribute [ ] name [ ] "chartreuse" [ ] at [ ] }xms,
110    '...with the right error'
111);
112