Skip to content

Commit

Permalink
add more unit tests for pkg/utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Kanchwala committed Jul 25, 2020
1 parent b71f018 commit 1fac82b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 22 additions & 2 deletions pkg/utils/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ package utils

import (
"bytes"
"io/ioutil"
"strings"
"testing"
)

const (
validJSONFile = "./testdata/valid.json"
)

var (
validJSON []byte
validJSONInput = map[string]int{"apple": 5, "lettuce": 7}
)

func init() {
validJSON, _ = ioutil.ReadFile(validJSONFile)

}

func TestPrintJSON(t *testing.T) {

table := []struct {
Expand All @@ -18,14 +33,19 @@ func TestPrintJSON(t *testing.T) {
input: make(map[string]interface{}),
want: "{}",
},
{
name: "valid JSON",
input: validJSONInput,
want: string(validJSON),
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
got := &bytes.Buffer{}
PrintJSON(tt.input, got)
if strings.TrimSpace(got.String()) != tt.want {
t.Errorf("got: '%v', want: '%v'", got, tt.want)
if strings.TrimSpace(got.String()) != strings.TrimSpace(tt.want) {
t.Errorf("got:\n'%v'\n, want:\n'%v'\n", got, tt.want)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/testdata/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"apple": 5,
"lettuce": 7
}

0 comments on commit 1fac82b

Please sign in to comment.