gobot.io/x/gobot@v1.16.0/drivers/i2c/hmc6352_driver_test.go (about) 1 package i2c 2 3 import ( 4 "errors" 5 "testing" 6 7 "gobot.io/x/gobot" 8 "gobot.io/x/gobot/gobottest" 9 ) 10 11 var _ gobot.Driver = (*HMC6352Driver)(nil) 12 13 // --------- HELPERS 14 func initTestHMC6352Driver() (driver *HMC6352Driver) { 15 driver, _ = initTestHMC6352DriverWithStubbedAdaptor() 16 return 17 } 18 19 func initTestHMC6352DriverWithStubbedAdaptor() (*HMC6352Driver, *i2cTestAdaptor) { 20 adaptor := newI2cTestAdaptor() 21 return NewHMC6352Driver(adaptor), adaptor 22 } 23 24 // --------- TESTS 25 26 func TestNewHMC6352Driver(t *testing.T) { 27 // Does it return a pointer to an instance of HMC6352Driver? 28 var bm interface{} = NewHMC6352Driver(newI2cTestAdaptor()) 29 _, ok := bm.(*HMC6352Driver) 30 if !ok { 31 t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver") 32 } 33 34 b := NewHMC6352Driver(newI2cTestAdaptor()) 35 gobottest.Refute(t, b.Connection(), nil) 36 } 37 38 // Methods 39 func TestHMC6352DriverStart(t *testing.T) { 40 hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor() 41 42 gobottest.Assert(t, hmc.Start(), nil) 43 44 adaptor.i2cWriteImpl = func([]byte) (int, error) { 45 return 0, errors.New("write error") 46 } 47 err := hmc.Start() 48 gobottest.Assert(t, err, errors.New("write error")) 49 } 50 51 func TestHMC6352StartConnectError(t *testing.T) { 52 d, adaptor := initTestHMC6352DriverWithStubbedAdaptor() 53 adaptor.Testi2cConnectErr(true) 54 gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) 55 } 56 57 func TestHMC6352DriverHalt(t *testing.T) { 58 hmc := initTestHMC6352Driver() 59 60 gobottest.Assert(t, hmc.Halt(), nil) 61 } 62 63 func TestHMC6352DriverHeading(t *testing.T) { 64 // when len(data) is 2 65 hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor() 66 67 gobottest.Assert(t, hmc.Start(), nil) 68 69 adaptor.i2cReadImpl = func(b []byte) (int, error) { 70 copy(b, []byte{99, 1}) 71 return 2, nil 72 } 73 74 heading, _ := hmc.Heading() 75 gobottest.Assert(t, heading, uint16(2534)) 76 77 // when len(data) is not 2 78 hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor() 79 80 gobottest.Assert(t, hmc.Start(), nil) 81 82 adaptor.i2cReadImpl = func(b []byte) (int, error) { 83 copy(b, []byte{99}) 84 return 1, nil 85 } 86 87 heading, err := hmc.Heading() 88 gobottest.Assert(t, heading, uint16(0)) 89 gobottest.Assert(t, err, ErrNotEnoughBytes) 90 91 // when read error 92 hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor() 93 94 gobottest.Assert(t, hmc.Start(), nil) 95 96 adaptor.i2cReadImpl = func([]byte) (int, error) { 97 return 0, errors.New("read error") 98 } 99 100 heading, err = hmc.Heading() 101 gobottest.Assert(t, heading, uint16(0)) 102 gobottest.Assert(t, err, errors.New("read error")) 103 104 // when write error 105 hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor() 106 107 gobottest.Assert(t, hmc.Start(), nil) 108 109 adaptor.i2cWriteImpl = func([]byte) (int, error) { 110 return 0, errors.New("write error") 111 } 112 113 heading, err = hmc.Heading() 114 gobottest.Assert(t, heading, uint16(0)) 115 gobottest.Assert(t, err, errors.New("write error")) 116 } 117 118 func TestHMC6352DriverSetName(t *testing.T) { 119 d := initTestHMC6352Driver() 120 d.SetName("TESTME") 121 gobottest.Assert(t, d.Name(), "TESTME") 122 } 123 124 func TestHMC6352DriverOptions(t *testing.T) { 125 d := NewHMC6352Driver(newI2cTestAdaptor(), WithBus(2)) 126 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 127 }