This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package printer | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockWriter struct { | ||
buffer []byte | ||
} | ||
|
||
func (w *mockWriter) Write(b []byte) (n int, err error) { | ||
w.buffer = append(w.buffer, b...) | ||
return len(w.buffer) - len(b), nil | ||
} | ||
|
||
func TestPrintT(t *testing.T) { | ||
w := bufio.NewWriter(&bytes.Buffer{}) | ||
printer.writer = w | ||
printer.Format = FormatPlain | ||
|
||
ts := struct { | ||
ID int | ||
}{ | ||
ID: 123, | ||
} | ||
|
||
t.Run("should execute template", func(t *testing.T) { | ||
tpl := `testing template {{.ID}}` | ||
PrintT(tpl, ts) | ||
assert.Equal(t, "testing template 123", printer.Lines[0], "should be equal") | ||
}) | ||
|
||
t.Run("should fail to execute, no method or field", func(t *testing.T) { | ||
Clean() | ||
tpl := `testing template {{.Name}}` | ||
PrintT(tpl, ts) | ||
assert.Equal(t, "Can't print the message using the provided template: "+tpl, printer.ErrorLines[0]) | ||
}) | ||
} | ||
|
||
func TestFlush(t *testing.T) { | ||
mw := &mockWriter{} | ||
w := mw | ||
printer.writer = w | ||
printer.Format = FormatJSON | ||
|
||
t.Run("should print a line in JSON format", func(t *testing.T) { | ||
mw.buffer = []byte{} | ||
Clean() | ||
|
||
Print("test string") | ||
Flush() | ||
|
||
assert.Equal(t, "[\n \"test string\"\n]\n", string(mw.buffer)) | ||
}) | ||
|
||
t.Run("should print multi line in JSON format", func(t *testing.T) { | ||
mw.buffer = []byte{} | ||
Clean() | ||
|
||
Print("test string-1") | ||
Print("test string-2") | ||
Flush() | ||
assert.Equal(t, "[\n \"test string-1\",\n \"test string-2\"\n]\n", string(mw.buffer)) | ||
}) | ||
} |