-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessagetype_test.go
More file actions
executable file
·181 lines (166 loc) · 4.93 KB
/
messagetype_test.go
File metadata and controls
executable file
·181 lines (166 loc) · 4.93 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
package fmt
import (
"testing"
)
func TestStringTypeDetection(t *testing.T) {
t.Run("Empty string", func(t *testing.T) {
msg, msgType := Convert("").StringType()
if msgType != Msg.Normal {
t.Errorf("Expected Normal for empty string, got %v", msgType)
}
if msg != "" {
t.Errorf("Expected empty string, got %q", msg)
}
})
t.Run("Error keywords", func(t *testing.T) {
errorKeywords := []string{
"This is an error message",
"Operation failed",
"exit status 1",
"variable undeclared",
"function undefined",
"fatal exception",
}
for _, keyword := range errorKeywords {
msg, msgType := Convert(keyword).StringType()
if msgType != Msg.Error {
t.Errorf("Expected Error for keyword %q, got %v", keyword, msgType)
}
if msg != keyword {
t.Errorf("Expected message to be unchanged, got %q", msg)
}
}
})
t.Run("Success keywords", func(t *testing.T) {
successKeywords := []string{
"Success! Operation completed",
"success",
"Operation completed",
"Build successful",
"Task done",
}
for _, keyword := range successKeywords {
msg, msgType := Convert(keyword).StringType()
if msgType != Msg.Success {
t.Errorf("Expected Success for keyword %q, got %v", keyword, msgType)
}
if msg != keyword {
t.Errorf("Expected message to be unchanged, got %q", msg)
}
}
})
t.Run("Info keywords", func(t *testing.T) {
infoKeywords := []string{
"Info: Starting process",
"... initializing ...",
"starting up",
"initializing system",
}
for _, keyword := range infoKeywords {
_, msgType := Convert(keyword).StringType()
if msgType != Msg.Info {
t.Errorf("Expected Info for keyword %q, got %v", keyword, msgType)
}
}
})
t.Run("Warning keywords", func(t *testing.T) {
warningKeywords := []string{
"Warning: disk space low",
"warn user",
}
for _, keyword := range warningKeywords {
_, msgType := Convert(keyword).StringType()
if msgType != Msg.Warning {
t.Errorf("Expected Warning for keyword %q, got %v", keyword, msgType)
}
}
})
t.Run("Debug keywords", func(t *testing.T) {
debugKeywords := []string{
"debug: something happening",
"[debug] status",
"DEBUG: uppercase",
"DeBuG: Mixed Case",
}
for _, keyword := range debugKeywords {
_, msgType := Convert(keyword).StringType()
if msgType != Msg.Debug {
t.Errorf("Expected Debug for keyword %q, got %v", keyword, msgType)
}
}
})
t.Run("Normal message", func(t *testing.T) {
_, msgType := Convert("Hello world").StringType()
if msgType != Msg.Normal {
t.Errorf("Expected Normal for generic message, got %v", msgType)
}
})
}
func TestSSERelatedTypes(t *testing.T) {
tests := []struct {
name string
msgType MessageType
check func(MessageType) bool
expected string
}{
{"Connect", Msg.Connect, func(t MessageType) bool { return t.IsConnect() }, "Connect"},
{"Auth", Msg.Auth, func(t MessageType) bool { return t.IsAuth() }, "Auth"},
{"Parse", Msg.Parse, func(t MessageType) bool { return t.IsParse() }, "Parse"},
{"Timeout", Msg.Timeout, func(t MessageType) bool { return t.IsTimeout() }, "Timeout"},
{"Broadcast", Msg.Broadcast, func(t MessageType) bool { return t.IsBroadcast() }, "Broadcast"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !tt.check(tt.msgType) {
t.Errorf("Expected check for %s to be true", tt.name)
}
if tt.msgType.String() != tt.expected {
t.Errorf("Expected String() for %s to be %q, got %q", tt.name, tt.expected, tt.msgType.String())
}
})
}
t.Run("IsNetworkError", func(t *testing.T) {
networkTypes := []MessageType{Msg.Connect, Msg.Auth, Msg.Timeout, Msg.Broadcast}
for _, nt := range networkTypes {
if !nt.IsNetworkError() {
t.Errorf("Expected %v to be a network error", nt)
}
}
nonNetworkTypes := []MessageType{Msg.Normal, Msg.Info, Msg.Error, Msg.Warning, Msg.Success, Msg.Parse, Msg.Debug}
for _, nnt := range nonNetworkTypes {
if nnt.IsNetworkError() {
t.Errorf("Expected %v NOT to be a network error", nnt)
}
}
})
}
func TestPubSubTypes(t *testing.T) {
tests := []struct {
name string
msgType MessageType
check func(MessageType) bool
expected string
}{
{"Event", Msg.Event, func(t MessageType) bool { return t.IsEvent() }, "Event"},
{"Request", Msg.Request, func(t MessageType) bool { return t.IsRequest() }, "Request"},
{"Response", Msg.Response, func(t MessageType) bool { return t.IsResponse() }, "Response"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !tt.check(tt.msgType) {
t.Errorf("Expected check for %s to be true", tt.name)
}
if tt.msgType.String() != tt.expected {
t.Errorf("Expected String() for %s to be %q, got %q", tt.name, tt.expected, tt.msgType.String())
}
})
}
}
func TestIsDebug(t *testing.T) {
if !Msg.Debug.IsDebug() {
t.Error("Expected Msg.Debug.IsDebug() to be true")
}
if Msg.Info.IsDebug() {
t.Error("Expected Msg.Info.IsDebug() to be false")
}
}