-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisibility_test.go
More file actions
202 lines (184 loc) · 4.11 KB
/
Copy pathvisibility_test.go
File metadata and controls
202 lines (184 loc) · 4.11 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package pages
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVisibility_String(t *testing.T) {
tests := []struct {
name string
visibility Visibility
expected string
}{
{
name: "Private visibility",
visibility: Private,
expected: "private",
},
{
name: "Public visibility",
visibility: Public,
expected: "public",
},
{
name: "Unknown visibility - invalid value",
visibility: Visibility(99),
expected: "unknown",
},
{
name: "Zero value visibility",
visibility: Visibility(0),
expected: "private",
},
{
name: "Negative visibility",
visibility: Visibility(-1),
expected: "unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.visibility.String()
assert.Equal(t, tt.expected, result, "String() should return the expected value")
})
}
}
func TestVisibilityFromString(t *testing.T) {
tests := []struct {
name string
input string
expected Visibility
}{
{
name: "Public string",
input: "public",
expected: Public,
},
{
name: "Private string - default case",
input: "private",
expected: Private,
},
{
name: "Empty string",
input: "",
expected: Private,
},
{
name: "Invalid string",
input: "invalid",
expected: Private,
},
{
name: "Mixed case public",
input: "Public",
expected: Private,
},
{
name: "Uppercase public",
input: "PUBLIC",
expected: Private,
},
{
name: "Whitespace around public",
input: " public ",
expected: Private,
},
{
name: "Partial match",
input: "pub",
expected: Private,
},
{
name: "Number string",
input: "123",
expected: Private,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := VisibilityFromString(tt.input)
assert.Equal(t, tt.expected, result, "VisibilityFromString() should return the expected visibility")
})
}
}
func TestVisibility_Constants(t *testing.T) {
tests := []struct {
name string
constant Visibility
expected int
stringVal string
}{
{
name: "Private constant",
constant: Private,
expected: 0,
stringVal: "private",
},
{
name: "Public constant",
constant: Public,
expected: 1,
stringVal: "public",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, int(tt.constant), "Constant should have expected integer value")
assert.Equal(t, tt.stringVal, tt.constant.String(), "Constant should have expected string representation")
})
}
}
func TestVisibility_RoundTrip(t *testing.T) {
tests := []struct {
name string
original Visibility
}{
{
name: "Private round trip",
original: Private,
},
{
name: "Public round trip",
original: Public,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Convert to string and back
str := tt.original.String()
result := VisibilityFromString(str)
assert.Equal(t, tt.original, result, "Round trip conversion should preserve the original value")
})
}
}
func TestVisibility_ValuesCoverage(t *testing.T) {
// Ensure we have tested all possible valid visibility values
validVisibilities := []Visibility{Private, Public}
for _, v := range validVisibilities {
str := v.String()
result := VisibilityFromString(str)
assert.Equal(t, v, result, "Visibility %d should round trip correctly", v)
require.NotEqual(t, "unknown", v.String(), "Valid visibility should not return 'unknown'")
}
}
func BenchmarkVisibility_String(b *testing.B) {
visibilities := []Visibility{Private, Public}
for _, v := range visibilities {
b.Run(v.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = v.String()
}
})
}
}
func BenchmarkVisibilityFromString(b *testing.B) {
inputs := []string{"public", "private", "invalid"}
for _, input := range inputs {
b.Run(input, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = VisibilityFromString(input)
}
})
}
}