-
Notifications
You must be signed in to change notification settings - Fork 190
/
container_test.go
119 lines (92 loc) · 2.41 KB
/
container_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
package accessory
import (
"reflect"
"testing"
"github.com/brutella/hc/service"
)
var info = Info{
Name: "Accessory1",
SerialNumber: "001",
Manufacturer: "Google",
Model: "Accessory",
}
func TestContainer(t *testing.T) {
acc1 := New(info, TypeOther)
info.Name = "Accessory2"
acc2 := New(info, TypeOther)
c := NewContainer()
if err := c.AddAccessory(acc1); err != nil {
t.Fatal(err)
}
if err := c.AddAccessory(acc2); err != nil {
t.Fatal(err)
}
if is, want := len(c.Accessories), 2; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
if x := acc1.ID; x == 2 {
t.Fatal(x)
}
if x := acc2.ID; x == 3 {
t.Fatal(x)
}
if acc1.ID == acc2.ID {
t.Fatal("equal ids not allowed")
}
c.RemoveAccessory(acc2)
if is, want := len(c.Accessories), 1; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}
func TestDuplicateAccessoryId(t *testing.T) {
acc1 := New(Info{ID: 1}, TypeOther)
acc2 := New(Info{ID: 1}, TypeOther)
c := NewContainer()
if err := c.AddAccessory(acc1); err != nil {
t.Fatal(err)
}
if err := c.AddAccessory(acc2); err == nil {
t.Fatal("Error expected")
}
}
func TestAccessoryCount(t *testing.T) {
accessory := New(info, TypeOther)
c := NewContainer()
c.AddAccessory(accessory)
if is, want := len(c.Accessories), 1; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
c.RemoveAccessory(accessory)
if is, want := len(c.Accessories), 0; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}
func TestAccessoryType(t *testing.T) {
a1 := New(info, TypeLightbulb)
a2 := New(info, TypeSwitch)
c := NewContainer()
c.AddAccessory(a1)
if is, want := c.AccessoryType(), TypeLightbulb; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
c.AddAccessory(a2)
if is, want := c.AccessoryType(), TypeBridge; is != want {
t.Fatalf("is=%v want=%v", is, want)
}
}
func TestContentHash(t *testing.T) {
acc := New(info, TypeLightbulb)
c := NewContainer()
c.AddAccessory(acc)
hash := c.ContentHash()
acc.Info.Name.SetValue("Test Value")
// Hash ignores the value field and should therefore be the same
if is, want := c.ContentHash(), hash; reflect.DeepEqual(is, want) == false {
t.Fatalf("is=%v want=%v", is, want)
}
acc.AddService(service.New(service.TypeLightbulb))
// Hash changes when accessories/services/characteristics are added
if is, want := c.ContentHash(), hash; reflect.DeepEqual(is, want) == true {
t.Fatalf("%v should not be %v", is, want)
}
}