Add more firmata test coverage

This commit is contained in:
Adrian Zankich 2014-07-16 11:49:06 -07:00
parent e55744657c
commit 419193db49
2 changed files with 41 additions and 10 deletions

View File

@ -100,6 +100,7 @@ func newBoard(sp io.ReadWriteCloser) *board {
func (b *board) connect() {
if b.connected == false {
b.reset()
b.initBoard()
for {
@ -240,7 +241,9 @@ func (b *board) process(data []byte) {
b.majorVersion, _ = buf.ReadByte()
b.minorVersion, _ = buf.ReadByte()
gobot.Publish(b.events["report_version"], b.version())
case analogMessageRangeStart <= messageType && analogMessageRangeEnd >= messageType:
case analogMessageRangeStart <= messageType &&
analogMessageRangeEnd >= messageType:
leastSignificantByte, _ := buf.ReadByte()
mostSignificantByte, _ := buf.ReadByte()
@ -256,7 +259,9 @@ func (b *board) process(data []byte) {
byte(value & 0xff),
},
)
case digitalMessageRangeStart <= messageType && digitalMessageRangeEnd >= messageType:
case digitalMessageRangeStart <= messageType &&
digitalMessageRangeEnd >= messageType:
port := messageType & 0x0F
firstBitmask, _ := buf.ReadByte()
secondBitmask, _ := buf.ReadByte()
@ -299,6 +304,7 @@ func (b *board) process(data []byte) {
}
b.pins = append(b.pins, pin{modes, output, 0, 0})
b.events[fmt.Sprintf("digital_read_%v", len(b.pins)-1)] = gobot.NewEvent()
b.events[fmt.Sprintf("pin_%v_state", len(b.pins)-1)] = gobot.NewEvent()
supportedModes = 0
n = 0
continue
@ -338,7 +344,11 @@ func (b *board) process(data []byte) {
}
gobot.Publish(b.events[fmt.Sprintf("pin_%v_state", currentBuffer[2])],
[]byte{byte(pin.value & 0xff)},
map[string]int{
"pin": int(currentBuffer[2]),
"mode": int(pin.mode),
"value": int(pin.value),
},
)
case i2CReply:
i2cReply := map[string][]byte{

View File

@ -28,6 +28,24 @@ func initTestFirmata() *board {
return b
}
func TestReportVersion(t *testing.T) {
b := initTestFirmata()
//test if functions executes
b.reportVersion()
}
func TestQueryFirmware(t *testing.T) {
b := initTestFirmata()
//test if functions executes
b.queryFirmware()
}
func TestQueryPinState(t *testing.T) {
b := initTestFirmata()
//test if functions executes
b.queryPinState(byte(1))
}
func TestProcess(t *testing.T) {
b := initTestFirmata()
sem := make(chan bool)
@ -72,14 +90,17 @@ func TestProcess(t *testing.T) {
})
b.process([]byte{0x90, 0x16, 0x00})
<-sem
//TODO we need a test here
//pinStateResponse
//gobot.Once(b.events["pin_0_state"], func(data interface{}) {
// gobot.Expect(t, data, "")
// sem <- true
//})
//b.process([]byte{240, 0x6E})
//<-sem
gobot.Once(b.events["pin_13_state"], func(data interface{}) {
gobot.Expect(t, data, map[string]int{
"pin": 13,
"mode": 1,
"value": 1,
})
sem <- true
})
b.process([]byte{240, 110, 13, 1, 1, 247})
<-sem
//i2cReply
gobot.Once(b.events["i2c_reply"], func(data interface{}) {
i2c_reply := map[string][]byte{