-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathxml_object_test.go
More file actions
62 lines (52 loc) · 1.61 KB
/
xml_object_test.go
File metadata and controls
62 lines (52 loc) · 1.61 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
55
56
57
58
59
60
61
62
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package spec
import (
"encoding/json"
"testing"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)
func TestXmlObject_Serialize(t *testing.T) {
obj1 := XMLObject{}
actual, err := json.Marshal(obj1)
require.NoError(t, err)
assert.EqualT(t, "{}", string(actual))
obj2 := XMLObject{
Name: "the name",
Namespace: "the namespace",
Prefix: "the prefix",
Attribute: true,
Wrapped: true,
}
actual, err = json.Marshal(obj2)
require.NoError(t, err)
var ad map[string]any
require.NoError(t, json.Unmarshal(actual, &ad))
assert.Equal(t, obj2.Name, ad["name"])
assert.Equal(t, obj2.Namespace, ad["namespace"])
assert.Equal(t, obj2.Prefix, ad["prefix"])
attrVal, ok := ad["attribute"].(bool)
require.TrueT(t, ok, "expected bool for attribute")
assert.TrueT(t, attrVal)
wrappedVal, ok := ad["wrapped"].(bool)
require.TrueT(t, ok, "expected bool for wrapped")
assert.TrueT(t, wrappedVal)
}
func TestXmlObject_Deserialize(t *testing.T) {
expected := XMLObject{}
actual := XMLObject{}
require.NoError(t, json.Unmarshal([]byte("{}"), &actual))
assert.EqualT(t, expected, actual)
completed := `{"name":"the name","namespace":"the namespace","prefix":"the prefix","attribute":true,"wrapped":true}`
expected = XMLObject{
Name: "the name",
Namespace: "the namespace",
Prefix: "the prefix",
Attribute: true,
Wrapped: true,
}
actual = XMLObject{}
require.NoError(t, json.Unmarshal([]byte(completed), &actual))
assert.EqualT(t, expected, actual)
}