Skip to content

Commit

Permalink
Test: Add tests for color.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 20, 2018
1 parent 01e6131 commit 84fa845
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
10 changes: 0 additions & 10 deletions color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,7 @@ func Green(style int, format string, a ...interface{}) string {
return colorString(FgGreen, style, format, a...)
}

// Yellow returns a string with yellow foreground
func Yellow(style int, format string, a ...interface{}) string {
return colorString(FgYellow, style, format, a...)
}

// Blue returns a string with blue foreground
func Blue(style int, format string, a ...interface{}) string {
return colorString(FgBlue, style, format, a...)
}

// Purple returns a string with purple foreground
func Purple(style int, format string, a ...interface{}) string {
return colorString(FgPurple, style, format, a...)
}
54 changes: 54 additions & 0 deletions color/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package color

import (
"os"
"testing"
)

// Setup tests
var tests = []struct {
color func(style int, format string, a ...interface{}) string
input []string
expected string
}{
{Black, []string{"%s", "foobar"}, "\033[30;1mfoobar"},
{Red, []string{"%s %s", "foobar", "hey"}, "\033[31;1mfoobar hey"},
{Green, []string{"%s", ""}, "\033[32;1m"},
{Blue, []string{"foobar"}, "\033[34;1mfoobar"},
}

func TestColors(t *testing.T) {
// Redirect stdout
oldStdout := os.Stdout
_, w, err := os.Pipe()
if err != nil {
t.Fatalf("ColorString failed to redirect stdout because %s", err.Error())
}
os.Stdout = w

// Restore old stdout
defer func() {
w.Close()
os.Stdout = oldStdout
}()

// Check output
var out string
for _, test := range tests {
if len(test.input) == 1 {
// With formatter
out = test.color(Regular, test.input[0])
} else {
// Without formatter
s := make([]interface{}, len(test.input)-1)
for i, v := range test.input[1:] {
s[i] = v
}
out = test.color(Regular, test.input[0], s...)
}

if test.expected != out {
t.Errorf("Color failed: got %s, want %s", out, test.expected)
}
}
}

0 comments on commit 84fa845

Please sign in to comment.