-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
127 lines (106 loc) · 2.98 KB
/
utils_test.go
File metadata and controls
127 lines (106 loc) · 2.98 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
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
package kit
import (
"testing"
"unsafe"
)
// TestEq tests the Eq function for different scenarios
func TestEq(t *testing.T) {
tests := []struct {
s1 string
s2 string
expected bool
}{
// Identical strings
{"hello", "hello", true},
{"world", "world", true},
{"", "", true},
// Different strings
{"hello", "world", false},
{"hello", "helloo", false},
{"world", "word", false},
{"", " ", false},
// Case sensitivity
{"Hello", "hello", false},
{"Case", "case", false},
// Unicode characters
{"こんにちは", "こんにちは", true},
{"こんにちは", "こんにちは ", false},
{"🚀", "🚀", true},
{"🚀", "🚀🚀", false},
// Empty string vs non-empty
{"", "a", false},
{"a", "", false},
// Large strings
{string(make([]byte, 1024)), string(make([]byte, 1024)), true}, // Both are same large strings
// Strings with different encodings
{"hello", "hello\x00", false},
{"hello\x00", "hello", false},
}
for _, test := range tests {
result := Eq(test.s1, test.s2)
if result != test.expected {
t.Errorf("Eq(%q, %q) = %v, want %v", test.s1, test.s2, result, test.expected)
}
}
}
type iface interface {
X()
}
type impl struct{}
func (impl) X() {}
func TestIsNil(t *testing.T) {
t.Parallel()
nonNilPtr := func() any {
x := 42
return &x
}
type T struct{}
tests := []struct {
name string
in any
want bool
}{
// Direct nils
{"nil literal", nil, true},
{"nil interface var", func() any { var i any = nil; return i }(), true}, //nolint:revive // testing nil interface
// Pointers
{"typed nil pointer", (*int)(nil), true},
{"non-nil pointer", nonNilPtr(), false},
// Interface holds nil pointer (edge this implementation handles)
{"iface holds nil pointer", func() any { var p *T = nil; var i any = p; return i }(), true}, //nolint:revive // testing nil interface
{"nested iface holds nil pointer", func() any {
var p *T = nil //nolint:revive // testing nil pointer
var i any = p
var j any = i //nolint:all // testing nil interface
return j
}(), true},
// Non-nil values
{"int zero", 0, false},
{"empty string", "", false},
{"struct value", struct{}{}, false},
{"iface holds non-nil concrete", iface(impl{}), false},
// Other kinds that can be nil
{"nil slice", []int(nil), true},
{"nil map", map[string]int(nil), true},
{"nil chan", (chan int)(nil), true}, //nolint:gocritic // testing nil chan
{"nil func", (func())(nil), true},
{"nil unsafe.Pointer", unsafe.Pointer(nil), true},
{"iface holds nil map", func() any {
var m map[string]int = nil //nolint:revive // testing nil map
return any(m)
}(), true},
{"iface holds nil slice", func() any {
var s []int = nil //nolint:revive // testing nil slice
return any(s)
}(), true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := IsNil(tc.in)
if got != tc.want {
t.Fatalf("IsNil(%T) = %v, want %v (value: %#v)", tc.in, got, tc.want, tc.in)
}
})
}
}