forked from Gandem/bonjour-reflector
-
Notifications
You must be signed in to change notification settings - Fork 12
/
config_test.go
62 lines (55 loc) · 1.96 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
package main
import (
"os"
"reflect"
"sort"
"testing"
)
var devices = map[macAddress]multicastDevice{
"00:14:22:01:23:45": {Description: "Test Chromecast", OriginPool: 45, SharedPools: []uint16{42, 1042, 46}},
"00:14:22:01:23:46": {Description: "Test Spotify Air", OriginPool: 46, SharedPools: []uint16{176, 148}},
"00:14:22:01:23:47": {Description: "Test Spotify Air", OriginPool: 47, SharedPools: []uint16{1042, 1717, 13}},
}
func TestReadConfig(t *testing.T) {
// Check that a valid config file is read adequately
validTestConfigFile := "config_test.toml"
computedCfg, err := readConfig(validTestConfigFile)
expectedCfg := config{
NetInterface: "test0",
Devices: devices,
}
if err != nil {
t.Errorf("Error in readConfig(): failed to read test config file %s", validTestConfigFile)
} else if !reflect.DeepEqual(expectedCfg, computedCfg) {
t.Error("Error in readConfig(): expected config does not match computed config")
}
// Check that a non-existant config file is handled adequately
nonexistantConfigFile := "nonexistant_test.toml"
computedCfg, err = readConfig(nonexistantConfigFile)
if !reflect.DeepEqual(computedCfg, config{}) {
t.Error("Error in readConfig(): unexpected config returned for non-existant config file")
}
if !os.IsNotExist(err) {
// if the error returned is not of type "file not found"
t.Error("Error in readConfig(): wrong error returned for nonexistant config file")
}
}
func TestMapByPool(t *testing.T) {
computedResult := mapByPool(devices)
// Sort slices to ensure that a different order does not make the test fail
for _, slice := range computedResult {
sort.Slice(slice, func(i, j int) bool { return slice[i] < slice[j] })
}
expectedResult := map[uint16]([]uint16){
42: []uint16{45},
1042: []uint16{45, 47},
46: []uint16{45},
176: []uint16{46},
148: []uint16{46},
13: []uint16{47},
1717: []uint16{47},
}
if !reflect.DeepEqual(computedResult, expectedResult) {
t.Error("Error in mapByPool()")
}
}