From 9af80269e5927023fddd11a997ed49b80f809972 Mon Sep 17 00:00:00 2001 From: Tasos papalyras Date: Fri, 28 Feb 2020 01:18:16 +0200 Subject: [PATCH] Add tests for Lprint --- tag.go | 16 ++++++++-------- tag_test.go | 12 +++++++++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tag.go b/tag.go index 38112d6..50daa40 100644 --- a/tag.go +++ b/tag.go @@ -3,9 +3,9 @@ package color import ( "fmt" "io" + "log" "regexp" "strings" - "log" ) // output colored text like use html tag. (not support windows cmd) @@ -168,13 +168,13 @@ func Fprintln(w io.Writer, a ...interface{}) { // Lprint passes colored messages to a log.Logger for printing. // Notice: should be goroutine safe func Lprint(l *log.Logger, a ...interface{}) { - if isLikeInCmd { - renderColorCodeOnCmd(func() { - l.Print(Render(a...)) - }) - } else { - l.Print(Render(a...)) - } + if isLikeInCmd { + renderColorCodeOnCmd(func() { + l.Print(Render(a...)) + }) + } else { + l.Print(Render(a...)) + } } // Render parse color tags, return rendered string. diff --git a/tag_test.go b/tag_test.go index 7ba187a..e0e84ce 100644 --- a/tag_test.go +++ b/tag_test.go @@ -2,7 +2,7 @@ package color import ( "testing" - + "log" "github.com/stretchr/testify/assert" ) @@ -31,7 +31,7 @@ func TestReplaceTag(t *testing.T) { is.NotContains(r, ">") // sample 3 - s = `abc err-text + s = `abc err-text def info text ` r = ReplaceTag(s) @@ -135,6 +135,12 @@ func TestPrint(t *testing.T) { Fprintf(buf, "%s", "MSG") is.Equal("\x1b[0;31mMSG\x1b[0m", buf.String()) buf.Reset() + + // Lprint + logger := log.New(buf, "", 0) + Lprint(logger, "MSG\n") + is.Equal("\x1b[0;31mMSG\x1b[0m\n", buf.String()) + buf.Reset() } func TestWrapTag(t *testing.T) { @@ -157,7 +163,7 @@ func TestClearTag(t *testing.T) { is.Equal("text", ClearTag("text")) is.Equal("abc error def info text", ClearTag("abc error def info text")) - str := `abc err-text + str := `abc err-text def info text ` ret := ClearTag(str)