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
|
package admin
import "testing"
// βββ validPort ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
func TestValidPort_Valid(t *testing.T) {
cases := []struct {
input string
fallback string
want string
}{
{"8080", "80", "8080"},
{"443", "80", "443"},
{"1", "80", "1"},
{"65535", "80", "65535"},
{"3000", "8080", "3000"},
}
for _, tc := range cases {
if got := validPort(tc.input, tc.fallback); got != tc.want {
t.Errorf("validPort(%q, %q) = %q, want %q", tc.input, tc.fallback, got, tc.want)
}
}
}
func TestValidPort_Invalid(t *testing.T) {
cases := []struct {
input string
fallback string
}{
{"0", "8080"}, // below range
{"65536", "8080"}, // above range
{"-1", "8080"}, // negative
{"abc", "8080"}, // not a number
{"", "8080"}, // empty
{"99999", "8080"}, // way out of range
{"8080.5", "8080"}, // float
}
for _, tc := range cases {
got := validPort(tc.input, tc.fallback)
if got != tc.fallback {
t.Errorf("validPort(%q, %q) = %q, want fallback %q", tc.input, tc.fallback, got, tc.fallback)
}
}
}
// βββ filterEmpty βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
func TestFilterEmpty_RemovesEmpty(t *testing.T) {
in := []string{"a", "", "b", "", "c"}
out := filterEmpty(in)
if len(out) != 3 {
t.Errorf("expected 3 elements, got %d: %v", len(out), out)
}
for _, s := range out {
if s == "" {
t.Error("filterEmpty should not return empty strings")
}
}
}
func TestFilterEmpty_AllEmpty(t *testing.T) {
out := filterEmpty([]string{"", "", ""})
if len(out) != 0 {
t.Errorf("expected empty slice, got %v", out)
}
}
func TestFilterEmpty_NilInput(t *testing.T) {
out := filterEmpty(nil)
if out != nil && len(out) != 0 {
t.Errorf("expected nil/empty slice for nil input, got %v", out)
}
}
func TestFilterEmpty_PreservesOrder(t *testing.T) {
in := []string{"", "first", "", "second", "third", ""}
out := filterEmpty(in)
want := []string{"first", "second", "third"}
if len(out) != len(want) {
t.Fatalf("length mismatch: got %v, want %v", out, want)
}
for i := range want {
if out[i] != want[i] {
t.Errorf("out[%d] = %q, want %q", i, out[i], want[i])
}
}
}
// βββ parseIntForm βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
func TestParseIntForm(t *testing.T) {
cases := []struct {
input string
want int
}{
{"42", 42},
{"0", 0},
{"-5", -5},
{"", 0},
{"abc", 0},
{"3.14", 0},
}
for _, tc := range cases {
if got := parseIntForm(tc.input); got != tc.want {
t.Errorf("parseIntForm(%q) = %d, want %d", tc.input, got, tc.want)
}
}
}
|