2014-02-17 12:12:32 +03:00
|
|
|
package color
|
|
|
|
|
|
|
|
import (
|
2014-02-18 10:25:12 +03:00
|
|
|
"bytes"
|
2014-02-17 12:12:32 +03:00
|
|
|
"fmt"
|
2023-03-12 14:54:28 +02:00
|
|
|
"io"
|
2017-01-03 17:56:11 +02:00
|
|
|
"os"
|
2014-02-17 12:12:32 +03:00
|
|
|
"testing"
|
2015-03-12 14:00:06 +02:00
|
|
|
|
2016-02-12 11:13:08 +02:00
|
|
|
"github.com/mattn/go-colorable"
|
2014-02-17 12:12:32 +03:00
|
|
|
)
|
|
|
|
|
2014-02-18 11:20:24 +03:00
|
|
|
// Testing colors is kinda different. First we test for given colors and their
|
|
|
|
// escaped formatted results. Next we create some visual tests to be tested.
|
|
|
|
// Each visual test includes the color name to be compared.
|
2014-02-17 12:12:32 +03:00
|
|
|
func TestColor(t *testing.T) {
|
2014-02-18 10:25:12 +03:00
|
|
|
rb := new(bytes.Buffer)
|
|
|
|
Output = rb
|
|
|
|
|
2015-08-20 20:45:44 +02:00
|
|
|
NoColor = false
|
|
|
|
|
2014-02-18 10:25:12 +03:00
|
|
|
testColors := []struct {
|
|
|
|
text string
|
2014-02-18 12:06:54 +03:00
|
|
|
code Attribute
|
2014-02-18 10:25:12 +03:00
|
|
|
}{
|
|
|
|
{text: "black", code: FgBlack},
|
|
|
|
{text: "red", code: FgRed},
|
|
|
|
{text: "green", code: FgGreen},
|
|
|
|
{text: "yellow", code: FgYellow},
|
|
|
|
{text: "blue", code: FgBlue},
|
|
|
|
{text: "magent", code: FgMagenta},
|
|
|
|
{text: "cyan", code: FgCyan},
|
|
|
|
{text: "white", code: FgWhite},
|
2015-10-26 10:54:07 +02:00
|
|
|
{text: "hblack", code: FgHiBlack},
|
|
|
|
{text: "hred", code: FgHiRed},
|
|
|
|
{text: "hgreen", code: FgHiGreen},
|
|
|
|
{text: "hyellow", code: FgHiYellow},
|
|
|
|
{text: "hblue", code: FgHiBlue},
|
|
|
|
{text: "hmagent", code: FgHiMagenta},
|
|
|
|
{text: "hcyan", code: FgHiCyan},
|
|
|
|
{text: "hwhite", code: FgHiWhite},
|
2014-02-18 10:25:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range testColors {
|
|
|
|
New(c.code).Print(c.text)
|
|
|
|
|
|
|
|
line, _ := rb.ReadString('\n')
|
|
|
|
scannedLine := fmt.Sprintf("%q", line)
|
|
|
|
colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.code, c.text)
|
|
|
|
escapedForm := fmt.Sprintf("%q", colored)
|
|
|
|
|
|
|
|
fmt.Printf("%s\t: %s\n", c.text, line)
|
|
|
|
|
|
|
|
if scannedLine != escapedForm {
|
|
|
|
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 00:48:14 +02:00
|
|
|
|
|
|
|
for _, c := range testColors {
|
|
|
|
line := New(c.code).Sprintf("%s", c.text)
|
|
|
|
scannedLine := fmt.Sprintf("%q", line)
|
|
|
|
colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.code, c.text)
|
|
|
|
escapedForm := fmt.Sprintf("%q", colored)
|
|
|
|
|
|
|
|
fmt.Printf("%s\t: %s\n", c.text, line)
|
|
|
|
|
|
|
|
if scannedLine != escapedForm {
|
|
|
|
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 10:13:04 +02:00
|
|
|
}
|
|
|
|
|
2015-08-23 23:24:46 +02:00
|
|
|
func TestColorEquals(t *testing.T) {
|
|
|
|
fgblack1 := New(FgBlack)
|
|
|
|
fgblack2 := New(FgBlack)
|
|
|
|
bgblack := New(BgBlack)
|
|
|
|
fgbgblack := New(FgBlack, BgBlack)
|
|
|
|
fgblackbgred := New(FgBlack, BgRed)
|
|
|
|
fgred := New(FgRed)
|
|
|
|
bgred := New(BgRed)
|
|
|
|
|
|
|
|
if !fgblack1.Equals(fgblack2) {
|
|
|
|
t.Error("Two black colors are not equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fgblack1.Equals(bgblack) {
|
|
|
|
t.Error("Fg and bg black colors are equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fgblack1.Equals(fgbgblack) {
|
|
|
|
t.Error("Fg black equals fg/bg black color")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fgblack1.Equals(fgred) {
|
|
|
|
t.Error("Fg black equals Fg red")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fgblack1.Equals(bgred) {
|
|
|
|
t.Error("Fg black equals Bg red")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fgblack1.Equals(fgblackbgred) {
|
|
|
|
t.Error("Fg black equals fg black bg red")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 10:13:04 +02:00
|
|
|
func TestNoColor(t *testing.T) {
|
|
|
|
rb := new(bytes.Buffer)
|
|
|
|
Output = rb
|
|
|
|
|
|
|
|
testColors := []struct {
|
|
|
|
text string
|
|
|
|
code Attribute
|
|
|
|
}{
|
|
|
|
{text: "black", code: FgBlack},
|
|
|
|
{text: "red", code: FgRed},
|
|
|
|
{text: "green", code: FgGreen},
|
|
|
|
{text: "yellow", code: FgYellow},
|
|
|
|
{text: "blue", code: FgBlue},
|
|
|
|
{text: "magent", code: FgMagenta},
|
|
|
|
{text: "cyan", code: FgCyan},
|
|
|
|
{text: "white", code: FgWhite},
|
2015-10-26 10:54:07 +02:00
|
|
|
{text: "hblack", code: FgHiBlack},
|
|
|
|
{text: "hred", code: FgHiRed},
|
|
|
|
{text: "hgreen", code: FgHiGreen},
|
|
|
|
{text: "hyellow", code: FgHiYellow},
|
|
|
|
{text: "hblue", code: FgHiBlue},
|
|
|
|
{text: "hmagent", code: FgHiMagenta},
|
|
|
|
{text: "hcyan", code: FgHiCyan},
|
|
|
|
{text: "hwhite", code: FgHiWhite},
|
2015-04-22 10:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range testColors {
|
|
|
|
p := New(c.code)
|
|
|
|
p.DisableColor()
|
|
|
|
p.Print(c.text)
|
|
|
|
|
|
|
|
line, _ := rb.ReadString('\n')
|
2015-04-22 10:19:51 +02:00
|
|
|
if line != c.text {
|
|
|
|
t.Errorf("Expecting %s, got '%s'\n", c.text, line)
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 10:13:04 +02:00
|
|
|
|
2015-04-22 10:19:51 +02:00
|
|
|
// global check
|
|
|
|
NoColor = true
|
2021-05-13 21:29:58 +02:00
|
|
|
t.Cleanup(func() {
|
2015-05-04 20:28:38 +02:00
|
|
|
NoColor = false
|
2021-05-13 21:29:58 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
for _, c := range testColors {
|
|
|
|
p := New(c.code)
|
|
|
|
p.Print(c.text)
|
|
|
|
|
|
|
|
line, _ := rb.ReadString('\n')
|
|
|
|
if line != c.text {
|
|
|
|
t.Errorf("Expecting %s, got '%s'\n", c.text, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoColor_Env(t *testing.T) {
|
|
|
|
rb := new(bytes.Buffer)
|
|
|
|
Output = rb
|
|
|
|
|
|
|
|
testColors := []struct {
|
|
|
|
text string
|
|
|
|
code Attribute
|
|
|
|
}{
|
|
|
|
{text: "black", code: FgBlack},
|
|
|
|
{text: "red", code: FgRed},
|
|
|
|
{text: "green", code: FgGreen},
|
|
|
|
{text: "yellow", code: FgYellow},
|
|
|
|
{text: "blue", code: FgBlue},
|
|
|
|
{text: "magent", code: FgMagenta},
|
|
|
|
{text: "cyan", code: FgCyan},
|
|
|
|
{text: "white", code: FgWhite},
|
|
|
|
{text: "hblack", code: FgHiBlack},
|
|
|
|
{text: "hred", code: FgHiRed},
|
|
|
|
{text: "hgreen", code: FgHiGreen},
|
|
|
|
{text: "hyellow", code: FgHiYellow},
|
|
|
|
{text: "hblue", code: FgHiBlue},
|
|
|
|
{text: "hmagent", code: FgHiMagenta},
|
|
|
|
{text: "hcyan", code: FgHiCyan},
|
|
|
|
{text: "hwhite", code: FgHiWhite},
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:58:32 +02:00
|
|
|
os.Setenv("NO_COLOR", "1")
|
2021-05-13 21:29:58 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("NO_COLOR")
|
|
|
|
})
|
|
|
|
|
2015-04-22 10:19:51 +02:00
|
|
|
for _, c := range testColors {
|
|
|
|
p := New(c.code)
|
|
|
|
p.Print(c.text)
|
|
|
|
|
|
|
|
line, _ := rb.ReadString('\n')
|
2015-04-22 10:13:04 +02:00
|
|
|
if line != c.text {
|
|
|
|
t.Errorf("Expecting %s, got '%s'\n", c.text, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 10:25:12 +03:00
|
|
|
|
2022-11-13 12:13:42 +02:00
|
|
|
func Test_noColorIsSet(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
act func()
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default",
|
|
|
|
act: func() {},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NO_COLOR=1",
|
|
|
|
act: func() { os.Setenv("NO_COLOR", "1") },
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NO_COLOR=",
|
|
|
|
act: func() { os.Setenv("NO_COLOR", "") },
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("NO_COLOR")
|
|
|
|
})
|
|
|
|
tt.act()
|
|
|
|
if got := noColorIsSet(); got != tt.want {
|
|
|
|
t.Errorf("noColorIsSet() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 10:13:04 +02:00
|
|
|
func TestColorVisual(t *testing.T) {
|
2014-02-18 11:20:24 +03:00
|
|
|
// First Visual Test
|
2016-02-12 11:13:08 +02:00
|
|
|
Output = colorable.NewColorableStdout()
|
2014-02-17 13:18:54 +03:00
|
|
|
|
|
|
|
New(FgRed).Printf("red\t")
|
|
|
|
New(BgRed).Print(" ")
|
|
|
|
New(FgRed, Bold).Println(" red")
|
|
|
|
|
|
|
|
New(FgGreen).Printf("green\t")
|
|
|
|
New(BgGreen).Print(" ")
|
|
|
|
New(FgGreen, Bold).Println(" green")
|
|
|
|
|
|
|
|
New(FgYellow).Printf("yellow\t")
|
|
|
|
New(BgYellow).Print(" ")
|
|
|
|
New(FgYellow, Bold).Println(" yellow")
|
|
|
|
|
|
|
|
New(FgBlue).Printf("blue\t")
|
|
|
|
New(BgBlue).Print(" ")
|
|
|
|
New(FgBlue, Bold).Println(" blue")
|
|
|
|
|
|
|
|
New(FgMagenta).Printf("magenta\t")
|
|
|
|
New(BgMagenta).Print(" ")
|
|
|
|
New(FgMagenta, Bold).Println(" magenta")
|
|
|
|
|
|
|
|
New(FgCyan).Printf("cyan\t")
|
|
|
|
New(BgCyan).Print(" ")
|
|
|
|
New(FgCyan, Bold).Println(" cyan")
|
|
|
|
|
|
|
|
New(FgWhite).Printf("white\t")
|
|
|
|
New(BgWhite).Print(" ")
|
|
|
|
New(FgWhite, Bold).Println(" white")
|
2014-02-17 12:58:08 +03:00
|
|
|
fmt.Println("")
|
2014-02-18 10:39:09 +03:00
|
|
|
|
|
|
|
// Second Visual test
|
2014-02-18 10:46:20 +03:00
|
|
|
Black("black")
|
2014-02-18 10:39:09 +03:00
|
|
|
Red("red")
|
2014-02-18 10:46:20 +03:00
|
|
|
Green("green")
|
|
|
|
Yellow("yellow")
|
|
|
|
Blue("blue")
|
|
|
|
Magenta("magenta")
|
|
|
|
Cyan("cyan")
|
|
|
|
White("white")
|
2017-05-23 15:53:55 +02:00
|
|
|
HiBlack("hblack")
|
|
|
|
HiRed("hred")
|
|
|
|
HiGreen("hgreen")
|
|
|
|
HiYellow("hyellow")
|
|
|
|
HiBlue("hblue")
|
|
|
|
HiMagenta("hmagenta")
|
|
|
|
HiCyan("hcyan")
|
|
|
|
HiWhite("hwhite")
|
2014-02-18 10:46:20 +03:00
|
|
|
|
2014-02-18 11:20:24 +03:00
|
|
|
// Third visual test
|
2014-02-18 13:52:23 +03:00
|
|
|
fmt.Println()
|
2014-02-18 11:20:24 +03:00
|
|
|
Set(FgBlue)
|
|
|
|
fmt.Println("is this blue?")
|
|
|
|
Unset()
|
|
|
|
|
|
|
|
Set(FgMagenta)
|
|
|
|
fmt.Println("and this magenta?")
|
|
|
|
Unset()
|
|
|
|
|
2014-02-18 13:52:23 +03:00
|
|
|
// Fourth Visual test
|
|
|
|
fmt.Println()
|
|
|
|
blue := New(FgBlue).PrintlnFunc()
|
|
|
|
blue("blue text with custom print func")
|
|
|
|
|
|
|
|
red := New(FgRed).PrintfFunc()
|
|
|
|
red("red text with a printf func: %d\n", 123)
|
|
|
|
|
2014-02-18 14:18:22 +03:00
|
|
|
put := New(FgYellow).SprintFunc()
|
|
|
|
warn := New(FgRed).SprintFunc()
|
|
|
|
|
2015-03-12 14:00:06 +02:00
|
|
|
fmt.Fprintf(Output, "this is a %s and this is %s.\n", put("warning"), warn("error"))
|
2014-02-18 14:18:22 +03:00
|
|
|
|
|
|
|
info := New(FgWhite, BgGreen).SprintFunc()
|
2015-03-12 14:00:06 +02:00
|
|
|
fmt.Fprintf(Output, "this %s rocks!\n", info("package"))
|
2014-02-18 14:18:22 +03:00
|
|
|
|
2017-01-03 17:56:11 +02:00
|
|
|
notice := New(FgBlue).FprintFunc()
|
|
|
|
notice(os.Stderr, "just a blue notice to stderr")
|
|
|
|
|
2014-02-24 21:01:46 +03:00
|
|
|
// Fifth Visual Test
|
|
|
|
fmt.Println()
|
|
|
|
|
2015-03-12 14:00:06 +02:00
|
|
|
fmt.Fprintln(Output, BlackString("black"))
|
|
|
|
fmt.Fprintln(Output, RedString("red"))
|
|
|
|
fmt.Fprintln(Output, GreenString("green"))
|
|
|
|
fmt.Fprintln(Output, YellowString("yellow"))
|
|
|
|
fmt.Fprintln(Output, BlueString("blue"))
|
|
|
|
fmt.Fprintln(Output, MagentaString("magenta"))
|
|
|
|
fmt.Fprintln(Output, CyanString("cyan"))
|
|
|
|
fmt.Fprintln(Output, WhiteString("white"))
|
2017-05-23 15:53:55 +02:00
|
|
|
fmt.Fprintln(Output, HiBlackString("hblack"))
|
|
|
|
fmt.Fprintln(Output, HiRedString("hred"))
|
|
|
|
fmt.Fprintln(Output, HiGreenString("hgreen"))
|
|
|
|
fmt.Fprintln(Output, HiYellowString("hyellow"))
|
|
|
|
fmt.Fprintln(Output, HiBlueString("hblue"))
|
|
|
|
fmt.Fprintln(Output, HiMagentaString("hmagenta"))
|
|
|
|
fmt.Fprintln(Output, HiCyanString("hcyan"))
|
|
|
|
fmt.Fprintln(Output, HiWhiteString("hwhite"))
|
2015-04-22 10:06:21 +02:00
|
|
|
}
|
2016-09-27 17:48:50 +02:00
|
|
|
|
|
|
|
func TestNoFormat(t *testing.T) {
|
|
|
|
fmt.Printf("%s %%s = ", BlackString("Black"))
|
|
|
|
Black("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", RedString("Red"))
|
|
|
|
Red("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", GreenString("Green"))
|
|
|
|
Green("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", YellowString("Yellow"))
|
|
|
|
Yellow("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", BlueString("Blue"))
|
|
|
|
Blue("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", MagentaString("Magenta"))
|
|
|
|
Magenta("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", CyanString("Cyan"))
|
|
|
|
Cyan("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", WhiteString("White"))
|
|
|
|
White("%s")
|
2017-05-23 15:53:55 +02:00
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", HiBlackString("HiBlack"))
|
|
|
|
HiBlack("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", HiRedString("HiRed"))
|
|
|
|
HiRed("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", HiGreenString("HiGreen"))
|
|
|
|
HiGreen("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", HiYellowString("HiYellow"))
|
|
|
|
HiYellow("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", HiBlueString("HiBlue"))
|
|
|
|
HiBlue("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", HiMagentaString("HiMagenta"))
|
|
|
|
HiMagenta("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", HiCyanString("HiCyan"))
|
|
|
|
HiCyan("%s")
|
|
|
|
|
|
|
|
fmt.Printf("%s %%s = ", HiWhiteString("HiWhite"))
|
|
|
|
HiWhite("%s")
|
2016-09-27 17:48:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoFormatString(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
f func(string, ...interface{}) string
|
|
|
|
format string
|
|
|
|
args []interface{}
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{BlackString, "%s", nil, "\x1b[30m%s\x1b[0m"},
|
|
|
|
{RedString, "%s", nil, "\x1b[31m%s\x1b[0m"},
|
|
|
|
{GreenString, "%s", nil, "\x1b[32m%s\x1b[0m"},
|
|
|
|
{YellowString, "%s", nil, "\x1b[33m%s\x1b[0m"},
|
|
|
|
{BlueString, "%s", nil, "\x1b[34m%s\x1b[0m"},
|
|
|
|
{MagentaString, "%s", nil, "\x1b[35m%s\x1b[0m"},
|
|
|
|
{CyanString, "%s", nil, "\x1b[36m%s\x1b[0m"},
|
|
|
|
{WhiteString, "%s", nil, "\x1b[37m%s\x1b[0m"},
|
2017-05-23 15:53:55 +02:00
|
|
|
{HiBlackString, "%s", nil, "\x1b[90m%s\x1b[0m"},
|
|
|
|
{HiRedString, "%s", nil, "\x1b[91m%s\x1b[0m"},
|
|
|
|
{HiGreenString, "%s", nil, "\x1b[92m%s\x1b[0m"},
|
|
|
|
{HiYellowString, "%s", nil, "\x1b[93m%s\x1b[0m"},
|
|
|
|
{HiBlueString, "%s", nil, "\x1b[94m%s\x1b[0m"},
|
|
|
|
{HiMagentaString, "%s", nil, "\x1b[95m%s\x1b[0m"},
|
|
|
|
{HiCyanString, "%s", nil, "\x1b[96m%s\x1b[0m"},
|
|
|
|
{HiWhiteString, "%s", nil, "\x1b[97m%s\x1b[0m"},
|
2016-09-27 17:48:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2021-12-16 10:00:24 +02:00
|
|
|
s := test.f(test.format, test.args...)
|
|
|
|
|
2016-09-27 17:48:50 +02:00
|
|
|
if s != test.want {
|
|
|
|
t.Errorf("[%d] want: %q, got: %q", i, test.want, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-12 14:54:28 +02:00
|
|
|
|
|
|
|
func TestColor_Println_Newline(t *testing.T) {
|
|
|
|
rb := new(bytes.Buffer)
|
|
|
|
Output = rb
|
|
|
|
|
|
|
|
c := New(FgRed)
|
|
|
|
c.Println("foo")
|
|
|
|
|
|
|
|
got := readRaw(t, rb)
|
|
|
|
want := "\x1b[31mfoo\x1b[0m\n"
|
|
|
|
|
|
|
|
if want != got {
|
|
|
|
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestColor_Sprintln_Newline(t *testing.T) {
|
|
|
|
c := New(FgRed)
|
|
|
|
|
|
|
|
got := c.Sprintln("foo")
|
|
|
|
want := "\x1b[31mfoo\x1b[0m\n"
|
|
|
|
|
|
|
|
if want != got {
|
|
|
|
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestColor_Fprintln_Newline(t *testing.T) {
|
|
|
|
rb := new(bytes.Buffer)
|
|
|
|
c := New(FgRed)
|
|
|
|
c.Fprintln(rb, "foo")
|
|
|
|
|
|
|
|
got := readRaw(t, rb)
|
|
|
|
want := "\x1b[31mfoo\x1b[0m\n"
|
|
|
|
|
|
|
|
if want != got {
|
|
|
|
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readRaw(t *testing.T, r io.Reader) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
out, err := io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|