gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/mma7660_driver_test.go (about) 1 package i2c 2 3 import ( 4 "bytes" 5 "errors" 6 "strings" 7 "testing" 8 9 "gobot.io/x/gobot/v2" 10 "gobot.io/x/gobot/v2/gobottest" 11 ) 12 13 // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver 14 // and tests all implementations, so no further tests needed here for gobot.Driver interface 15 var _ gobot.Driver = (*MMA7660Driver)(nil) 16 17 func initTestMMA7660DriverWithStubbedAdaptor() (*MMA7660Driver, *i2cTestAdaptor) { 18 a := newI2cTestAdaptor() 19 d := NewMMA7660Driver(a) 20 if err := d.Start(); err != nil { 21 panic(err) 22 } 23 return d, a 24 } 25 26 func TestNewMMA7660Driver(t *testing.T) { 27 var di interface{} = NewMMA7660Driver(newI2cTestAdaptor()) 28 d, ok := di.(*MMA7660Driver) 29 if !ok { 30 t.Errorf("NewMMA7660Driver() should have returned a *MMA7660Driver") 31 } 32 gobottest.Refute(t, d.Driver, nil) 33 gobottest.Assert(t, strings.HasPrefix(d.Name(), "MMA7660"), true) 34 gobottest.Assert(t, d.defaultAddress, 0x4c) 35 } 36 37 func TestMMA7660Options(t *testing.T) { 38 // This is a general test, that options are applied in constructor by using the common WithBus() option and 39 // least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)". 40 d := NewMMA7660Driver(newI2cTestAdaptor(), WithBus(2)) 41 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 42 } 43 44 func TestMMA7660Start(t *testing.T) { 45 d := NewMMA7660Driver(newI2cTestAdaptor()) 46 gobottest.Assert(t, d.Start(), nil) 47 } 48 49 func TestMMA7660Halt(t *testing.T) { 50 d, _ := initTestMMA7660DriverWithStubbedAdaptor() 51 gobottest.Assert(t, d.Halt(), nil) 52 } 53 54 func TestMMA7660Acceleration(t *testing.T) { 55 d, _ := initTestMMA7660DriverWithStubbedAdaptor() 56 x, y, z := d.Acceleration(21.0, 21.0, 21.0) 57 gobottest.Assert(t, x, 1.0) 58 gobottest.Assert(t, y, 1.0) 59 gobottest.Assert(t, z, 1.0) 60 } 61 62 func TestMMA7660NullXYZ(t *testing.T) { 63 d, _ := initTestMMA7660DriverWithStubbedAdaptor() 64 65 x, y, z, _ := d.XYZ() 66 gobottest.Assert(t, x, 0.0) 67 gobottest.Assert(t, y, 0.0) 68 gobottest.Assert(t, z, 0.0) 69 } 70 71 func TestMMA7660XYZ(t *testing.T) { 72 d, a := initTestMMA7660DriverWithStubbedAdaptor() 73 a.i2cReadImpl = func(b []byte) (int, error) { 74 buf := new(bytes.Buffer) 75 buf.Write([]byte{0x11, 0x12, 0x13}) 76 copy(b, buf.Bytes()) 77 return buf.Len(), nil 78 } 79 80 x, y, z, _ := d.XYZ() 81 gobottest.Assert(t, x, 17.0) 82 gobottest.Assert(t, y, 18.0) 83 gobottest.Assert(t, z, 19.0) 84 } 85 86 func TestMMA7660XYZError(t *testing.T) { 87 d, a := initTestMMA7660DriverWithStubbedAdaptor() 88 a.i2cReadImpl = func(b []byte) (int, error) { 89 return 0, errors.New("read error") 90 } 91 92 _, _, _, err := d.XYZ() 93 gobottest.Assert(t, err, errors.New("read error")) 94 } 95 96 func TestMMA7660XYZNotReady(t *testing.T) { 97 d, a := initTestMMA7660DriverWithStubbedAdaptor() 98 a.i2cReadImpl = func(b []byte) (int, error) { 99 buf := new(bytes.Buffer) 100 buf.Write([]byte{0x40, 0x40, 0x40}) 101 copy(b, buf.Bytes()) 102 return buf.Len(), nil 103 } 104 105 _, _, _, err := d.XYZ() 106 gobottest.Assert(t, err, ErrNotReady) 107 }