Skip to content

Commit

Permalink
Tests for I2C
Browse files Browse the repository at this point in the history
  • Loading branch information
rafmagana committed Sep 18, 2014
1 parent 843ee4d commit 5148562
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 22 deletions.
12 changes: 6 additions & 6 deletions platforms/i2c/blinkm_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
),
}

b.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
return b.FirmwareVersion()
})
b.AddCommand("Color", func(params map[string]interface{}) interface{} {
return b.Color()
})
b.AddCommand("Rgb", func(params map[string]interface{}) interface{} {
red := byte(params["red"].(float64))
green := byte(params["green"].(float64))
Expand All @@ -39,6 +33,12 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
b.Fade(red, green, blue)
return nil
})
b.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
return b.FirmwareVersion()
})
b.AddCommand("Color", func(params map[string]interface{}) interface{} {
return b.Color()
})

return b
}
Expand Down
132 changes: 128 additions & 4 deletions platforms/i2c/blinkm_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,135 @@ import (
"testing"
)

func initTestBlinkMDriver() *BlinkMDriver {
return NewBlinkMDriver(newI2cTestAdaptor("adaptor"), "bot")
// --------- HELPERS
func initTestBlinkMDriver() (driver *BlinkMDriver) {
driver, _ = initTestBlinkDriverWithStubbedAdaptor()
return
}

func initTestBlinkDriverWithStubbedAdaptor() (*BlinkMDriver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor("adaptor")
return NewBlinkMDriver(adaptor, "bot"), adaptor
}

// --------- TESTS

func TestBlinkMDriver(t *testing.T) {
// Does it implement gobot.DriverInterface?
var _ gobot.DriverInterface = (*BlinkMDriver)(nil)

// Does its adaptor implements the I2cInterface?
driver := initTestBlinkMDriver()
var _ I2cInterface = driver.adaptor()
}

func TestNewBlinkMDriver(t *testing.T) {
// Does it return a pointer to an instance of BlinkMDriver?
var bm interface{} = NewBlinkMDriver(newI2cTestAdaptor("adaptor"), "bot")
_, ok := bm.(*BlinkMDriver)
if !ok {
t.Errorf("NewBlinkMDriver() should have returned a *BlinkMDriver")
}
}

// Commands
func TestNewBlinkMDriverCommands_Rgb(t *testing.T) {
blinkM := initTestBlinkMDriver()

result := blinkM.Driver.Command("Rgb")(rgb)
gobot.Assert(t, result, nil)
}

func TestNewBlinkMDriverCommands_Fade(t *testing.T) {
blinkM := initTestBlinkMDriver()

result := blinkM.Driver.Command("Fade")(rgb)
gobot.Assert(t, result, nil)
}

func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {
blinkM, adaptor := initTestBlinkDriverWithStubbedAdaptor()

param := make(map[string]interface{})

// When len(data) is 2
adaptor.i2cReadImpl = func() []byte {
return []byte{99, 1}
}

result := blinkM.Driver.Command("FirmwareVersion")(param)

gobot.Assert(t, result, blinkM.FirmwareVersion())

// When len(data) is not 2
adaptor.i2cReadImpl = func() []byte {
return []byte{99}
}
result = blinkM.Driver.Command("FirmwareVersion")(param)

gobot.Assert(t, result, blinkM.FirmwareVersion())
}

func TestNewBlinkMDriverCommands_Color(t *testing.T) {
blinkM := initTestBlinkMDriver()

param := make(map[string]interface{})

result := blinkM.Driver.Command("Color")(param)

gobot.Assert(t, result, blinkM.Color())
}

// Methods
func TestBlinkMDriverStart(t *testing.T) {
d := initTestBlinkMDriver()
gobot.Assert(t, d.Start(), true)
blinkM := initTestBlinkMDriver()

gobot.Assert(t, blinkM.Start(), true)
}

func TestBlinkMDriverInit(t *testing.T) {
blinkM := initTestBlinkMDriver()
gobot.Assert(t, blinkM.Init(), true)
}

func TestBlinkMDriverHalt(t *testing.T) {
blinkM := initTestBlinkMDriver()
gobot.Assert(t, blinkM.Halt(), true)
}

func TestBlinkMDriverFirmwareVersion(t *testing.T) {
blinkM, adaptor := initTestBlinkDriverWithStubbedAdaptor()

// when len(data) is 2
adaptor.i2cReadImpl = func() []byte {
return []byte{99, 1}
}

gobot.Assert(t, blinkM.FirmwareVersion(), "99.1")

// when len(data) is not 2
adaptor.i2cReadImpl = func() []byte {
return []byte{99}
}

gobot.Assert(t, blinkM.FirmwareVersion(), "")
}

func TestBlinkMDriverColor(t *testing.T) {
blinkM, adaptor := initTestBlinkDriverWithStubbedAdaptor()

// when len(data) is 3
adaptor.i2cReadImpl = func() []byte {
return []byte{99, 1, 2}
}

gobot.Assert(t, blinkM.Color(), []byte{99, 1, 2})

// when len(data) is not 3
adaptor.i2cReadImpl = func() []byte {
return []byte{99}
}

gobot.Assert(t, blinkM.Color(), []byte{})

}
1 change: 1 addition & 0 deletions platforms/i2c/hmc6352_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ func (h *HMC6352Driver) Start() bool {
})
return true
}

func (h *HMC6352Driver) Init() bool { return true }
func (h *HMC6352Driver) Halt() bool { return true }
79 changes: 74 additions & 5 deletions platforms/i2c/hmc6352_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,83 @@ package i2c
import (
"github.com/hybridgroup/gobot"
"testing"
"time"
)

func initTestHMC6352Driver() *HMC6352Driver {
return NewHMC6352Driver(newI2cTestAdaptor("adaptor"), "bot")
// --------- HELPERS
func initTestHMC6352Driver() (driver *HMC6352Driver) {
driver, _ = initTestHMC6352DriverWithStubbedAdaptor()
return
}

func initTestHMC6352DriverWithStubbedAdaptor() (*HMC6352Driver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor("adaptor")
return NewHMC6352Driver(adaptor, "bot"), adaptor
}

// --------- TESTS

func TestHMC6352Driver(t *testing.T) {
// Does it implement gobot.DriverInterface?
var _ gobot.DriverInterface = (*HMC6352Driver)(nil)

// Does its adaptor implements the I2cInterface?
driver := initTestHMC6352Driver()
var _ I2cInterface = driver.adaptor()
}

func TestNewHMC6352Driver(t *testing.T) {
// Does it return a pointer to an instance of HMC6352Driver?
var bm interface{} = NewHMC6352Driver(newI2cTestAdaptor("adaptor"), "bot")
_, ok := bm.(*HMC6352Driver)
if !ok {
t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver")
}
}

// Methods
func TestHMC6352DriverStart(t *testing.T) {
t.SkipNow()
d := initTestHMC6352Driver()
gobot.Assert(t, d.Start(), true)
// when len(data) is 2
hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor()

adaptor.i2cReadImpl = func() []byte {
return []byte{99, 1}
}

numberOfCyclesForEvery := 3

// Make sure "Heading" is set to 0 so that we assert
// its new value after executing "Start()"
gobot.Assert(t, hmc.Heading, uint16(0))

hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, hmc.Start(), true)
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)

gobot.Assert(t, hmc.Heading, uint16(2534))

// when len(data) is not 2
hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor()

adaptor.i2cReadImpl = func() []byte {
return []byte{99}
}

hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, hmc.Start(), true)
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)

gobot.Assert(t, hmc.Heading, uint16(0))
}

func TestHMC6352DriverInit(t *testing.T) {
hmc := initTestHMC6352Driver()

gobot.Assert(t, hmc.Init(), true)
}

func TestHMC6352DriverHalt(t *testing.T) {
hmc := initTestHMC6352Driver()

gobot.Assert(t, hmc.Halt(), true)
}
24 changes: 22 additions & 2 deletions platforms/i2c/test_helper.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package i2c

import "github.com/hybridgroup/gobot"
import (
"github.com/hybridgroup/gobot"
)

var rgb = map[string]interface{}{
"red": 1.0,
"green": 1.0,
"blue": 1.0,
}

func castColor(color string) byte {
return byte(rgb[color].(float64))
}

var red = castColor("red")
var green = castColor("green")
var blue = castColor("blue")

type i2cTestAdaptor struct {
gobot.Adaptor
i2cReadImpl func() []byte
}

func (t *i2cTestAdaptor) I2cStart(byte) {}
func (t *i2cTestAdaptor) I2cRead(uint) []byte {
return []byte{99, 1}
return t.i2cReadImpl()
}
func (t *i2cTestAdaptor) I2cWrite([]byte) {}
func (t *i2cTestAdaptor) Connect() bool { return true }
Expand All @@ -20,5 +37,8 @@ func newI2cTestAdaptor(name string) *i2cTestAdaptor {
name,
"I2cTestAdaptor",
),
i2cReadImpl: func() []byte {
return []byte{}
},
}
}
1 change: 1 addition & 0 deletions platforms/i2c/wiichuck_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (w *WiichuckDriver) Start() bool {
})
return true
}

func (w *WiichuckDriver) Init() bool { return true }
func (w *WiichuckDriver) Halt() bool { return true }

Expand Down
Loading

0 comments on commit 5148562

Please sign in to comment.