-
Notifications
You must be signed in to change notification settings - Fork 6
/
version_test.go
121 lines (106 loc) · 2.45 KB
/
version_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
package lifxlan_test
import (
"context"
"reflect"
"testing"
"time"
"go.yhsif.com/lifxlan"
"go.yhsif.com/lifxlan/mock"
)
func mockProductMap(t *testing.T) {
t.Helper()
backupProductMap := lifxlan.ProductMap
t.Cleanup(func() {
lifxlan.ProductMap = backupProductMap
})
lifxlan.ProductMap = map[uint64]lifxlan.Product{
lifxlan.ProductMapKey(1, 1): {
ProductName: "Foo",
Features: lifxlan.Features{
Color: lifxlan.OptionalBoolPtr(true),
},
},
}
}
func TestVersion(t *testing.T) {
mockProductMap(t)
t.Run(
"Found",
func(t *testing.T) {
raw := &lifxlan.HardwareVersion{
VendorID: 1,
ProductID: 1,
HardwareVersion: 1,
}
expectedParsed := lifxlan.Product{
ProductName: "Foo",
Features: lifxlan.Features{
Color: lifxlan.OptionalBoolPtr(true),
},
}
parsed := raw.Parse()
if !reflect.DeepEqual(*parsed, expectedParsed) {
t.Errorf("Parse expected %+v, got %+v", expectedParsed, parsed)
}
expectedStr := "Foo(1, 1, 1)"
s := raw.String()
if s != expectedStr {
t.Errorf("String expected %q, got %q", expectedStr, s)
}
},
)
t.Run(
"NotFound",
func(t *testing.T) {
raw := &lifxlan.HardwareVersion{
VendorID: 1,
ProductID: 2,
HardwareVersion: 1,
}
parsed := raw.Parse()
if parsed != nil {
t.Errorf("Parse expected nil, got %+v", parsed)
}
expectedStr := "(1, 2, 1)"
s := raw.String()
if s != expectedStr {
t.Errorf("String expected %q, got %q", expectedStr, s)
}
},
)
}
func TestEmptyHardwareVersion(t *testing.T) {
var version lifxlan.HardwareVersion
s := version.String()
if s != lifxlan.EmptyHardwareVersion {
t.Errorf("Expected %q, got %q", lifxlan.EmptyHardwareVersion, s)
}
}
func TestGetHardwareVersion(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
const timeout = time.Millisecond * 200
mockProductMap(t)
expected := lifxlan.HardwareVersion{
VendorID: 1,
ProductID: 1,
HardwareVersion: 1,
}
service, device := mock.StartService(t)
service.RawStateVersionPayload = &lifxlan.RawStateVersionPayload{
Version: expected,
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := device.GetHardwareVersion(ctx, nil); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(*device.HardwareVersion(), expected) {
t.Errorf(
"HardwareVersion expected %v, got %v",
expected,
device.HardwareVersion(),
)
}
}