-
Notifications
You must be signed in to change notification settings - Fork 1
/
internal_test.go
80 lines (69 loc) · 1.92 KB
/
internal_test.go
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
package sashay
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"reflect"
)
var _ = Describe("Swagger internals test", func() {
Describe("jsonName", func() {
type Tester struct {
Dash int `json:"-"`
None int
NoneMapped int `json:",omitempty"`
Mapped int `json:"mapped"`
}
testerType := reflect.TypeOf(Tester{})
It("returns empty string for json tag value of -", func() {
f, _ := testerType.FieldByName("Dash")
Expect(jsonName(f)).To(Equal(""))
})
It("returns empty string for no JSON tag", func() {
f, _ := testerType.FieldByName("None")
Expect(jsonName(f)).To(Equal(""))
})
It("returns the field name if there is no JSON name mapped (',omitempty')", func() {
f, _ := testerType.FieldByName("NoneMapped")
Expect(jsonName(f)).To(Equal("NoneMapped"))
})
It("returns mapped json name", func() {
f, _ := testerType.FieldByName("Mapped")
Expect(jsonName(f)).To(Equal("mapped"))
})
})
Describe("isExportedField", func() {
It("is true for anonymous fields", func() {
type T struct {
int
}
Expect(isExportedField(reflect.TypeOf(T{}).Field(0))).To(BeTrue())
})
It("is true for unnamed fields (not sure if happens in real world)", func() {
type T struct {
field int
}
f := reflect.TypeOf(T{}).Field(0)
f.Name = ""
Expect(isExportedField(f)).To(BeTrue())
})
It("is true for fields with an exported name", func() {
type T struct {
Field int `json:"field"`
}
Expect(isExportedField(reflect.TypeOf(T{}).Field(0))).To(BeTrue())
})
It("is false for fields with an unexported name", func() {
//noinspection GoStructTag
type T struct {
field int `path:"field"`
}
Expect(isExportedField(reflect.TypeOf(T{}).Field(0))).To(BeFalse())
})
})
Describe("isExportedName", func() {
It("panics if used with an empty string", func() {
Expect(func() {
isExportedName("")
}).To(Panic())
})
})
})