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.
* printer: add tests * printer: add license to test file * Update printer/printer_test.go Co-Authored-By: Miguel de la Cruz <[email protected]> * Update printer/printer_test.go Co-Authored-By: Miguel de la Cruz <[email protected]> * printer: incorporate review comments * printer: add tests for check to if json output clears buffer Co-authored-by: mattermod <[email protected]> Co-authored-by: Miguel de la Cruz <[email protected]>
- Loading branch information
1 parent
6d06580
commit cef1dc6
Showing
2 changed files
with
102 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,81 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
package printer | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockWriter []byte | ||
|
||
func (w *mockWriter) Write(b []byte) (n int, err error) { | ||
*w = append(*w, b...) | ||
return len(*w) - 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.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) { | ||
printer.Format = FormatJSON | ||
|
||
t.Run("should print a line in JSON format", func(t *testing.T) { | ||
mw := &mockWriter{} | ||
printer.writer = mw | ||
Clean() | ||
|
||
Print("test string") | ||
assert.Len(t, GetLines(), 1) | ||
|
||
Flush() | ||
assert.Equal(t, "[\n \"test string\"\n]\n", string(*mw)) | ||
assert.Empty(t, GetLines(), 0) | ||
}) | ||
|
||
t.Run("should print multi line in JSON format", func(t *testing.T) { | ||
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)) | ||
assert.Empty(t, GetLines(), 0) | ||
}) | ||
} |