structs_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spec
  15. import (
  16. "encoding/json"
  17. "reflect"
  18. "testing"
  19. "github.com/stretchr/testify/assert"
  20. "gopkg.in/yaml.v2"
  21. )
  22. func assertSerializeJSON(t testing.TB, actual interface{}, expected string) bool {
  23. ser, err := json.Marshal(actual)
  24. if err != nil {
  25. return assert.Fail(t, "unable to marshal to json (%s): %#v", err, actual)
  26. }
  27. return assert.Equal(t, string(ser), expected)
  28. }
  29. func assertSerializeYAML(t testing.TB, actual interface{}, expected string) bool {
  30. ser, err := yaml.Marshal(actual)
  31. if err != nil {
  32. return assert.Fail(t, "unable to marshal to yaml (%s): %#v", err, actual)
  33. }
  34. return assert.Equal(t, string(ser), expected)
  35. }
  36. func derefTypeOf(expected interface{}) (tpe reflect.Type) {
  37. tpe = reflect.TypeOf(expected)
  38. if tpe.Kind() == reflect.Ptr {
  39. tpe = tpe.Elem()
  40. }
  41. return
  42. }
  43. func isPointed(expected interface{}) (pointed bool) {
  44. tpe := reflect.TypeOf(expected)
  45. if tpe.Kind() == reflect.Ptr {
  46. pointed = true
  47. }
  48. return
  49. }
  50. func assertParsesJSON(t testing.TB, actual string, expected interface{}) bool {
  51. parsed := reflect.New(derefTypeOf(expected))
  52. err := json.Unmarshal([]byte(actual), parsed.Interface())
  53. if err != nil {
  54. return assert.Fail(t, "unable to unmarshal from json (%s): %s", err, actual)
  55. }
  56. act := parsed.Interface()
  57. if !isPointed(expected) {
  58. act = reflect.Indirect(parsed).Interface()
  59. }
  60. return assert.Equal(t, act, expected)
  61. }
  62. func assertParsesYAML(t testing.TB, actual string, expected interface{}) bool {
  63. parsed := reflect.New(derefTypeOf(expected))
  64. err := yaml.Unmarshal([]byte(actual), parsed.Interface())
  65. if err != nil {
  66. return assert.Fail(t, "unable to unmarshal from yaml (%s): %s", err, actual)
  67. }
  68. act := parsed.Interface()
  69. if !isPointed(expected) {
  70. act = reflect.Indirect(parsed).Interface()
  71. }
  72. return assert.EqualValues(t, act, expected)
  73. }
  74. func TestSerialization_SerializeJSON(t *testing.T) {
  75. assertSerializeJSON(t, []string{"hello"}, "[\"hello\"]")
  76. assertSerializeJSON(t, []string{"hello", "world", "and", "stuff"}, "[\"hello\",\"world\",\"and\",\"stuff\"]")
  77. assertSerializeJSON(t, StringOrArray(nil), "null")
  78. assertSerializeJSON(t, SchemaOrArray{
  79. Schemas: []Schema{
  80. {SchemaProps: SchemaProps{Type: []string{"string"}}}},
  81. }, "[{\"type\":\"string\"}]")
  82. assertSerializeJSON(t, SchemaOrArray{
  83. Schemas: []Schema{
  84. {SchemaProps: SchemaProps{Type: []string{"string"}}},
  85. {SchemaProps: SchemaProps{Type: []string{"string"}}},
  86. }}, "[{\"type\":\"string\"},{\"type\":\"string\"}]")
  87. assertSerializeJSON(t, SchemaOrArray{}, "null")
  88. }
  89. func TestSerialization_DeserializeJSON(t *testing.T) {
  90. // String
  91. assertParsesJSON(t, "\"hello\"", StringOrArray([]string{"hello"}))
  92. assertParsesJSON(t, "[\"hello\",\"world\",\"and\",\"stuff\"]",
  93. StringOrArray([]string{"hello", "world", "and", "stuff"}))
  94. assertParsesJSON(t, "[\"hello\",\"world\",null,\"stuff\"]", StringOrArray([]string{"hello", "world", "", "stuff"}))
  95. assertParsesJSON(t, "null", StringOrArray(nil))
  96. // Schema
  97. assertParsesJSON(t, "{\"type\":\"string\"}", SchemaOrArray{Schema: &Schema{
  98. SchemaProps: SchemaProps{Type: []string{"string"}}},
  99. })
  100. assertParsesJSON(t, "[{\"type\":\"string\"},{\"type\":\"string\"}]", &SchemaOrArray{
  101. Schemas: []Schema{
  102. {SchemaProps: SchemaProps{Type: []string{"string"}}},
  103. {SchemaProps: SchemaProps{Type: []string{"string"}}},
  104. },
  105. })
  106. assertParsesJSON(t, "null", SchemaOrArray{})
  107. }