File: color.t.cpp

package info (click to toggle)
taskd 1.1.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,576 kB
  • ctags: 1,141
  • sloc: cpp: 13,971; python: 1,523; sh: 1,080; perl: 610; ansic: 48; makefile: 21
file content (192 lines) | stat: -rw-r--r-- 9,586 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2010 - 2015, Göteborg Bit Factory.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#include <cmake.h>
#include <iostream>
#include <stdio.h>
#include <Color.h>
#include <test.h>

////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
  UnitTest t (40 + 256 + 256 + 6*6*6 + 6*6*6 + 1 + 24 + 24 + 3);

  // Names matched to values.
  t.is ((int) Color (""),        (int) Color (Color::nocolor), "''        == Color::nocolor");
  t.is ((int) Color ("black"),   (int) Color (Color::black),   "'black'   == Color::black");
  t.is ((int) Color ("red"),     (int) Color (Color::red),     "'red'     == Color::red");
  t.is ((int) Color ("green"),   (int) Color (Color::green),   "'green'   == Color::green");
  t.is ((int) Color ("yellow"),  (int) Color (Color::yellow),  "'yellow'  == Color::yellow");
  t.is ((int) Color ("blue"),    (int) Color (Color::blue),    "'blue'    == Color::blue");
  t.is ((int) Color ("magenta"), (int) Color (Color::magenta), "'magenta' == Color::magenta");
  t.is ((int) Color ("cyan"),    (int) Color (Color::cyan),    "'cyan'    == Color::cyan");
  t.is ((int) Color ("white"),   (int) Color (Color::white),   "'white'   == Color::white");

  // Auto upgrades.
  Color c ("red on color0");
  t.is ((std::string) c, "color1 on color0", "upgrade red on color0 -> color1 on color0");

  c = Color ("color1 on black");
  t.is ((std::string) c, "color1 on color0", "upgrade color1 on black -> color1 on color0");

  c = Color ("bold red on color0");
  t.is ((std::string) c, "color9 on color0", "upgrade bold red on color0 -> color9 on color0");

  c = Color ("color1 on bright black");
  t.is ((std::string) c, "color1 on color8", "upgrade color1 on bright black -> color1 on color8");

  // Simple blending.
  c = Color ("red");
  c.blend (Color ("on white"));
  t.is ((std::string) c, "red on white", "red + on white -> red on white");

  c = Color ("bold underline red");
  c.blend (Color ("on bright white"));
  t.is ((std::string) c, "bold underline red on bright white", "bold underline red + on bright white -> bold underline red on bright white");

  // Blending with conflicts.
  c = Color ("red on white");
  c.blend (Color ("on blue"));
  t.is ((std::string) c, "red on blue", "red on white + on blue -> red on blue");

  c = Color ("red on white");
  c.blend (Color ("blue on magenta"));
  t.is ((std::string) c, "blue on magenta", "red on white + blue on magenta -> blue on magenta");

  // Blending with upgrades.
  c = Color ("color1 on color0");
  c.blend (Color ("blue"));
  t.is ((std::string) c, "color4 on color0", "color1 on color0 + blue -> color4 on color0");

  // Now the dumb show of every color and its code.
  t.is (Color::colorize ("foo", "red"),                std::string ("\033[31mfoo\033[0m"),       "red                -> ^[[31m");
  t.is (Color::colorize ("foo", "bold red"),           std::string ("\033[1;31mfoo\033[0m"),     "bold red           -> ^[[1;31m");
  t.is (Color::colorize ("foo", "underline red"),      std::string ("\033[4;31mfoo\033[0m"),     "underline red      -> ^[[4;31m");
  t.is (Color::colorize ("foo", "underline bold red"), std::string ("\033[1;4;31mfoo\033[0m"),   "underline bold red -> ^[[1;4;31m");

  // 16-color foregrounds.
  t.is (Color::colorize ("foo", ""),                   std::string ("foo"),                      "''                 -> ''");

  t.is (Color::colorize ("foo", "black"),              std::string ("\033[30mfoo\033[0m"),       "black              -> ^[[30m");
  t.is (Color::colorize ("foo", "red"),                std::string ("\033[31mfoo\033[0m"),       "red                -> ^[[31m");
  t.is (Color::colorize ("foo", "green"),              std::string ("\033[32mfoo\033[0m"),       "green              -> ^[[32m");
  t.is (Color::colorize ("foo", "yellow"),             std::string ("\033[33mfoo\033[0m"),       "yellow             -> ^[[33m");
  t.is (Color::colorize ("foo", "blue"),               std::string ("\033[34mfoo\033[0m"),       "blue               -> ^[[34m");
  t.is (Color::colorize ("foo", "magenta"),            std::string ("\033[35mfoo\033[0m"),       "magenta            -> ^[[35m");
  t.is (Color::colorize ("foo", "cyan"),               std::string ("\033[36mfoo\033[0m"),       "cyan               -> ^[[36m");
  t.is (Color::colorize ("foo", "white"),              std::string ("\033[37mfoo\033[0m"),       "white              -> ^[[37m");

  // 16-color backgrounds.
  t.is (Color::colorize ("foo", "on bright black"),    std::string ("\033[100mfoo\033[0m"),      "on bright black    -> ^[[100m");

  t.is (Color::colorize ("foo", "on black"),           std::string ("\033[40mfoo\033[0m"),       "on black           -> ^[[40m");
  t.is (Color::colorize ("foo", "on red"),             std::string ("\033[41mfoo\033[0m"),       "on red             -> ^[[41m");
  t.is (Color::colorize ("foo", "on green"),           std::string ("\033[42mfoo\033[0m"),       "on green           -> ^[[42m");
  t.is (Color::colorize ("foo", "on yellow"),          std::string ("\033[43mfoo\033[0m"),       "on yellow          -> ^[[43m");
  t.is (Color::colorize ("foo", "on blue"),            std::string ("\033[44mfoo\033[0m"),       "on blue            -> ^[[44m");
  t.is (Color::colorize ("foo", "on magenta"),         std::string ("\033[45mfoo\033[0m"),       "on magenta         -> ^[[45m");
  t.is (Color::colorize ("foo", "on cyan"),            std::string ("\033[46mfoo\033[0m"),       "on cyan            -> ^[[46m");
  t.is (Color::colorize ("foo", "on white"),           std::string ("\033[47mfoo\033[0m"),       "on white           -> ^[[47m");

  // 256-color, basic colors.
  char color [24];
  char codes [64];
  char description [64];
  for (int i = 0; i < 256; ++i)
  {
    sprintf (color,       "color%d", i);
    sprintf (codes,       "\033[38;5;%dmfoo\033[0m", i);
    sprintf (description, "color%d -> ^[[38;5;%dm", i, i);

    t.is (Color::colorize ("foo", color), std::string (codes), description);
  }

  for (int i = 0; i < 256; ++i)
  {
    sprintf (color,       "on color%d", i);
    sprintf (codes,       "\033[48;5;%dmfoo\033[0m", i);
    sprintf (description, "on color%d -> ^[[48;5;%dm", i, i);

    t.is (Color::colorize ("foo", color), std::string (codes), description);
  }

  // RGB Color Cube.
  for (int r = 0; r < 6; ++r)
    for (int g = 0; g < 6; ++g)
      for (int b = 0; b < 6; ++b)
      {
        int code = 16 + (r*36 + g*6 + b);
        sprintf (color,       "rgb%d%d%d", r, g, b);
        sprintf (codes,       "\033[38;5;%dmfoo\033[0m", code);
        sprintf (description, "rgb%d%d%d -> ^[[38;5;%dm", r, g, b, code);

        t.is (Color::colorize ("foo", color), std::string (codes), description);
      }

  for (int r = 0; r < 6; ++r)
    for (int g = 0; g < 6; ++g)
      for (int b = 0; b < 6; ++b)
      {
        int code = 16 + (r*36 + g*6 + b);
        sprintf (color,       "on rgb%d%d%d", r, g, b);
        sprintf (codes,       "\033[48;5;%dmfoo\033[0m", code);
        sprintf (description, "on rgb%d%d%d -> ^[[48;5;%dm", r, g, b, code);

        t.is (Color::colorize ("foo", color), std::string (codes), description);
      }

  // 256-color, grays.
  // grey == gray.
  t.is (Color::colorize ("foo", "grey0"), std::string ("\033[38;5;232mfoo\033[0m"), "grey0 -> ^[[38;5;232m");

  for (int i = 0; i < 24; ++i)
  {
    sprintf (color,       "gray%d", i);
    sprintf (codes,       "\033[38;5;%dmfoo\033[0m", i + 232);
    sprintf (description, "gray%d -> ^[[38;5;%dm", i + 232, i + 232);

    t.is (Color::colorize ("foo", color), std::string (codes), description);
  }

  for (int i = 0; i < 24; ++i)
  {
    sprintf (color,       "on gray%d", i);
    sprintf (codes,       "\033[48;5;%dmfoo\033[0m", i + 232);
    sprintf (description, "on gray%d -> ^[[48;5;%dm", i + 232, i + 232);

    t.is (Color::colorize ("foo", color), std::string (codes), description);
  }

  // std::string Color::strip (const std::string&);
  t.is (Color::strip (""),                  "",    "Color::strip '' -> ''");
  t.is (Color::strip ("foo"),               "foo", "Color::strip 'foo' -> 'foo'");
  t.is (Color::strip ("f\033[1mo\033[0mo"), "foo", "Color::strip 'f<b>o</b>o' -> 'foo'");

  return 0;
}

////////////////////////////////////////////////////////////////////////////////