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
|
package colorstring
import (
"os"
"testing"
)
func TestColor(t *testing.T) {
cases := []struct {
Input, Output string
}{
{
Input: "foo",
Output: "foo",
},
{
Input: "[blue]foo",
Output: "\033[34mfoo\033[0m",
},
{
Input: "foo[blue]foo",
Output: "foo\033[34mfoo\033[0m",
},
{
Input: "foo[what]foo",
Output: "foo[what]foo",
},
{
Input: "foo[_blue_]foo",
Output: "foo\033[44mfoo\033[0m",
},
{
Input: "foo[bold]foo",
Output: "foo\033[1mfoo\033[0m",
},
{
Input: "[blue]foo[bold]bar",
Output: "\033[34mfoo\033[1mbar\033[0m",
},
{
Input: "[underline]foo[reset]bar",
Output: "\033[4mfoo\033[0mbar\033[0m",
},
}
for _, tc := range cases {
actual := Color(tc.Input)
if actual != tc.Output {
t.Errorf(
"Input: %#v\n\nOutput: %#v\n\nExpected: %#v",
tc.Input,
actual,
tc.Output)
}
}
}
func TestColorPrefix(t *testing.T) {
cases := []struct {
Input, Output string
}{
{
Input: "foo",
Output: "",
},
{
Input: "[blue]foo",
Output: "[blue]",
},
{
Input: "[bold][blue]foo",
Output: "[bold][blue]",
},
{
Input: " [bold][blue]foo",
Output: "[bold][blue]",
},
}
for _, tc := range cases {
actual := ColorPrefix(tc.Input)
if actual != tc.Output {
t.Errorf(
"Input: %#v\n\nOutput: %#v\n\nExpected: %#v",
tc.Input,
actual,
tc.Output)
}
}
}
func TestColorizeColor_disable(t *testing.T) {
c := def
c.Disable = true
cases := []struct {
Input, Output string
}{
{
"[blue]foo",
"foo",
},
{
"[foo]bar",
"[foo]bar",
},
}
for _, tc := range cases {
actual := c.Color(tc.Input)
if actual != tc.Output {
t.Errorf(
"Input: %#v\n\nOutput: %#v\n\nExpected: %#v",
tc.Input,
actual,
tc.Output)
}
}
}
func TestColorizeColor_noReset(t *testing.T) {
c := def
c.Reset = false
input := "[blue]foo"
output := "\033[34mfoo"
actual := c.Color(input)
if actual != output {
t.Errorf(
"Input: %#v\n\nOutput: %#v\n\nExpected: %#v",
input,
actual,
output)
}
}
func TestConvenienceWrappers(t *testing.T) {
var length int
printInput := "[bold]Print:\t\t[default][red]R[green]G[blue]B[cyan]C[magenta]M[yellow]Y\n"
printlnInput := "[bold]Println:\t[default][red]R[green]G[blue]B[cyan]C[magenta]M[yellow]Y"
printfInput := "[bold]Printf:\t\t[default][red]R[green]G[blue]B[cyan]C[magenta]M[yellow]Y\n"
fprintInput := "[bold]Fprint:\t\t[default][red]R[green]G[blue]B[cyan]C[magenta]M[yellow]Y\n"
fprintlnInput := "[bold]Fprintln:\t[default][red]R[green]G[blue]B[cyan]C[magenta]M[yellow]Y"
fprintfInput := "[bold]Fprintf:\t[default][red]R[green]G[blue]B[cyan]C[magenta]M[yellow]Y\n"
// colorstring.Print
length, _ = Print(printInput)
assertOutputLength(t, printInput, 58, length)
// colorstring.Println
length, _ = Println(printlnInput)
assertOutputLength(t, printlnInput, 59, length)
// colorstring.Printf
length, _ = Printf(printfInput)
assertOutputLength(t, printfInput, 59, length)
// colorstring.Fprint
length, _ = Fprint(os.Stdout, fprintInput)
assertOutputLength(t, fprintInput, 59, length)
// colorstring.Fprintln
length, _ = Fprintln(os.Stdout, fprintlnInput)
assertOutputLength(t, fprintlnInput, 60, length)
// colorstring.Fprintf
length, _ = Fprintf(os.Stdout, fprintfInput)
assertOutputLength(t, fprintfInput, 59, length)
}
func assertOutputLength(t *testing.T, input string, expectedLength int, actualLength int) {
if actualLength != expectedLength {
t.Errorf("Input: %#v\n\n Output length: %d\n\n Expected: %d",
input,
actualLength,
expectedLength)
}
}
|