File: aliases-func.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 (111 lines) | stat: -rw-r--r-- 3,565 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/perl
#
# Test setting color aliases via the function interface.
#
# Copyright 2012, 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 => 30;

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

# Confirm our test alias doesn't exist.
my $output = eval { color('alert') };
ok(!$output, 'alert color not recognized');
like(
    $@,
    qr{ \A Invalid [ ] attribute [ ] name [ ] alert [ ] at [ ] }xms,
    '...with the right error'
);

# Basic alias functionality.
is(coloralias('alert', 'red'), 'red', 'coloralias works and returns color');
is(color('alert'),           color('red'),      'alert now works as a color');
is(colored('test', 'alert'), "\e[31mtest\e[0m", '..and colored works');
ok(colorvalid('alert'), '...and alert is now a valid color');
is(coloralias('alert'), 'red', 'coloralias with one arg returns value');

# The alias can be changed.
is(coloralias('alert', 'green'), 'green', 'changing the alias works');
is(coloralias('alert'),          'green', '...and changed the mapping');
is(color('alert'), color('green'), '...and now returns its new value');

# Aliasing to an alias expands the underlying alias.
is(coloralias('warning', 'alert'), 'green', 'aliasing to an alias works');
is(color('warning'), color('green'), '...and returns the right value');

# An alias can map to multiple attributes.
is(
    coloralias('multiple', 'blue on_green', 'bold'),
    'blue on_green bold',
    'aliasing to multiple attributes works'
);
is(color('multiple'), color('blue on_green bold'), '...and works with color');
is(colored('foo', 'multiple'), "\e[34;42;1mfoo\e[0m", '...and colored works');
ok(colorvalid('multiple'), '...and colorvalid works');

# Those can include other aliases.
is(
    coloralias('multiple', 'on_blue alert blink'),
    'on_blue green blink',
    'aliasing to multiple attributes including aliases'
);
is(color('multiple'), color('on_blue green blink'), '...and works with color');

# color supports aliases among multiple attributes.
is(
    color('bold warning'),
    color('bold', 'green'),
    'color supports aliases with multiple attributes'
);

# uncolor ignores aliases.
is_deeply([uncolor("\e[32m")], ['green'], 'uncolor ignores aliases');

# Asking for the value of an unknown alias returns undef.
is(coloralias('foo'), undef, 'coloralias on unknown alias returns undef');

# Invalid alias names.
$output = eval { coloralias('foo;bar', 'green') };
ok(!$output, 'invalid alias name rejected');
like(
    $@,
    qr{ \A Invalid [ ] alias [ ] name [ ] "foo;bar" [ ] at [ ] }xms,
    '...with the right error'
);
$output = eval { coloralias(q{}, 'green') };
ok(!$output, 'empty alias name rejected');
like(
    $@,
    qr{ \A Invalid [ ] alias [ ] name [ ] "" [ ] at [ ] }xms,
    '...with the right error'
);

# Aliasing an existing color.
$output = eval { coloralias('red', 'green') };
ok(!$output, 'aliasing an existing color rejected');
like(
    $@,
    qr{ \A Cannot [ ] alias [ ] standard [ ] color [ ] "red" [ ] at [ ] }xms,
    '...with the right error'
);

# Aliasing to a color that doesn't exist.
$output = eval { coloralias('warning', 'chartreuse') };
ok(!$output, 'aliasing to an unknown color rejected');
like(
    $@,
    qr{ \A Invalid [ ] attribute [ ] name [ ] "chartreuse" [ ] at [ ] }xms,
    '...with the right error'
);