package color import ( "bytes" "fmt" "os" "testing" ) func TestColor(t *testing.T) { rb := new(bytes.Buffer) Output = rb testColors := []struct { text string code Parameter }{ {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) } } // Visual Test fmt.Println("") Output = os.Stdout 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") fmt.Println("") // Second Visual test Black("black") Red("red") Green("green") Yellow("yellow") Blue("blue") Magenta("magenta") Cyan("cyan") White("white") }