Added jsonutil functional tests

This commit is contained in:
vishalnayak 2016-07-07 12:12:51 -04:00
parent 2dc811d1b3
commit c62a368ccd
2 changed files with 66 additions and 7 deletions

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"reflect"
)
// Encodes/Marshals the given object into JSON
@ -39,12 +38,6 @@ func DecodeJSONFromReader(r io.Reader, out interface{}) error {
return fmt.Errorf("output parameter 'out' is nil")
}
// Decoding requires a pointer type to be supplied
value := reflect.ValueOf(out)
if value.Kind() != reflect.Ptr {
return fmt.Errorf("decoding the value into a non-pointer type: %v", reflect.TypeOf(out))
}
dec := json.NewDecoder(r)
// While decoding JSON values, intepret the integer values as `json.Number`s instead of `float64`.

View File

@ -0,0 +1,66 @@
package jsonutil
import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"
)
func Test_EncodeJSON(t *testing.T) {
input := map[string]interface{}{
"test": "data",
"validation": "process",
}
actualBytes, err := EncodeJSON(input)
if err != nil {
t.Fatalf("failed to encode JSON: %v", err)
}
actual := strings.TrimSpace(string(actualBytes))
expected := `{"test":"data","validation":"process"}`
if actual != expected {
t.Fatal("bad: encoded JSON: expected:%s\nactual:%s\n", expected, string(actualBytes))
}
}
func Test_DecodeJSON(t *testing.T) {
input := `{"test":"data","validation":"process"}`
var actual map[string]interface{}
err := DecodeJSON([]byte(input), &actual)
if err != nil {
fmt.Printf("decoding err: %v\n", err)
}
expected := map[string]interface{}{
"test": "data",
"validation": "process",
}
if !reflect.DeepEqual(actual, expected) {
t.Fatal("bad: expected:%#v\nactual:%#v", expected, actual)
}
}
func Test_DecodeJSONFromReader(t *testing.T) {
input := `{"test":"data","validation":"process"}`
var actual map[string]interface{}
err := DecodeJSONFromReader(bytes.NewReader([]byte(input)), &actual)
if err != nil {
fmt.Printf("decoding err: %v\n", err)
}
expected := map[string]interface{}{
"test": "data",
"validation": "process",
}
if !reflect.DeepEqual(actual, expected) {
t.Fatal("bad: expected:%#v\nactual:%#v", expected, actual)
}
}