Skip to content

Commit 708b6c4

Browse files
committed
chore: add test coverage for lib/findmy
Signed-off-by: deadprogram <[email protected]>
1 parent 9d0ef23 commit 708b6c4

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

lib/findmy/data_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package findmy
2+
3+
import (
4+
"testing"
5+
6+
"tinygo.org/x/bluetooth"
7+
)
8+
9+
func TestNewData(t *testing.T) {
10+
key := []byte{0xce, 0x8b, 0xad, 0x5f, 0x8a, 0x02, 0x71, 0x53, 0x8f, 0xf5, 0xaf, 0xda, 0x87, 0x49, 0x8c, 0xb0, 0x67, 0xe9, 0xa0, 0x20, 0xd6, 0xe4, 0x16, 0x78, 0x01, 0xd5, 0x5d, 0x83}
11+
data := NewData(key)
12+
if data.Data[2] != StatusBatteryFull {
13+
t.Errorf("expected 0x%02x, got 0x%02x", StatusBatteryFull, data.Data[2])
14+
}
15+
if data.Data[3] != 0x71 {
16+
t.Errorf("expected 0x71, got 0x%02x", data.Data[3])
17+
}
18+
if data.Data[4] != 0x53 {
19+
t.Errorf("expected 0x53, got 0x%02x", data.Data[4])
20+
}
21+
}
22+
23+
func TestParseData(t *testing.T) {
24+
address := bluetooth.Address{bluetooth.MACAddress{MAC: bluetooth.MAC{0x02, 0x8a, 0x5f, 0xad, 0x8b, 0xce}}}
25+
startingkey := []byte{0xce, 0x8b, 0xad, 0x5f, 0x8a, 0x02, 0x71, 0x53, 0x8f, 0xf5, 0xaf, 0xda, 0x87, 0x49, 0x8c, 0xb0, 0x67, 0xe9, 0xa0, 0x20, 0xd6, 0xe4, 0x16, 0x78, 0x01, 0xd5, 0x5d, 0x83}
26+
data := NewData(startingkey)
27+
status, key, err := ParseData(address, data.Data)
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
if status != StatusBatteryFull {
32+
t.Errorf("expected 0x%02x, got 0x%02x", StatusBatteryFull, status)
33+
}
34+
if !bytesEqual(key, startingkey) {
35+
t.Errorf("expected %v, got %v", startingkey, key)
36+
}
37+
}
38+
39+
func TestBatteryStatus(t *testing.T) {
40+
tests := []struct {
41+
status byte
42+
want string
43+
}{
44+
{StatusBatteryFull, "full"},
45+
{StatusBatteryMedium, "medium"},
46+
{StatusBatteryLow, "low"},
47+
{0xff, "unknown"},
48+
}
49+
for _, test := range tests {
50+
got := BatteryStatus(test.status)
51+
if got != test.want {
52+
t.Errorf("BatteryStatus(%d) = %q, want %q", test.status, got, test.want)
53+
}
54+
}
55+
}
56+
57+
func bytesEqual(a, b []byte) bool {
58+
if len(a) != len(b) {
59+
return false
60+
}
61+
for i, av := range a {
62+
if av != b[i] {
63+
return false
64+
}
65+
}
66+
return true
67+
}

0 commit comments

Comments
 (0)