2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert // ok here
|
2021-03-12 02:43:17 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-02-13 17:33:46 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-12 02:43:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewConfig(t *testing.T) {
|
|
|
|
// arrange, act
|
|
|
|
ci := NewConfig()
|
|
|
|
// assert
|
|
|
|
c, ok := ci.(*i2cConfig)
|
|
|
|
if !ok {
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "NewConfig() should have returned a *i2cConfig")
|
2021-03-12 02:43:17 +08:00
|
|
|
}
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, BusNotInitialized, c.bus)
|
|
|
|
assert.Equal(t, AddressNotInitialized, c.address)
|
2021-03-12 02:43:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithBus(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
c := NewConfig()
|
|
|
|
// act
|
2022-12-10 20:10:23 +08:00
|
|
|
c.SetBus(0x23)
|
2021-03-12 02:43:17 +08:00
|
|
|
// assert
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 0x23, c.(*i2cConfig).bus)
|
2021-03-12 02:43:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithAddress(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
c := NewConfig()
|
|
|
|
// act
|
2022-12-10 20:10:23 +08:00
|
|
|
c.SetAddress(0x24)
|
2021-03-12 02:43:17 +08:00
|
|
|
// assert
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 0x24, c.(*i2cConfig).address)
|
2021-03-12 02:43:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBusOrDefaultWithBusOption(t *testing.T) {
|
2023-10-21 02:50:42 +08:00
|
|
|
tests := map[string]struct {
|
2021-03-12 02:43:17 +08:00
|
|
|
init int
|
|
|
|
bus int
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
"not_initialized": {init: -1, bus: 0x25, want: 0x25},
|
|
|
|
"initialized": {init: 0x26, bus: 0x27, want: 0x26},
|
|
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
c := NewConfig()
|
|
|
|
// act
|
|
|
|
WithBus(tc.init)(c)
|
|
|
|
got := c.GetBusOrDefault(tc.bus)
|
|
|
|
// assert
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, tc.want, got)
|
2021-03-12 02:43:17 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAddressOrDefaultWithAddressOption(t *testing.T) {
|
2023-10-21 02:50:42 +08:00
|
|
|
tests := map[string]struct {
|
2021-03-12 02:43:17 +08:00
|
|
|
init int
|
|
|
|
address int
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
"not_initialized": {init: -1, address: 0x28, want: 0x28},
|
|
|
|
"initialized": {init: 0x29, address: 0x2A, want: 0x29},
|
|
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
c := NewConfig()
|
|
|
|
// act
|
|
|
|
WithAddress(tc.init)(c)
|
|
|
|
got := c.GetAddressOrDefault(tc.address)
|
|
|
|
// assert
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, tc.want, got)
|
2021-03-12 02:43:17 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|