hybridgroup.gobot/platforms/mqtt/mqtt_adaptor_test.go

54 lines
1.2 KiB
Go
Raw Normal View History

2014-11-04 10:30:56 +08:00
package mqtt
import (
2014-11-05 00:33:05 +08:00
"fmt"
2014-11-04 10:56:33 +08:00
"testing"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
2014-11-04 10:30:56 +08:00
)
var _ gobot.Adaptor = (*MqttAdaptor)(nil)
2014-11-04 10:30:56 +08:00
func initTestMqttAdaptor() *MqttAdaptor {
return NewMqttAdaptor("mqtt", "localhost:1883", "client")
2014-11-04 10:30:56 +08:00
}
func TestMqttAdaptorConnect(t *testing.T) {
2014-11-04 10:56:33 +08:00
a := initTestMqttAdaptor()
gobottest.Assert(t, a.Connect()[0].Error(), "Network Error : Unknown protocol")
2014-11-04 10:30:56 +08:00
}
func TestMqttAdaptorFinalize(t *testing.T) {
2014-11-04 10:56:33 +08:00
a := initTestMqttAdaptor()
gobottest.Assert(t, len(a.Finalize()), 0)
2014-11-04 10:30:56 +08:00
}
2014-11-05 00:33:05 +08:00
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), false)
2014-11-05 00:33:05 +08:00
}
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
2014-11-05 00:33:05 +08:00
}
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.On("hola", func(data []byte) {
2014-11-05 00:33:05 +08:00
fmt.Println("hola")
}), false)
}
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
a.Connect()
gobottest.Assert(t, a.On("hola", func(data []byte) {
2014-11-05 00:33:05 +08:00
fmt.Println("hola")
}), true)
}