Skip to content

Commit

Permalink
tests(gpio,aio): cleanup helper_test (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored Oct 27, 2023
1 parent 1f09353 commit f7f4820
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 201 deletions.
25 changes: 13 additions & 12 deletions drivers/aio/analog_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ func TestAnalogSensorDriver(t *testing.T) {
assert.Equal(t, "42", d.Pin())
assert.Equal(t, 30*time.Second, d.interval)

a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = 100
return
})
}

ret := d.Command("ReadRaw")(nil).(map[string]interface{})
assert.Equal(t, 100, ret["val"].(int))
assert.Nil(t, ret["err"])
Expand All @@ -42,10 +43,10 @@ func TestAnalogSensorDriver(t *testing.T) {
// refresh value on read
a = newAioTestAdaptor()
d = NewAnalogSensorDriver(a, "3")
a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = 150
return
})
}
assert.Equal(t, 0.0, d.Value())
val, err := d.Read()
assert.NoError(t, err)
Expand Down Expand Up @@ -78,9 +79,9 @@ func TestAnalogSensorDriverWithLinearScaler(t *testing.T) {
t.Run(name, func(t *testing.T) {
// arrange
d.SetScaler(AnalogSensorLinearScaler(0, 255, tt.toMin, tt.toMax))
a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
return tt.input, nil
})
}
// act
got, err := d.Read()
// assert
Expand Down Expand Up @@ -108,10 +109,10 @@ func TestAnalogSensorDriverStart(t *testing.T) {
})

// send data
a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = 100
return
})
}

assert.NoError(t, d.Start())

Expand All @@ -128,10 +129,10 @@ func TestAnalogSensorDriverStart(t *testing.T) {
})

// send error
a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
err = errors.New("read error")
return
})
}

select {
case <-sem:
Expand All @@ -148,10 +149,10 @@ func TestAnalogSensorDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = 200
return
})
}

d.halt <- true

Expand Down
7 changes: 3 additions & 4 deletions drivers/aio/grove_drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func TestAnalogDriverHalt(t *testing.T) {

for _, driver := range drivers {
var callCount int32
testAdaptor.TestAdaptorAnalogRead(func() (int, error) {
testAdaptor.analogReadFunc = func() (int, error) {
atomic.AddInt32(&callCount, 1)
return 42, nil
})
}

// Start the driver and allow for multiple digital reads
_ = driver.Start()
Expand Down Expand Up @@ -84,11 +84,10 @@ func TestDriverPublishesError(t *testing.T) {
for _, driver := range drivers {
sem := make(chan struct{}, 1)
// send error
returnErr := func() (val int, err error) {
testAdaptor.analogReadFunc = func() (val int, err error) {
err = errors.New("read error")
return
}
testAdaptor.TestAdaptorAnalogRead(returnErr)

assert.NoError(t, driver.Start())

Expand Down
8 changes: 4 additions & 4 deletions drivers/aio/grove_temperature_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func TestGroveTemperatureSensorDriverScaling(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = tt.input
return
})
}
// act
got, err := d.Read()
// assert
Expand All @@ -58,10 +58,10 @@ func TestGroveTempSensorPublishesTemperatureInCelsius(t *testing.T) {
a := newAioTestAdaptor()
d := NewGroveTemperatureSensorDriver(a, "1")

a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = 585
return
})
}
_ = d.Once(d.Event(Value), func(data interface{}) {
assert.Equal(t, "31.62", fmt.Sprintf("%.2f", data.(float64)))
sem <- true
Expand Down
51 changes: 22 additions & 29 deletions drivers/aio/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,46 @@ package aio
import "sync"

type aioTestAdaptor struct {
name string
port string
mtx sync.Mutex
testAdaptorAnalogRead func() (val int, err error)
testAdaptorAnalogWrite func(val int) (err error)
written []int
name string
port string
mtx sync.Mutex
analogReadFunc func() (val int, err error)
analogWriteFunc func(val int) (err error)
written []int
}

func (t *aioTestAdaptor) TestAdaptorAnalogRead(f func() (val int, err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorAnalogRead = f
}
func newAioTestAdaptor() *aioTestAdaptor {
t := aioTestAdaptor{
name: "aio_test_adaptor",
port: "/dev/null",
analogReadFunc: func() (val int, err error) {
return 99, nil
},
analogWriteFunc: func(val int) (err error) {
return nil
},
}

func (t *aioTestAdaptor) TestAdaptorAnalogWrite(f func(val int) (err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorAnalogWrite = f
return &t
}

// AnalogRead capabilities (interface AnalogReader)
func (t *aioTestAdaptor) AnalogRead(pin string) (val int, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorAnalogRead()
return t.analogReadFunc()
}

// AnalogWrite capabilities (interface AnalogWriter)
func (t *aioTestAdaptor) AnalogWrite(pin string, val int) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.written = append(t.written, val)
return t.testAdaptorAnalogWrite(val)
return t.analogWriteFunc(val)
}

func (t *aioTestAdaptor) Connect() (err error) { return }
func (t *aioTestAdaptor) Finalize() (err error) { return }
func (t *aioTestAdaptor) Name() string { return t.name }
func (t *aioTestAdaptor) SetName(n string) { t.name = n }
func (t *aioTestAdaptor) Port() string { return t.port }

func newAioTestAdaptor() *aioTestAdaptor {
return &aioTestAdaptor{
port: "/dev/null",
testAdaptorAnalogRead: func() (val int, err error) {
return 99, nil
},
testAdaptorAnalogWrite: func(val int) (err error) {
return nil
},
}
}
16 changes: 8 additions & 8 deletions drivers/aio/temperature_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func TestTemperatureSensorDriverNtcScaling(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = tt.input
return
})
}
// act
got, err := d.Read()
// assert
Expand Down Expand Up @@ -76,10 +76,10 @@ func TestTemperatureSensorDriverLinearScaling(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = tt.input
return
})
}
// act
got, err := d.Read()
// assert
Expand All @@ -96,10 +96,10 @@ func TestTempSensorPublishesTemperatureInCelsius(t *testing.T) {
ntc := TemperatureSensorNtcConf{TC0: 25, R0: 10000.0, B: 3975} // Ohm, R25=10k
d.SetNtcScaler(1023, 10000, false, ntc) // Ohm, reference value: 1023, series R: 10k

a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
val = 585
return
})
}
_ = d.Once(d.Event(Value), func(data interface{}) {
assert.Equal(t, "31.62", fmt.Sprintf("%.2f", data.(float64)))
sem <- true
Expand All @@ -121,10 +121,10 @@ func TestTempSensorPublishesError(t *testing.T) {
d := NewTemperatureSensorDriver(a, "1")

// send error
a.TestAdaptorAnalogRead(func() (val int, err error) {
a.analogReadFunc = func() (val int, err error) {
err = errors.New("read error")
return
})
}

assert.NoError(t, d.Start())

Expand Down
24 changes: 12 additions & 12 deletions drivers/gpio/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func(string) (val int, err error) {
a.digitalReadFunc = func(string) (val int, err error) {
val = 1
return
})
}

assert.NoError(t, d.Start())

Expand All @@ -62,10 +62,10 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func(string) (val int, err error) {
a.digitalReadFunc = func(string) (val int, err error) {
val = 0
return
})
}

select {
case <-sem:
Expand All @@ -77,10 +77,10 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func(string) (val int, err error) {
a.digitalReadFunc = func(string) (val int, err error) {
err = errors.New("digital read error")
return
})
}

select {
case <-sem:
Expand All @@ -94,10 +94,10 @@ func TestButtonDriverStart(t *testing.T) {

d.halt <- true

a.TestAdaptorDigitalRead(func(string) (val int, err error) {
a.digitalReadFunc = func(string) (val int, err error) {
val = 1
return
})
}

select {
case <-sem:
Expand All @@ -117,10 +117,10 @@ func TestButtonDriverDefaultState(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func(string) (val int, err error) {
a.digitalReadFunc = func(string) (val int, err error) {
val = 0
return
})
}

assert.NoError(t, d.Start())

Expand All @@ -135,10 +135,10 @@ func TestButtonDriverDefaultState(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func(string) (val int, err error) {
a.digitalReadFunc = func(string) (val int, err error) {
val = 1
return
})
}

select {
case <-sem:
Expand Down
12 changes: 6 additions & 6 deletions drivers/gpio/buzzer_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,29 @@ func TestBuzzerDriverTone(t *testing.T) {
func TestBuzzerDriverOnError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.TestAdaptorDigitalWrite(func(string, byte) (err error) {
a.digitalWriteFunc = func(string, byte) (err error) {
return errors.New("write error")
})
}

assert.ErrorContains(t, d.On(), "write error")
}

func TestBuzzerDriverOffError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.TestAdaptorDigitalWrite(func(string, byte) (err error) {
a.digitalWriteFunc = func(string, byte) (err error) {
return errors.New("write error")
})
}

assert.ErrorContains(t, d.Off(), "write error")
}

func TestBuzzerDriverToneError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.TestAdaptorDigitalWrite(func(string, byte) (err error) {
a.digitalWriteFunc = func(string, byte) (err error) {
return errors.New("write error")
})
}

assert.ErrorContains(t, d.Tone(100, 0.01), "write error")
}
8 changes: 4 additions & 4 deletions drivers/gpio/direct_pin_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ var _ gobot.Driver = (*DirectPinDriver)(nil)

func initTestDirectPinDriver() *DirectPinDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalRead = func(string) (val int, err error) {
a.digitalReadFunc = func(string) (val int, err error) {
val = 1
return
}
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
a.digitalWriteFunc = func(string, byte) (err error) {
return errors.New("write error")
}
a.testAdaptorPwmWrite = func(string, byte) (err error) {
a.pwmWriteFunc = func(string, byte) (err error) {
return errors.New("write error")
}
a.testAdaptorServoWrite = func(string, byte) (err error) {
a.servoWriteFunc = func(string, byte) (err error) {
return errors.New("write error")
}
return NewDirectPinDriver(a, "1")
Expand Down
Loading

0 comments on commit f7f4820

Please sign in to comment.