-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.go
More file actions
88 lines (83 loc) · 1.94 KB
/
validate_test.go
File metadata and controls
88 lines (83 loc) · 1.94 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
package main
import (
"strings"
"testing"
)
func TestValidateColor(t *testing.T) {
tests := []struct {
input string
expected string
wantErr bool
}{
{"#d73a4a", "d73a4a", false},
{"d73a4a", "d73a4a", false},
{"#FFFFFF", "FFFFFF", false},
{"ffffff", "ffffff", false},
{"#fff", "", true}, // Too short
{"#gggggg", "", true}, // Invalid hex
{"12345", "", true}, // Too short
{"#1234567", "", true}, // Too long
{"", "", true}, // Empty
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result, err := validateColor(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("validateColor(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
}
if result != tt.expected {
t.Errorf("validateColor(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestValidateLabel(t *testing.T) {
tests := []struct {
name string
label Label
wantErr bool
errMsg string
}{
{
"valid label",
Label{Name: "bug", Color: "#d73a4a", Description: "Something isn't working"},
false,
"",
},
{
"empty name",
Label{Name: "", Color: "#d73a4a", Description: "desc"},
true,
"empty",
},
{
"whitespace name",
Label{Name: " ", Color: "#d73a4a", Description: "desc"},
true,
"empty",
},
{
"invalid color",
Label{Name: "bug", Color: "invalid", Description: "desc"},
true,
"color",
},
{
"description too long",
Label{Name: "bug", Color: "#d73a4a", Description: strings.Repeat("a", 101)},
true,
"too long",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateLabel(tt.label)
if (err != nil) != tt.wantErr {
t.Errorf("validateLabel() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil && tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
t.Errorf("validateLabel() error = %v, should contain %q", err, tt.errMsg)
}
})
}
}