| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package spec |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"reflect" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/stretchr/testify/assert" |
| 23 |
) |
| 24 |
|
| 25 |
func assertSerializeJSON(t testing.TB, actual interface{}, expected string) bool { |
| 26 |
ser, err := json.Marshal(actual) |
| 27 |
if err != nil { |
| 28 |
return assert.Fail(t, "unable to marshal to json (%s): %#v", err, actual) |
| 29 |
} |
| 30 |
return assert.Equal(t, string(ser), expected) |
| 31 |
} |
| 32 |
|
| 33 |
func derefTypeOf(expected interface{}) (tpe reflect.Type) { |
| 34 |
tpe = reflect.TypeOf(expected) |
| 35 |
if tpe.Kind() == reflect.Ptr { |
| 36 |
tpe = tpe.Elem() |
| 37 |
} |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
func isPointed(expected interface{}) (pointed bool) { |
| 42 |
tpe := reflect.TypeOf(expected) |
| 43 |
if tpe.Kind() == reflect.Ptr { |
| 44 |
pointed = true |
| 45 |
} |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
func assertParsesJSON(t testing.TB, actual string, expected interface{}) bool { |
| 50 |
parsed := reflect.New(derefTypeOf(expected)) |
| 51 |
err := json.Unmarshal([]byte(actual), parsed.Interface()) |
| 52 |
if err != nil { |
| 53 |
return assert.Fail(t, "Failed to unmarshal JSON", "unable to unmarshal from json (%v): %s", err, actual) |
| 54 |
} |
| 55 |
act := parsed.Interface() |
| 56 |
if !isPointed(expected) { |
| 57 |
act = reflect.Indirect(parsed).Interface() |
| 58 |
} |
| 59 |
return assert.Equal(t, act, expected) |
| 60 |
} |
| 61 |
|
| 62 |
func TestSerialization_SerializeJSON(t *testing.T) { |
| 63 |
assertSerializeJSON(t, []string{"hello"}, "[\"hello\"]") |
| 64 |
assertSerializeJSON(t, []string{"hello", "world", "and", "stuff"}, "[\"hello\",\"world\",\"and\",\"stuff\"]") |
| 65 |
assertSerializeJSON(t, StringOrArray(nil), "null") |
| 66 |
assertSerializeJSON(t, SchemaOrArray{ |
| 67 |
Schemas: []Schema{ |
| 68 |
{SchemaProps: SchemaProps{Type: []string{"string"}}}}, |
| 69 |
}, "[{\"type\":\"string\"}]") |
| 70 |
assertSerializeJSON(t, SchemaOrArray{ |
| 71 |
Schemas: []Schema{ |
| 72 |
{SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 73 |
{SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 74 |
}}, "[{\"type\":\"string\"},{\"type\":\"string\"}]") |
| 75 |
assertSerializeJSON(t, SchemaOrArray{}, "null") |
| 76 |
} |
| 77 |
|
| 78 |
func TestSerialization_DeserializeJSON(t *testing.T) { |
| 79 |
|
| 80 |
assertParsesJSON(t, "\"hello\"", StringOrArray([]string{"hello"})) |
| 81 |
assertParsesJSON(t, "[\"hello\",\"world\",\"and\",\"stuff\"]", |
| 82 |
StringOrArray([]string{"hello", "world", "and", "stuff"})) |
| 83 |
assertParsesJSON(t, "[\"hello\",\"world\",null,\"stuff\"]", StringOrArray([]string{"hello", "world", "", "stuff"})) |
| 84 |
assertParsesJSON(t, "null", StringOrArray(nil)) |
| 85 |
|
| 86 |
|
| 87 |
assertParsesJSON(t, "{\"type\":\"string\"}", SchemaOrArray{Schema: &Schema{ |
| 88 |
SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 89 |
}) |
| 90 |
assertParsesJSON(t, "[{\"type\":\"string\"},{\"type\":\"string\"}]", &SchemaOrArray{ |
| 91 |
Schemas: []Schema{ |
| 92 |
{SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 93 |
{SchemaProps: SchemaProps{Type: []string{"string"}}}, |
| 94 |
}, |
| 95 |
}) |
| 96 |
assertParsesJSON(t, "null", SchemaOrArray{}) |
| 97 |
} |