forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp280_driver_test.go
158 lines (133 loc) · 4.59 KB
/
bmp280_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
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
155
156
157
158
package i2c
import (
"bytes"
"errors"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*BMP280Driver)(nil)
// --------- HELPERS
func initTestBMP280Driver() (driver *BMP280Driver) {
driver, _ = initTestBMP280DriverWithStubbedAdaptor()
return
}
func initTestBMP280DriverWithStubbedAdaptor() (*BMP280Driver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewBMP280Driver(adaptor), adaptor
}
// --------- TESTS
func TestNewBMP280Driver(t *testing.T) {
// Does it return a pointer to an instance of BME280Driver?
var bmp280 interface{} = NewBMP280Driver(newI2cTestAdaptor())
_, ok := bmp280.(*BMP280Driver)
if !ok {
t.Errorf("NewBMP280Driver() should have returned a *BMP280Driver")
}
}
func TestBMP280Driver(t *testing.T) {
bmp280 := initTestBMP280Driver()
gobottest.Refute(t, bmp280.Connection(), nil)
}
func TestBMP280DriverStart(t *testing.T) {
bmp280, _ := initTestBMP280DriverWithStubbedAdaptor()
gobottest.Assert(t, bmp280.Start(), nil)
}
func TestBMP280StartConnectError(t *testing.T) {
d, adaptor := initTestBMP280DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestBMP280DriverStartWriteError(t *testing.T) {
bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
gobottest.Assert(t, bmp280.Start(), errors.New("write error"))
}
func TestBMP280DriverStartReadError(t *testing.T) {
bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor()
adaptor.i2cReadImpl = func(b []byte) (int, error) {
return 0, errors.New("read error")
}
gobottest.Assert(t, bmp280.Start(), errors.New("read error"))
}
func TestBMP280DriverHalt(t *testing.T) {
bmp280 := initTestBMP280Driver()
gobottest.Assert(t, bmp280.Halt(), nil)
}
func TestBMP280DriverMeasurements(t *testing.T) {
bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor()
adaptor.i2cReadImpl = func(b []byte) (int, error) {
buf := new(bytes.Buffer)
// Values produced by dumping data from actual sensor
if adaptor.written[len(adaptor.written)-1] == bmp280RegisterCalib00 {
buf.Write([]byte{126, 109, 214, 102, 50, 0, 54, 149, 220, 213, 208, 11, 64, 30, 166, 255, 249, 255, 172, 38, 10, 216, 189, 16})
} else if adaptor.written[len(adaptor.written)-1] == bmp280RegisterTempData {
buf.Write([]byte{128, 243, 0})
} else if adaptor.written[len(adaptor.written)-1] == bmp280RegisterPressureData {
buf.Write([]byte{77, 23, 48})
}
copy(b, buf.Bytes())
return buf.Len(), nil
}
bmp280.Start()
temp, err := bmp280.Temperature()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, temp, float32(25.014637))
pressure, err := bmp280.Pressure()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, pressure, float32(99545.414))
alt, err := bmp280.Altitude()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, alt, float32(149.22713))
}
func TestBMP280DriverTemperatureWriteError(t *testing.T) {
bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor()
bmp280.Start()
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
temp, err := bmp280.Temperature()
gobottest.Assert(t, err, errors.New("write error"))
gobottest.Assert(t, temp, float32(0.0))
}
func TestBMP280DriverTemperatureReadError(t *testing.T) {
bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor()
bmp280.Start()
adaptor.i2cReadImpl = func([]byte) (int, error) {
return 0, errors.New("read error")
}
temp, err := bmp280.Temperature()
gobottest.Assert(t, err, errors.New("read error"))
gobottest.Assert(t, temp, float32(0.0))
}
func TestBMP280DriverPressureWriteError(t *testing.T) {
bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor()
bmp280.Start()
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
press, err := bmp280.Pressure()
gobottest.Assert(t, err, errors.New("write error"))
gobottest.Assert(t, press, float32(0.0))
}
func TestBMP280DriverPressureReadError(t *testing.T) {
bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor()
bmp280.Start()
adaptor.i2cReadImpl = func([]byte) (int, error) {
return 0, errors.New("read error")
}
press, err := bmp280.Pressure()
gobottest.Assert(t, err, errors.New("read error"))
gobottest.Assert(t, press, float32(0.0))
}
func TestBMP280DriverSetName(t *testing.T) {
b := initTestBMP280Driver()
b.SetName("TESTME")
gobottest.Assert(t, b.Name(), "TESTME")
}
func TestBMP280DriverOptions(t *testing.T) {
b := NewBMP280Driver(newI2cTestAdaptor(), WithBus(2))
gobottest.Assert(t, b.GetBusOrDefault(1), 2)
}