-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
221 lines (187 loc) · 9.29 KB
/
config_test.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"fmt"
"testing"
"time"
)
func TestReadConfigFile(t *testing.T) {
// Set up test data
var configData Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
// The tests to run
var tests = []struct {
name string
input string
expectedResult Config
}{
{"NoParameterData", "", configData},
{"FileDoesNotExist", "fakeFile", configData},
{"FileExists", CONFIG_FILE, configData},
}
// Write name of function being tested to test results file
LogResult("ReadConfigFile")
// Run the tests
for _, currentTest := range tests {
testname := fmt.Sprintf("%s", currentTest.name)
t.Run(testname, func(t *testing.T) {
result := ReadConfigFile(currentTest.input)
if result != currentTest.expectedResult {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %s Got: %v Expected: %v", currentTest.input, result, currentTest.expectedResult) + " - FAIL")
} else {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %s Got: %v Expected: %v", currentTest.input, result, currentTest.expectedResult) + " - PASS")
}
})
}
}
func TestCheckConfigFile(t *testing.T) {
// Set up test data
var defaultData Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var portChanged Config = Config{8181, DEFAULT_STREAM_ONLY, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var invalidPort Config = Config{-1, DEFAULT_STREAM_ONLY, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var streamOnlyOn Config = Config{DEFAULT_PORT, true, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var hideOnlyOn Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, true, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var streamOnlyHideOnlyBothOn Config = Config{DEFAULT_PORT, true, true, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var wipeAudioOn Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, DEFAULT_HIDE_ONLY, true, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var wipeAudioHideOnlyOn Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, true, true, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var wipeHiddenOn Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, true, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var wipeHiddenStreamOnlyBothOn Config = Config{DEFAULT_PORT, true, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, true, DEFAULT_AUTO_SHUTDOWN, DEFAULT_WIPE_AFTER_HIDE}
var autoShutdownHideOnlybothOn Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, true, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, true}
var wipeAfterHideWipeHiddenBothOn Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, true, DEFAULT_AUTO_SHUTDOWN, true}
var wipeAfterHideHideOnlyBothOn Config = Config{DEFAULT_PORT, DEFAULT_STREAM_ONLY, true, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, true}
var wipeAfterHideStreamOnlyBothOn Config = Config{DEFAULT_PORT, true, DEFAULT_HIDE_ONLY, DEFAULT_WIPE_AUDIO, DEFAULT_WIPE_HIDDEN, DEFAULT_AUTO_SHUTDOWN, true}
// The tests to run
var tests = []struct {
name string
input Config
expectedConfig Config
expectedBoolean bool
}{
{"DefaultData", defaultData, defaultData, true},
{"PortChanged", portChanged, portChanged, true},
{"InvalidPort", invalidPort, defaultData, true},
{"streamOnlyOn", streamOnlyOn, streamOnlyOn, true},
{"hideOnlyOn", hideOnlyOn, hideOnlyOn, true},
{"streamOnlyHideOnlyBothOn", streamOnlyHideOnlyBothOn, streamOnlyHideOnlyBothOn, false},
{"wipeAudioOn", wipeAudioOn, wipeAudioOn, true},
{"wipeAudioHideOnlyOn", wipeAudioHideOnlyOn, wipeAudioHideOnlyOn, false},
{"wipeHiddenOn", wipeHiddenOn, wipeHiddenOn, true},
{"wipeHiddenStreamOnlyBothOn", wipeHiddenStreamOnlyBothOn, wipeHiddenStreamOnlyBothOn, false},
{"autoShutdownHideOnlyBothOn", autoShutdownHideOnlybothOn, autoShutdownHideOnlybothOn, false},
{"wipeAfterHideWipeHiddenBothOn", wipeAfterHideWipeHiddenBothOn, wipeAfterHideWipeHiddenBothOn, false},
{"wipeAfterHideHideOnlyBothOn", wipeAfterHideHideOnlyBothOn, wipeAfterHideHideOnlyBothOn, false},
{"wipeAfterHideStreamOnlyBothOn", wipeAfterHideStreamOnlyBothOn, wipeAfterHideStreamOnlyBothOn, false},
}
// Write name of function being tested to test results file
LogResult("CheckConfigFile")
// Run the tests
for _, currentTest := range tests {
testname := fmt.Sprintf("%s", currentTest.name)
t.Run(testname, func(t *testing.T) {
resultConfig, resultBoolean := CheckConfigFile(currentTest.input)
if resultConfig != currentTest.expectedConfig && resultBoolean != currentTest.expectedBoolean {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %v Got: %v %t Expected: %v %t", currentTest.input, resultConfig, resultBoolean, currentTest.expectedConfig, currentTest.expectedBoolean) + " - FAIL")
} else {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %v Got: %v %t Expected: %v %t", currentTest.input, resultConfig, resultBoolean, currentTest.expectedConfig, currentTest.expectedBoolean) + " - PASS")
}
})
}
}
func TestParseStringToInt(t *testing.T) {
// The tests to run
var tests = []struct {
name string
input string
expectedResult int
}{
{"NoParameterData", "", -1},
{"InvalidData1", "invalidData", -1},
{"InvalidData2", "34e", -1},
{"InvalidData3", "e56", -1},
{"ZeroNumber", "0", -1},
{"MinusNumber", "-4", -1},
{"10", "10", 10},
{"8080", "8080", 8080},
}
// Write name of function being tested to test results file
LogResult("ParseStringToInt")
// Run the tests
for _, currentTest := range tests {
testname := fmt.Sprintf("%s", currentTest.name)
t.Run(testname, func(t *testing.T) {
result := ParseStringToInt(currentTest.input)
if result != currentTest.expectedResult {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %s Got: %d Expected: %d", currentTest.input, result, currentTest.expectedResult) + " - FAIL")
} else {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %s Got: %d Expected: %d", currentTest.input, result, currentTest.expectedResult) + " - PASS")
}
})
}
}
func TestParseStringToBool(t *testing.T) {
// The tests to run
var tests = []struct {
name string
input string
expectedResult bool
}{
{"NoParameterData", "", false},
{"InvalidData1", "invalidData", false},
{"InvalidData2", "34e", false},
{"MinusNumber", "-4", false},
{"ZeroNumber", "0", false},
{"OneNumber", "1", true},
{"LowerCaseTrue", "true", true},
{"UpperCaseTrue", "TRUE", true},
{"LowerCaseFalse", "false", false},
{"UpperCaseFalse", "FALSE", false},
}
// Write name of function being tested to test results file
LogResult("ParseStringToBool")
// Run the tests
for _, currentTest := range tests {
testname := fmt.Sprintf("%s", currentTest.name)
t.Run(testname, func(t *testing.T) {
result := ParseStringToBool(currentTest.input)
if result != currentTest.expectedResult {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %s Got: %t Expected: %t", currentTest.input, result, currentTest.expectedResult) + " - FAIL")
} else {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %s Got: %t Expected: %t", currentTest.input, result, currentTest.expectedResult) + " - PASS")
}
})
}
}
func TestParseStringToDateTime(t *testing.T) {
// The tests to run
var tests = []struct {
name string
input string
expectedResult time.Time
}{
{"NoParameterData", "", time.Time{}},
{"InvalidData1", "invalidData", time.Time{}},
{"InvalidData2", "34e", time.Time{}},
{"ValidDateTime1", "10/04/2024 11:00", time.Date(2024, 04, 10, 11, 00, 00, 00, time.Local)},
{"ValidDateTime2", "01/12/2024 11:00", time.Date(2024, 12, 01, 11, 00, 00, 00, time.Local)},
{"ValidDateTime2", "01/12/2024 23:00", time.Date(2024, 12, 01, 23, 00, 00, 00, time.Local)},
{"InvalidDateTime1", "50/12/2024 11:00", time.Time{}},
{"InvalidDateTime2", "01/50/2024 11:00", time.Time{}},
{"InvalidDateTime3", "01/12/99686 11:00", time.Time{}},
{"InvalidDateTime4", "01/12/2024 25:00", time.Time{}},
{"InvalidDateTime5", "01/12/2024 11:61", time.Time{}},
{"InvalidDateTime6", "01-12-2024 11:00", time.Time{}},
{"InvalidDateTime6", "01/12/2024 11-00", time.Time{}},
}
// Write name of function being tested to test results file
LogResult("ParseStringToDateTime")
// Run the tests
for _, currentTest := range tests {
testname := fmt.Sprintf("%s", currentTest.name)
t.Run(testname, func(t *testing.T) {
result := ParseStringToDateTime(currentTest.input)
if result != currentTest.expectedResult {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %s Got: %s Expected: %s", currentTest.input, result, currentTest.expectedResult) + " - FAIL")
} else {
LogResult(currentTest.name + " - " + fmt.Sprintf("Input: %s Got: %s Expected: %s", currentTest.input, result, currentTest.expectedResult) + " - PASS")
}
})
}
}