-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathbh1750_driver_test.go
112 lines (93 loc) · 2.94 KB
/
bh1750_driver_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 i2c
import (
"bytes"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
// this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
// and tests all implementations, so no further tests needed here for gobot.Driver interface
var _ gobot.Driver = (*BH1750Driver)(nil)
func initTestBH1750DriverWithStubbedAdaptor() (*BH1750Driver, *i2cTestAdaptor) {
a := newI2cTestAdaptor()
d := NewBH1750Driver(a)
if err := d.Start(); err != nil {
panic(err)
}
return d, a
}
func TestNewBH1750Driver(t *testing.T) {
var di interface{} = NewBH1750Driver(newI2cTestAdaptor())
d, ok := di.(*BH1750Driver)
if !ok {
require.Fail(t, "NewBH1750Driver() should have returned a *BH1750Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BH1750"))
assert.Equal(t, 0x23, d.defaultAddress)
}
func TestBH1750Options(t *testing.T) {
// This is a general test, that options are applied in constructor by using the common WithBus() option and
// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
d := NewBH1750Driver(newI2cTestAdaptor(), WithBus(2))
assert.Equal(t, 2, d.GetBusOrDefault(1))
}
func TestBH1750Start(t *testing.T) {
d := NewBH1750Driver(newI2cTestAdaptor())
require.NoError(t, d.Start())
}
func TestBH1750Halt(t *testing.T) {
d, _ := initTestBH1750DriverWithStubbedAdaptor()
require.NoError(t, d.Halt())
}
func TestBH1750NullLux(t *testing.T) {
d, _ := initTestBH1750DriverWithStubbedAdaptor()
lux, _ := d.Lux()
assert.Equal(t, 0, lux)
}
func TestBH1750Lux(t *testing.T) {
d, a := initTestBH1750DriverWithStubbedAdaptor()
a.i2cReadImpl = func(b []byte) (int, error) {
buf := new(bytes.Buffer)
buf.Write([]byte{0x05, 0xb0})
copy(b, buf.Bytes())
return buf.Len(), nil
}
lux, _ := d.Lux()
assert.Equal(t, 1213, lux)
}
func TestBH1750NullRawSensorData(t *testing.T) {
d, _ := initTestBH1750DriverWithStubbedAdaptor()
level, _ := d.RawSensorData()
assert.Equal(t, 0, level)
}
func TestBH1750RawSensorData(t *testing.T) {
d, a := initTestBH1750DriverWithStubbedAdaptor()
a.i2cReadImpl = func(b []byte) (int, error) {
buf := new(bytes.Buffer)
buf.Write([]byte{0x05, 0xb0})
copy(b, buf.Bytes())
return buf.Len(), nil
}
level, _ := d.RawSensorData()
assert.Equal(t, 1456, level)
}
func TestBH1750LuxError(t *testing.T) {
d, a := initTestBH1750DriverWithStubbedAdaptor()
a.i2cReadImpl = func(b []byte) (int, error) {
return 0, errors.New("wrong number of bytes read")
}
_, err := d.Lux()
require.ErrorContains(t, err, "wrong number of bytes read")
}
func TestBH1750RawSensorDataError(t *testing.T) {
d, a := initTestBH1750DriverWithStubbedAdaptor()
a.i2cReadImpl = func(b []byte) (int, error) {
return 0, errors.New("wrong number of bytes read")
}
_, err := d.RawSensorData()
require.ErrorContains(t, err, "wrong number of bytes read")
}