Skip to content

Commit

Permalink
Add tests for Lprint
Browse files Browse the repository at this point in the history
  • Loading branch information
rdnt committed Feb 27, 2020
1 parent 18f055e commit 9af8026
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
16 changes: 8 additions & 8 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package color
import (
"fmt"
"io"
"log"
"regexp"
"strings"
"log"
)

// output colored text like use html tag. (not support windows cmd)
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package color

import (
"testing"

"log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -31,7 +31,7 @@ func TestReplaceTag(t *testing.T) {
is.NotContains(r, ">")

// sample 3
s = `abc <err>err-text</>
s = `abc <err>err-text</>
def <info>info text
</>`
r = ReplaceTag(s)
Expand Down Expand Up @@ -135,6 +135,12 @@ func TestPrint(t *testing.T) {
Fprintf(buf, "<red>%s</>", "MSG")
is.Equal("\x1b[0;31mMSG\x1b[0m", buf.String())
buf.Reset()

// Lprint
logger := log.New(buf, "", 0)
Lprint(logger, "<red>MSG</>\n")
is.Equal("\x1b[0;31mMSG\x1b[0m\n", buf.String())
buf.Reset()
}

func TestWrapTag(t *testing.T) {
Expand All @@ -157,7 +163,7 @@ func TestClearTag(t *testing.T) {
is.Equal("text", ClearTag("<err>text</>"))
is.Equal("abc error def info text", ClearTag("abc <err>error</> def <info>info text</>"))

str := `abc <err>err-text</>
str := `abc <err>err-text</>
def <info>info text
</>`
ret := ClearTag(str)
Expand Down

0 comments on commit 9af8026

Please sign in to comment.