File: 05-uncolor.t

package info (click to toggle)
libterm-extendedcolor-perl 0.504-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 1,036; makefile: 2
file content (75 lines) | stat: -rwxr-xr-x 1,740 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 4;
use Term::ExtendedColor qw(uncolor get_colors);

my(@colors, @colors2, @attributes);

my $j = 0;
for(my $i = 0; $i< 17; ++$i) {
  if($j % 2 == 0) {
    push(@colors, "\e[38;5;$i" . 'm' . 'This is in color' . "\e[0m");
  }
  else {
    push(@colors, "\033[38;5;$i" . 'm' . 'This is in color' . "\033[0m");
  }
  $j++;
}

$j = 0;
for(my $i = 0; $i<9; ++$i) {
  if($j % 2 == 0) {
    push(@attributes, "\e[$i" . 'm' . 'This is with attributes added' . "\e[0m");
  }
  else {
    push(@attributes, "\033[$i" . 'm' . 'This is with attributes added' . "\033[0m");
  }
}

@colors2    = @colors;
@colors     = uncolor(@colors);
@attributes = uncolor(@attributes);

my $colors = get_colors();

#push(@colors, fg('red2', 'foobar'));

my $fail = 0;
for(@colors) {
  if($_ =~ /\e[38;[\d]+/) {
    $fail++;
  }
}
for(@attributes) {
  if($_ =~ /\e[48;[\d]+/) {
    $fail++;
  }
}

$fail
  ? fail("uncolor: color esc sequences not parsed correctly ($fail)")
  : pass("uncolor: color esc sequences parsed correctly")
;

use Data::Dumper;
$Data::Dumper::Terse     = 1;
$Data::Dumper::Indent    = 1;
$Data::Dumper::Useqq     = 1;
$Data::Dumper::Deparse   = 1;
$Data::Dumper::Quotekeys = 0;
$Data::Dumper::Sortkeys  = 1;

my $non_color = Dumper uncolor(
  "\e[2cNot \e[38;5;100;1mlegal\e[0m color esc sequences\e [0m"
);

$non_color =~ s/^"(.+)"\n$/$1/;
is($non_color,
  '\e[2cNot legal color esc sequences\e [0m',
  'uncolor does not remove non-color stuff'
);

is_deeply( [ uncolor(\@colors2) ], \@colors,     "uncolor: passing arrayref as first argument - list context");

is       ( uncolor(\@colors2), join('',@colors), "uncolor: passing arrayref as first argument - scalar context");