File: util_test.go

package info (click to toggle)
golang-github-gofiber-schema 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 288 kB
  • sloc: makefile: 46
file content (182 lines) | stat: -rw-r--r-- 4,094 bytes parent folder | download | duplicates (2)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package schema

import (
	"reflect"
	"testing"
)

func TestParseTagAndOptions(t *testing.T) {
	alias, opts := parseTag("name,omitempty,default:foo")
	if alias != "name" {
		t.Fatalf("expected alias name, got %s", alias)
	}
	if !opts.Contains("omitempty") {
		t.Fatalf("expected omitempty option")
	}
	if val := opts.getDefaultOptionValue(); val != "foo" {
		t.Fatalf("expected default foo, got %s", val)
	}
}

func TestFieldAlias(t *testing.T) {
	type S struct {
		Field string `json:"custom,omitempty"`
	}
	f, ok := reflect.TypeOf(S{}).FieldByName("Field")
	if !ok {
		t.Fatal("field not found")
	}
	alias, opts := fieldAlias(f, "json")
	if alias != "custom" {
		t.Fatalf("expected alias custom, got %s", alias)
	}
	if !opts.Contains("omitempty") {
		t.Fatalf("expected omitempty option")
	}
}

func TestTagOptionsContains(t *testing.T) {
	opts := tagOptions{"a", "b", "default:val"}
	if !opts.Contains("a") || opts.Contains("c") {
		t.Fatalf("contains failed")
	}
	if val := opts.getDefaultOptionValue(); val != "val" {
		t.Fatalf("expected default val, got %s", val)
	}
}

func TestIsValidStructPointer(t *testing.T) {
	type S struct{}
	if !isValidStructPointer(reflect.ValueOf(&S{})) {
		t.Errorf("expected true for struct pointer")
	}
	if isValidStructPointer(reflect.ValueOf(S{})) {
		t.Errorf("expected false for struct value")
	}
	var sp *S
	if isValidStructPointer(reflect.ValueOf(sp)) {
		t.Errorf("expected false for nil pointer")
	}
	var i int
	if isValidStructPointer(reflect.ValueOf(&i)) {
		t.Errorf("expected false for pointer to non-struct")
	}
}

func TestConvertPointer(t *testing.T) {
	v := convertPointer(reflect.Bool, "true")
	if !v.IsValid() || !v.Elem().Bool() {
		t.Fatalf("expected true, got %v", v)
	}

	v = convertPointer(reflect.Int, "10")
	if !v.IsValid() || v.Elem().Int() != 10 {
		t.Fatalf("expected 10, got %v", v)
	}

	v = convertPointer(reflect.String, "abc")
	if !v.IsValid() || v.Elem().String() != "abc" {
		t.Fatalf("expected abc, got %v", v)
	}

	v = convertPointer(reflect.Complex64, "1")
	if v.IsValid() {
		t.Fatalf("expected invalid value for unsupported kind")
	}
}

func BenchmarkParseTag(b *testing.B) {
	for b.Loop() {
		parseTag("field,omitempty,default:value")
	}
}

func BenchmarkIsZero(b *testing.B) {
	type S struct{ A int }
	v := reflect.ValueOf(S{})
	for b.Loop() {
		isZero(v)
	}
}

func BenchmarkConvertPointer(b *testing.B) {
	for b.Loop() {
		convertPointer(reflect.Int, "42")
	}
}

type customZero struct{ A int }

func (c customZero) IsZero() bool { return c.A == 0 }

func TestIsZeroCases(t *testing.T) {
	var sl []int
	if !isZero(reflect.ValueOf(sl)) {
		t.Errorf("nil slice should be zero")
	}
	sl = []int{}
	if !isZero(reflect.ValueOf(sl)) {
		t.Errorf("empty slice should be zero")
	}
	sl = []int{1}
	if isZero(reflect.ValueOf(sl)) {
		t.Errorf("non-empty slice considered zero")
	}

	arr := [2]int{}
	if !isZero(reflect.ValueOf(arr)) {
		t.Errorf("zero array should be zero")
	}
	arr = [2]int{0, 1}
	if isZero(reflect.ValueOf(arr)) {
		t.Errorf("non-zero array considered zero")
	}

	type S struct {
		A int
		B string
	}
	if !isZero(reflect.ValueOf(S{})) {
		t.Errorf("zero struct should be zero")
	}
	if isZero(reflect.ValueOf(S{A: 1})) {
		t.Errorf("non-zero struct considered zero")
	}

	if !isZero(reflect.ValueOf(customZero{})) {
		t.Errorf("IsZero method not used for zero value")
	}
	if isZero(reflect.ValueOf(customZero{A: 1})) {
		t.Errorf("IsZero method not used for non-zero value")
	}
}

func TestIsZeroFuncAndMap(t *testing.T) {
	tests := map[string]func(){
		"nil":     nil,
		"non-nil": func() {},
	}
	for name, fn := range tests {
		t.Run(name, func(t *testing.T) {
			defer func() {
				if r := recover(); r == nil {
					t.Errorf("expected panic for %s func", name)
				}
			}()
			isZero(reflect.ValueOf(fn))
		})
	}

	var m map[string]int
	if !isZero(reflect.ValueOf(m)) {
		t.Errorf("nil map should be zero")
	}
	m = map[string]int{}
	if !isZero(reflect.ValueOf(m)) {
		t.Errorf("empty map should be zero")
	}
	m["a"] = 1
	if isZero(reflect.ValueOf(m)) {
		t.Errorf("non-empty map considered zero")
	}
}