-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstringmap_test.go
76 lines (68 loc) · 2.02 KB
/
stringmap_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
// KIProtect Go-Helpers - Golang Utility Functions
// Copyright (C) 2019-2024 KIProtect GmbH (HRB 208395B) - Germany
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the 3-Clause BSD License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// license for more details.
//
// You should have received a copy of the 3-Clause BSD License
// along with this program. If not, see <https://opensource.org/licenses/BSD-3-Clause>.
package maps
import (
"testing"
)
func TestEnsureStringKeys(t *testing.T) {
invalidStruct := map[interface{}]interface{}{
"test": map[interface{}]interface{}{
1: "test",
},
}
validStruct := map[interface{}]interface{}{
"test": map[interface{}]interface{}{
"foo": "test",
},
"test2": []interface{}{
map[interface{}]interface{}{
"foo": "bar",
},
},
"test3": map[string]interface{}{
"foo": "bar",
"deep": []interface{}{
map[string]interface{}{
"foo": "bar",
},
},
},
}
if _, ok := EnsureStringKeys(invalidStruct); ok {
t.Error("should not work")
}
if stringObj, ok := EnsureStringKeys(validStruct); !ok {
t.Error("should work")
} else {
stringMap, ok := stringObj.(map[string]interface{})
if !ok {
t.Fatal("not a map")
}
if _, ok := stringMap["test"].(map[string]interface{}); !ok {
t.Error("should be a string map")
}
if deepValue, ok := stringMap["test2"].([]interface{}); !ok {
t.Error("should be a list")
} else if _, ok := deepValue[0].(map[string]interface{}); !ok {
t.Error("should be a string map")
}
if deepValue, ok := stringMap["test3"].(map[string]interface{}); !ok {
t.Error("should be a string map")
} else if veryDeepValue, ok := deepValue["deep"].([]interface{}); !ok {
t.Error("should be a list")
} else if _, ok := veryDeepValue[0].(map[string]interface{}); !ok {
t.Error("should be a string map")
}
}
}