gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/i2c_config_test.go (about) 1 package i2c 2 3 import ( 4 "testing" 5 6 "gobot.io/x/gobot/v2/gobottest" 7 ) 8 9 func TestNewConfig(t *testing.T) { 10 // arrange, act 11 ci := NewConfig() 12 // assert 13 c, ok := ci.(*i2cConfig) 14 if !ok { 15 t.Errorf("NewConfig() should have returned a *i2cConfig") 16 } 17 gobottest.Assert(t, c.bus, BusNotInitialized) 18 gobottest.Assert(t, c.address, AddressNotInitialized) 19 } 20 21 func TestWithBus(t *testing.T) { 22 // arrange 23 c := NewConfig() 24 // act 25 c.SetBus(0x23) 26 // assert 27 gobottest.Assert(t, c.(*i2cConfig).bus, 0x23) 28 } 29 30 func TestWithAddress(t *testing.T) { 31 // arrange 32 c := NewConfig() 33 // act 34 c.SetAddress(0x24) 35 // assert 36 gobottest.Assert(t, c.(*i2cConfig).address, 0x24) 37 } 38 39 func TestGetBusOrDefaultWithBusOption(t *testing.T) { 40 var tests = map[string]struct { 41 init int 42 bus int 43 want int 44 }{ 45 "not_initialized": {init: -1, bus: 0x25, want: 0x25}, 46 "initialized": {init: 0x26, bus: 0x27, want: 0x26}, 47 } 48 for name, tc := range tests { 49 t.Run(name, func(t *testing.T) { 50 // arrange 51 c := NewConfig() 52 // act 53 WithBus(tc.init)(c) 54 got := c.GetBusOrDefault(tc.bus) 55 // assert 56 gobottest.Assert(t, got, tc.want) 57 }) 58 } 59 } 60 61 func TestGetAddressOrDefaultWithAddressOption(t *testing.T) { 62 var tests = map[string]struct { 63 init int 64 address int 65 want int 66 }{ 67 "not_initialized": {init: -1, address: 0x28, want: 0x28}, 68 "initialized": {init: 0x29, address: 0x2A, want: 0x29}, 69 } 70 for name, tc := range tests { 71 t.Run(name, func(t *testing.T) { 72 // arrange 73 c := NewConfig() 74 // act 75 WithAddress(tc.init)(c) 76 got := c.GetAddressOrDefault(tc.address) 77 // assert 78 gobottest.Assert(t, got, tc.want) 79 }) 80 } 81 }