Repository navigation
Expand file tree
/
Copy pathtag_test.go
More file actions
88 lines (84 loc) · 1.49 KB
/
Copy pathtag_test.go
File metadata and controls
88 lines (84 loc) · 1.49 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package _struct
import (
"reflect"
"testing"
)
// Sample 用于测试的结构体
type Sample struct {
A int `json:"a" xml:"A"`
B string `xml:"b"`
C bool
}
func TestGetTag(t *testing.T) {
tests := []struct {
name string
input any
key string
want map[string]any
wantErr bool
}{
{
name: "非结构体类型",
input: 123,
key: "json",
wantErr: true,
},
{
name: "结构体值类型,json tag",
input: Sample{A: 1, B: "x", C: true},
key: "json",
want: map[string]any{
"A": "a",
"B": "",
"C": "",
},
},
{
name: "结构体值类型,xml tag",
input: Sample{A: 2, B: "y", C: false},
key: "xml",
want: map[string]any{
"A": "A",
"B": "b",
"C": "",
},
},
{
name: "结构体指针,json tag",
input: &Sample{A: 3, B: "z", C: false},
key: "json",
want: map[string]any{
"A": "a",
"B": "",
"C": "",
},
},
{
name: "nil 结构体指针",
input: (*Sample)(nil),
key: "xml",
want: map[string]any{
"A": "A",
"B": "b",
"C": "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetTag(tt.input, tt.key)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetTag(%#v, %q) = %#v; want %#v", tt.input, tt.key, got, tt.want)
}
})
}
}