-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes_test.go
154 lines (142 loc) · 2.84 KB
/
types_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
package fahapi
import (
"github.com/MakotoE/checkerror"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestStringBool_UnmarshalJSON(t *testing.T) {
result := StringBool(false)
assert.NotNil(t, result.UnmarshalJSON(nil))
assert.Nil(t, result.UnmarshalJSON([]byte(`"true"`)))
assert.Equal(t, StringBool(true), result)
}
func TestStringInt_UnmarshalJSON(t *testing.T) {
result := StringInt(0)
assert.NotNil(t, result.UnmarshalJSON([]byte("1")))
assert.Nil(t, result.UnmarshalJSON([]byte(`"2"`)))
assert.Equal(t, StringInt(2), result)
}
func TestParseFAHDuration(t *testing.T) {
tests := []struct {
s string
expected FAHDuration
expectError bool
}{
{
"",
0,
true,
},
{
"1",
0,
true,
},
{
"days",
0,
true,
},
{
"0 day",
0,
false,
},
{
"0 day 0 day",
0,
true,
},
{
"1day",
FAHDuration(time.Hour * 24),
false,
},
{
"2 days",
FAHDuration(time.Hour * 24 * 2),
false,
},
{
"1 sec",
FAHDuration(time.Second),
false,
},
{
"1 day 1 sec",
FAHDuration(time.Hour*24 + time.Second),
false,
},
{
"1.5 days",
FAHDuration(time.Hour * 36),
false,
},
{
"unknowntime",
-1,
false,
},
}
for i, test := range tests {
result, err := ParseFAHDuration(test.s)
assert.Equal(t, test.expected, result, i)
checkerror.Check(t, test.expectError, err, i)
}
}
func TestFAHDuration_String(t *testing.T) {
{
d := FAHDuration(1)
assert.False(t, d.UnknownTime())
assert.NotEqual(t, "unknowntime", d.String())
}
{
d := FAHDuration(-1)
assert.True(t, d.UnknownTime())
assert.Equal(t, "unknowntime", d.String())
}
}
func TestFAHDuration_UnknownTime(t *testing.T) {
duration := FAHDuration(0)
assert.False(t, duration.UnknownTime())
assert.NotEqual(t, "unknowntime", duration.String())
assert.Nil(t, duration.UnmarshalJSON([]byte(`"unknowntime"`)))
assert.True(t, duration.UnknownTime())
assert.Equal(t, "unknowntime", duration.String())
}
func TestFAHTime_Invalid(t *testing.T) {
ti := FAHTime(time.Now())
assert.False(t, ti.Invalid())
assert.NotEqual(t, "<invalid>", ti.String())
assert.Nil(t, ti.UnmarshalJSON([]byte(`"<invalid>"`)))
assert.True(t, ti.Invalid())
assert.Equal(t, "<invalid>", ti.String())
}
func TestInfo_FromSlice(t *testing.T) {
src := [][]interface{}{
{
"FAHClient",
[]interface{}{"Version", "7.6.13"},
},
{
"CBang",
[]interface{}{"Date", "Apr 20 2020"},
},
{
"System",
[]interface{}{"CPU ID", "Intel Management Engine is a backdoor"},
[]interface{}{"CPUs", "1"},
},
{
"libFAH",
[]interface{}{"Date", "Apr 20 2020"},
},
}
info := Info{}
assert.NotNil(t, info.FromSlice(nil))
assert.Nil(t, info.FromSlice(src))
assert.NotEmpty(t, info.FAHClient.Version)
assert.NotEmpty(t, info.System.CPUID)
assert.Equal(t, info.System.CPUs, StringInt(1))
}