forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
112 lines (107 loc) · 2.65 KB
/
util_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
package proxmox
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_keyExists(t *testing.T) {
tests := []struct {
name string
input []interface{}
key string
output bool
}{
{name: "key empty",
input: []interface{}{
map[string]interface{}{"aaa": "", "bbb": "", "ccc": ""},
map[string]interface{}{"aab": "", "bba": "", "cca": ""},
map[string]interface{}{"aac": "", "bbc": "", "ccb": ""},
},
},
{name: "Key in map",
input: []interface{}{
map[string]interface{}{"aaa": "", "bbb": "", "ccc": ""},
map[string]interface{}{"aab": "", "bba": "", "cca": ""},
map[string]interface{}{"aac": "", "bbc": "", "ccb": ""},
},
key: "bba",
output: true,
},
{name: "Key not in map",
input: []interface{}{
map[string]interface{}{"aaa": "", "bbb": "", "ccc": ""},
map[string]interface{}{"aab": "", "bba": "", "cca": ""},
map[string]interface{}{"aac": "", "bbc": "", "ccb": ""},
},
key: "ddd",
},
{name: "no array",
key: "aaa",
},
{name: "no keys",
input: []interface{}{map[string]interface{}{}},
key: "aaa",
},
}
for _, test := range tests {
t.Run(test.name, func(*testing.T) {
require.Equal(t, test.output, keyExists(test.input, test.key), test.name)
})
}
}
func Test_floatToTrimmedString(t *testing.T) {
type testStruct struct {
number float64
decimals uint8
}
tests := []struct {
name string
input testStruct
output string
}{
{name: "float64",
input: testStruct{number: 1.23456789, decimals: 8},
output: "1.23456789",
},
{name: "float32",
input: testStruct{number: float64(float32(1.23456789)), decimals: 8},
output: "1.23456788",
},
{name: "no decimal trimmed",
input: testStruct{number: 1.0000000, decimals: 10},
output: "1",
},
{name: "one decimal trimmed",
input: testStruct{number: 10.3000000, decimals: 10},
output: "10.3",
},
{name: "tree decimal trimmed",
input: testStruct{number: 45.73300000, decimals: 10},
output: "45.733",
},
}
for _, test := range tests {
t.Run(test.name, func(*testing.T) {
require.Equal(t, test.output, floatToTrimmedString(test.input.number, test.input.decimals), test.name)
})
}
}
func Test_splitStringOfSettings(t *testing.T) {
testData := []struct {
Input string
Output map[string]interface{}
}{
{
Input: "setting=a,thing=b,randomString,doubleTest=value=equals,object=test",
Output: map[string]interface{}{
"setting": "a",
"thing": "b",
"randomString": "",
"doubleTest": "value=equals",
"object": "test",
},
},
}
for _, e := range testData {
require.Equal(t, e.Output, splitStringOfSettings(e.Input))
}
}