-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathstructs_test.go
More file actions
54 lines (48 loc) · 1.74 KB
/
structs_test.go
File metadata and controls
54 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package spec
import (
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func TestSerialization_SerializeJSON(t *testing.T) {
assert.JSONMarshalAsT(t, `["hello"]`, []string{"hello"})
assert.JSONMarshalAsT(t, `["hello","world","and","stuff"]`, []string{"hello", "world", "and", "stuff"})
assert.JSONMarshalAsT(t, `null`, StringOrArray(nil))
assert.JSONMarshalAsT(t, `[{"type":"string"}]`, SchemaOrArray{
Schemas: []Schema{
{SchemaProps: SchemaProps{Type: []string{"string"}}},
},
})
assert.JSONMarshalAsT(t, `[{"type":"string"},{"type":"string"}]`, SchemaOrArray{
Schemas: []Schema{
{SchemaProps: SchemaProps{Type: []string{"string"}}},
{SchemaProps: SchemaProps{Type: []string{"string"}}},
},
})
assert.JSONMarshalAsT(t, `null`, SchemaOrArray{})
}
func TestSerialization_DeserializeJSON(t *testing.T) {
// String
assert.JSONUnmarshalAsT(t, StringOrArray([]string{"hello"}), `"hello"`)
assert.JSONUnmarshalAsT(t,
StringOrArray([]string{"hello", "world", "and", "stuff"}),
`["hello","world","and","stuff"]`)
assert.JSONUnmarshalAsT(t,
StringOrArray([]string{"hello", "world", "", "stuff"}),
`["hello","world",null,"stuff"]`)
assert.JSONUnmarshalAsT(t, StringOrArray(nil), `null`)
// Schema
assert.JSONUnmarshalAsT(t, SchemaOrArray{
Schema: &Schema{
SchemaProps: SchemaProps{Type: []string{"string"}},
},
}, `{"type":"string"}`)
assert.JSONUnmarshalAsT(t, &SchemaOrArray{
Schemas: []Schema{
{SchemaProps: SchemaProps{Type: []string{"string"}}},
{SchemaProps: SchemaProps{Type: []string{"string"}}},
},
}, `[{"type":"string"},{"type":"string"}]`)
assert.JSONUnmarshalAsT(t, SchemaOrArray{}, `null`)
}