diff --git a/printer/printer_test.go b/printer/printer_test.go index e732c8f96..6dbf0c389 100644 --- a/printer/printer_test.go +++ b/printer/printer_test.go @@ -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) { @@ -34,6 +32,9 @@ 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]) }) @@ -41,32 +42,38 @@ func TestPrintT(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)) }) }