1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

gpio: reduce test side effects

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-02 17:15:45 +02:00
parent d405b7a5c7
commit b6c8138551
11 changed files with 138 additions and 123 deletions

View File

@ -36,7 +36,8 @@ func TestButtonDriver(t *testing.T) {
func TestButtonDriverStart(t *testing.T) {
sem := make(chan bool, 0)
d := initTestButtonDriver()
a := newGpioTestAdaptor()
d := NewButtonDriver(a, "1")
gobottest.Assert(t, d.Start(), nil)
d.Once(ButtonPush, func(data interface{}) {
@ -44,7 +45,7 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}
@ -60,7 +61,7 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 0
return
}
@ -71,7 +72,7 @@ func TestButtonDriverStart(t *testing.T) {
t.Errorf("Button Event \"Release\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
err = errors.New("digital read error")
return
}
@ -86,7 +87,7 @@ func TestButtonDriverStart(t *testing.T) {
t.Errorf("Button Event \"Error\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}

View File

@ -11,12 +11,6 @@ import (
var _ gobot.Driver = (*BuzzerDriver)(nil)
func initTestBuzzerDriver(conn DigitalWriter) *BuzzerDriver {
testAdaptorDigitalWrite = func() (err error) {
return nil
}
testAdaptorPwmWrite = func() (err error) {
return nil
}
return NewBuzzerDriver(conn, "1")
}

View File

@ -11,28 +11,29 @@ import (
var _ gobot.Driver = (*DirectPinDriver)(nil)
func initTestDirectPinDriver(conn gobot.Connection) *DirectPinDriver {
testAdaptorDigitalRead = func() (val int, err error) {
func initTestDirectPinDriver() *DirectPinDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}
testAdaptorDigitalWrite = func() (err error) {
a.testAdaptorDigitalWrite = func() (err error) {
return errors.New("write error")
}
testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func() (err error) {
return errors.New("write error")
}
testAdaptorServoWrite = func() (err error) {
a.testAdaptorServoWrite = func() (err error) {
return errors.New("write error")
}
return NewDirectPinDriver(conn, "1")
return NewDirectPinDriver(a, "1")
}
func TestDirectPinDriver(t *testing.T) {
var ret map[string]interface{}
var err interface{}
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
@ -52,72 +53,74 @@ func TestDirectPinDriver(t *testing.T) {
}
func TestDirectPinDriverStart(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Start(), nil)
}
func TestDirectPinDriverHalt(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Halt(), nil)
}
func TestDirectPinDriverOff(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Refute(t, d.DigitalWrite(0), nil)
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
gobottest.Assert(t, d.DigitalWrite(0), ErrDigitalWriteUnsupported)
a := newGpioTestAdaptor()
d = NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.DigitalWrite(0), nil)
}
func TestDirectPinDriverOn(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Refute(t, d.DigitalWrite(1), nil)
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
gobottest.Assert(t, d.DigitalWrite(1), ErrDigitalWriteUnsupported)
a := newGpioTestAdaptor()
d = NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.DigitalWrite(1), nil)
}
func TestDirectPinDriverDigitalWrite(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Refute(t, d.DigitalWrite(1), nil)
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
gobottest.Assert(t, d.DigitalWrite(1), ErrDigitalWriteUnsupported)
a := newGpioTestAdaptor()
d = NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.DigitalWrite(1), nil)
}
func TestDirectPinDriverDigitalRead(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
ret, err := d.DigitalRead()
gobottest.Assert(t, ret, 1)
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
ret, err = d.DigitalRead()
gobottest.Assert(t, err, ErrDigitalReadUnsupported)
gobottest.Assert(t, err, nil)
}
func TestDirectPinDriverPwmWrite(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Refute(t, d.PwmWrite(1), nil)
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
gobottest.Assert(t, d.PwmWrite(1), ErrPwmWriteUnsupported)
a := newGpioTestAdaptor()
d = NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.PwmWrite(1), nil)
}
func TestDirectPinDriverServoWrite(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Refute(t, d.ServoWrite(1), nil)
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
gobottest.Assert(t, d.ServoWrite(1), ErrServoWriteUnsupported)
a := newGpioTestAdaptor()
d = NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.ServoWrite(1), nil)
}
func TestDirectPinDriverDefaultName(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Direct"), true)
}
func TestDirectPinDriverSetName(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor())
d := initTestDirectPinDriver()
d.SetName("mybot")
gobottest.Assert(t, d.Name(), "mybot")
}

View File

@ -53,7 +53,7 @@ func TestDigitalDriverHalt(t *testing.T) {
for _, driver := range drivers {
var callCount int32
testAdaptorDigitalRead = func() (int, error) {
testAdaptor.testAdaptorDigitalRead = func() (int, error) {
atomic.AddInt32(&callCount, 1)
return 42, nil
}
@ -89,7 +89,7 @@ func TestDriverPublishesError(t *testing.T) {
err = errors.New("read error")
return
}
testAdaptorDigitalRead = returnErr
testAdaptor.testAdaptorDigitalRead = returnErr
gobottest.Assert(t, driver.Start(), nil)

View File

@ -14,40 +14,29 @@ type gpioTestDigitalWriter struct {
func (t *gpioTestDigitalWriter) DigitalWrite(string, byte) (err error) { return }
type gpioTestAdaptor struct {
name string
port string
}
var testAdaptorDigitalWrite = func() (err error) {
return nil
}
var testAdaptorServoWrite = func() (err error) {
return nil
}
var testAdaptorPwmWrite = func() (err error) {
return nil
}
var testAdaptorAnalogRead = func() (val int, err error) {
return 99, nil
}
var testAdaptorDigitalRead = func() (val int, err error) {
return 1, nil
name string
port string
testAdaptorDigitalWrite func() (err error)
testAdaptorServoWrite func() (err error)
testAdaptorPwmWrite func() (err error)
testAdaptorAnalogRead func() (val int, err error)
testAdaptorDigitalRead func() (val int, err error)
}
func (t *gpioTestAdaptor) DigitalWrite(string, byte) (err error) {
return testAdaptorDigitalWrite()
return t.testAdaptorDigitalWrite()
}
func (t *gpioTestAdaptor) ServoWrite(string, byte) (err error) {
return testAdaptorServoWrite()
return t.testAdaptorServoWrite()
}
func (t *gpioTestAdaptor) PwmWrite(string, byte) (err error) {
return testAdaptorPwmWrite()
return t.testAdaptorPwmWrite()
}
func (t *gpioTestAdaptor) AnalogRead(string) (val int, err error) {
return testAdaptorAnalogRead()
return t.testAdaptorAnalogRead()
}
func (t *gpioTestAdaptor) DigitalRead(string) (val int, err error) {
return testAdaptorDigitalRead()
return t.testAdaptorDigitalRead()
}
func (t *gpioTestAdaptor) Connect() (err error) { return }
func (t *gpioTestAdaptor) Finalize() (err error) { return }
@ -58,5 +47,20 @@ func (t *gpioTestAdaptor) Port() string { return t.port }
func newGpioTestAdaptor() *gpioTestAdaptor {
return &gpioTestAdaptor{
port: "/dev/null",
testAdaptorDigitalWrite: func() (err error) {
return nil
},
testAdaptorServoWrite: func() (err error) {
return nil
},
testAdaptorPwmWrite: func() (err error) {
return nil
},
testAdaptorAnalogRead: func() (val int, err error) {
return 99, nil
},
testAdaptorDigitalRead: func() (val int, err error) {
return 1, nil
},
}
}

View File

@ -11,28 +11,29 @@ import (
var _ gobot.Driver = (*LedDriver)(nil)
func initTestLedDriver(conn DigitalWriter) *LedDriver {
testAdaptorDigitalWrite = func() (err error) {
func initTestLedDriver() *LedDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalWrite = func() (err error) {
return nil
}
testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func() (err error) {
return nil
}
return NewLedDriver(conn, "1")
return NewLedDriver(a, "1")
}
func TestLedDriver(t *testing.T) {
var err interface{}
d := initTestLedDriver(newGpioTestAdaptor())
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
testAdaptorDigitalWrite = func() (err error) {
a.testAdaptorDigitalWrite = func() (err error) {
return errors.New("write error")
}
testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func() (err error) {
return errors.New("pwm error")
}
@ -51,17 +52,17 @@ func TestLedDriver(t *testing.T) {
}
func TestLedDriverStart(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor())
d := initTestLedDriver()
gobottest.Assert(t, d.Start(), nil)
}
func TestLedDriverHalt(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor())
d := initTestLedDriver()
gobottest.Assert(t, d.Halt(), nil)
}
func TestLedDriverToggle(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor())
d := initTestLedDriver()
d.Off()
d.Toggle()
gobottest.Assert(t, d.State(), true)
@ -70,11 +71,12 @@ func TestLedDriverToggle(t *testing.T) {
}
func TestLedDriverBrightness(t *testing.T) {
d := initTestLedDriver(&gpioTestDigitalWriter{})
gobottest.Assert(t, d.Brightness(150), ErrPwmWriteUnsupported)
// d := initTestLedDriver(&gpioTestDigitalWriter{})
// gobottest.Assert(t, d.Brightness(150), ErrPwmWriteUnsupported)
d = initTestLedDriver(newGpioTestAdaptor())
testAdaptorPwmWrite = func() (err error) {
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
a.testAdaptorPwmWrite = func() (err error) {
err = errors.New("pwm error")
return
}
@ -82,12 +84,14 @@ func TestLedDriverBrightness(t *testing.T) {
}
func TestLEDDriverDefaultName(t *testing.T) {
d := initTestLedDriver(&gpioTestDigitalWriter{})
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
gobottest.Assert(t, strings.HasPrefix(d.Name(), "LED"), true)
}
func TestLEDDriverSetName(t *testing.T) {
d := initTestLedDriver(&gpioTestDigitalWriter{})
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
d.SetName("mybot")
gobottest.Assert(t, d.Name(), "mybot")
}

View File

@ -44,10 +44,12 @@ func TestMakeyButtonDriver(t *testing.T) {
func TestMakeyButtonDriverStart(t *testing.T) {
sem := make(chan bool)
d := initTestMakeyButtonDriver()
a := newGpioTestAdaptor()
d := NewMakeyButtonDriver(a, "1")
gobottest.Assert(t, d.Start(), nil)
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 0
return
}
@ -63,7 +65,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
t.Errorf("MakeyButton Event \"Push\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}
@ -79,7 +81,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
t.Errorf("MakeyButton Event \"Release\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
err = errors.New("digital read error")
return
}
@ -100,7 +102,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
sem <- true
})
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}

View File

@ -36,7 +36,9 @@ func TestPIRMotionDriver(t *testing.T) {
func TestPIRMotionDriverStart(t *testing.T) {
sem := make(chan bool, 0)
d := initTestPIRMotionDriver()
a := newGpioTestAdaptor()
d := NewPIRMotionDriver(a, "1")
gobottest.Assert(t, d.Start(), nil)
d.Once(MotionDetected, func(data interface{}) {
@ -44,7 +46,7 @@ func TestPIRMotionDriverStart(t *testing.T) {
sem <- true
})
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}
@ -60,7 +62,7 @@ func TestPIRMotionDriverStart(t *testing.T) {
sem <- true
})
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
val = 0
return
}
@ -71,7 +73,7 @@ func TestPIRMotionDriverStart(t *testing.T) {
t.Errorf("PIRMotionDriver Event \"MotionStopped\" was not published")
}
testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func() (val int, err error) {
err = errors.New("digital read error")
return
}

View File

@ -10,40 +10,41 @@ import (
var _ gobot.Driver = (*RelayDriver)(nil)
func initTestRelayDriver(conn DigitalWriter) *RelayDriver {
testAdaptorDigitalWrite = func() (err error) {
func initTestRelayDriver() *RelayDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalWrite = func() (err error) {
return nil
}
testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func() (err error) {
return nil
}
return NewRelayDriver(conn, "1")
return NewRelayDriver(a, "1")
}
func TestRelayDriverDefaultName(t *testing.T) {
g := initTestRelayDriver(newGpioTestAdaptor())
g := initTestRelayDriver()
gobottest.Refute(t, g.Connection(), nil)
gobottest.Assert(t, strings.HasPrefix(g.Name(), "Relay"), true)
}
func TestRelayDriverSetName(t *testing.T) {
g := initTestRelayDriver(newGpioTestAdaptor())
g := initTestRelayDriver()
g.SetName("mybot")
gobottest.Assert(t, g.Name(), "mybot")
}
func TestRelayDriverStart(t *testing.T) {
d := initTestRelayDriver(newGpioTestAdaptor())
d := initTestRelayDriver()
gobottest.Assert(t, d.Start(), nil)
}
func TestRelayDriverHalt(t *testing.T) {
d := initTestRelayDriver(newGpioTestAdaptor())
d := initTestRelayDriver()
gobottest.Assert(t, d.Halt(), nil)
}
func TestRelayDriverToggle(t *testing.T) {
d := initTestRelayDriver(newGpioTestAdaptor())
d := initTestRelayDriver()
d.Off()
d.Toggle()
gobottest.Assert(t, d.State(), true)
@ -52,7 +53,7 @@ func TestRelayDriverToggle(t *testing.T) {
}
func TestRelayDriverCommands(t *testing.T) {
d := initTestRelayDriver(newGpioTestAdaptor())
d := initTestRelayDriver()
gobottest.Assert(t, d.Command("Off")(nil), nil)
gobottest.Assert(t, d.State(), false)

View File

@ -11,20 +11,22 @@ import (
var _ gobot.Driver = (*RgbLedDriver)(nil)
func initTestRgbLedDriver(conn DigitalWriter) *RgbLedDriver {
testAdaptorDigitalWrite = func() (err error) {
func initTestRgbLedDriver() *RgbLedDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalWrite = func() (err error) {
return nil
}
testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func() (err error) {
return nil
}
return NewRgbLedDriver(conn, "1", "2", "3")
return NewRgbLedDriver(a, "1", "2", "3")
}
func TestRgbLedDriver(t *testing.T) {
var err interface{}
d := initTestRgbLedDriver(newGpioTestAdaptor())
a := newGpioTestAdaptor()
d := NewRgbLedDriver(a, "1", "2", "3")
gobottest.Assert(t, d.Pin(), "r=1, g=2, b=3")
gobottest.Assert(t, d.RedPin(), "1")
@ -32,10 +34,10 @@ func TestRgbLedDriver(t *testing.T) {
gobottest.Assert(t, d.BluePin(), "3")
gobottest.Refute(t, d.Connection(), nil)
testAdaptorDigitalWrite = func() (err error) {
a.testAdaptorDigitalWrite = func() (err error) {
return errors.New("write error")
}
testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func() (err error) {
return errors.New("pwm error")
}
@ -50,21 +52,20 @@ func TestRgbLedDriver(t *testing.T) {
err = d.Command("SetRGB")(map[string]interface{}{"r": 0xff, "g": 0xff, "b": 0xff})
gobottest.Assert(t, err.(error), errors.New("pwm error"))
}
func TestRgbLedDriverStart(t *testing.T) {
d := initTestRgbLedDriver(newGpioTestAdaptor())
d := initTestRgbLedDriver()
gobottest.Assert(t, d.Start(), nil)
}
func TestRgbLedDriverHalt(t *testing.T) {
d := initTestRgbLedDriver(newGpioTestAdaptor())
d := initTestRgbLedDriver()
gobottest.Assert(t, d.Halt(), nil)
}
func TestRgbLedDriverToggle(t *testing.T) {
d := initTestRgbLedDriver(newGpioTestAdaptor())
d := initTestRgbLedDriver()
d.Off()
d.Toggle()
gobottest.Assert(t, d.State(), true)
@ -73,11 +74,12 @@ func TestRgbLedDriverToggle(t *testing.T) {
}
func TestRgbLedDriverSetLevel(t *testing.T) {
d := initTestRgbLedDriver(&gpioTestDigitalWriter{})
gobottest.Assert(t, d.SetLevel("1", 150), ErrPwmWriteUnsupported)
a := newGpioTestAdaptor()
d := NewRgbLedDriver(a, "1", "2", "3")
gobottest.Assert(t, d.SetLevel("1", 150), nil)
d = initTestRgbLedDriver(newGpioTestAdaptor())
testAdaptorPwmWrite = func() (err error) {
d = NewRgbLedDriver(a, "1", "2", "3")
a.testAdaptorPwmWrite = func() (err error) {
err = errors.New("pwm error")
return
}
@ -85,12 +87,14 @@ func TestRgbLedDriverSetLevel(t *testing.T) {
}
func TestRgbLedDriverDefaultName(t *testing.T) {
d := initTestRgbLedDriver(&gpioTestDigitalWriter{})
a := newGpioTestAdaptor()
d := NewRgbLedDriver(a, "1", "2", "3")
gobottest.Assert(t, strings.HasPrefix(d.Name(), "RGB"), true)
}
func TestRgbLedDriverSetName(t *testing.T) {
d := initTestRgbLedDriver(&gpioTestDigitalWriter{})
a := newGpioTestAdaptor()
d := NewRgbLedDriver(a, "1", "2", "3")
d.SetName("mybot")
gobottest.Assert(t, d.Name(), "mybot")
}

View File

@ -18,12 +18,13 @@ func initTestServoDriver() *ServoDriver {
func TestServoDriver(t *testing.T) {
var err interface{}
d := initTestServoDriver()
a := newGpioTestAdaptor()
d := NewServoDriver(a, "1")
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
testAdaptorServoWrite = func() (err error) {
a.testAdaptorServoWrite = func() (err error) {
return errors.New("pwm error")
}
@ -38,7 +39,6 @@ func TestServoDriver(t *testing.T) {
err = d.Command("Move")(map[string]interface{}{"angle": 100.0})
gobottest.Assert(t, err.(error), errors.New("pwm error"))
}
func TestServoDriverStart(t *testing.T) {