Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
printer: incorporate review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
isacikgoz committed Mar 15, 2020
1 parent 4516a58 commit 8d103a6
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import (
"github.com/stretchr/testify/assert"
)

type mockWriter struct {
buffer []byte
}
type mockWriter []byte

func (w *mockWriter) Write(b []byte) (n int, err error) {
w.buffer = append(w.buffer, b...)
return len(w.buffer) - len(b), nil
*w = append(*w, b...)
return len(*w) - len(b), nil
}

func TestPrintT(t *testing.T) {
Expand All @@ -34,39 +32,48 @@ func TestPrintT(t *testing.T) {
t.Run("should execute template", func(t *testing.T) {
tpl := `testing template {{.ID}}`
PrintT(tpl, ts)
assert.Len(t, GetLines(), 1)

Flush()
assert.Equal(t, "testing template 123", printer.Lines[0])
})

t.Run("should fail to execute, no method or field", func(t *testing.T) {
Clean()
tpl := `testing template {{.Name}}`
PrintT(tpl, ts)
assert.Len(t, GetErrorLines(), 1)

Flush()
assert.Equal(t, "Can't print the message using the provided template: "+tpl, printer.ErrorLines[0])
})
}

func TestFlush(t *testing.T) {
mw := &mockWriter{}
printer.writer = mw
printer.Format = FormatJSON

t.Run("should print a line in JSON format", func(t *testing.T) {
mw.buffer = []byte{}
mw := &mockWriter{}
printer.writer = mw
Clean()

Print("test string")
Flush()
assert.Len(t, GetLines(), 1)

assert.Equal(t, "[\n \"test string\"\n]\n", string(mw.buffer))
Flush()
assert.Equal(t, "[\n \"test string\"\n]\n", string(*mw))
})

t.Run("should print multi line in JSON format", func(t *testing.T) {
mw.buffer = []byte{}
Clean()
mw := &mockWriter{}
printer.writer = mw

Clean()
Print("test string-1")
Print("test string-2")
assert.Len(t, GetLines(), 2)

Flush()
assert.Equal(t, "[\n \"test string-1\",\n \"test string-2\"\n]\n", string(mw.buffer))
assert.Equal(t, "[\n \"test string-1\",\n \"test string-2\"\n]\n", string(*mw))
})
}

0 comments on commit 8d103a6

Please sign in to comment.