Add JsonPrintError
This commit is contained in:
41
json_test.go
Normal file
41
json_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package commons
|
||||
|
||||
import (
|
||||
"log"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJsonPrintError(t *testing.T) {
|
||||
|
||||
jsonstr := `
|
||||
{
|
||||
"name": "George",
|
||||
"age": 87,
|
||||
active": "false",
|
||||
"properties": {
|
||||
"one": "two",
|
||||
"three": "four"
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
|
||||
content := []byte(jsonstr)
|
||||
var data map[string]any
|
||||
|
||||
err := json.Unmarshal(content, &data)
|
||||
|
||||
JsonPrintError(err, content)
|
||||
|
||||
errOut := fmt.Sprintf("%s", buf.String())
|
||||
|
||||
if errOut == "" {
|
||||
t.Fatal("should have printed the error")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user