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"
|
2014-02-18 10:25:12 +03:00
|
|
|
"os"
|
2014-02-17 12:12:32 +03:00
|
|
|
"testing"
|
2015-03-12 14:00:06 +02:00
|
|
|
|
|
|
|
"github.com/shiena/ansicolor"
|
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},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
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},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-05-04 20:28:38 +02:00
|
|
|
defer func() {
|
|
|
|
NoColor = false
|
|
|
|
}()
|
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
|
|
|
|
2015-04-22 10:13:04 +02:00
|
|
|
func TestColorVisual(t *testing.T) {
|
2014-02-18 11:20:24 +03:00
|
|
|
// First Visual Test
|
2014-02-18 10:25:12 +03:00
|
|
|
fmt.Println("")
|
2015-03-12 14:00:06 +02:00
|
|
|
Output = ansicolor.NewAnsiColorWriter(os.Stdout)
|
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")
|
|
|
|
|
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
|
|
|
|
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"))
|
2015-04-22 10:06:21 +02:00
|
|
|
}
|