func TestJoystickAdaptorConnect(t *testing.T) { a := initTestJoystickAdaptor() gobot.Assert(t, len(a.Connect()), 0) a = NewJoystickAdaptor("bot") gobot.Assert(t, a.Connect()[0], errors.New("No joystick available")) }
func TestMCP23017DriverGetPort(t *testing.T) { // port a mcp := initTestMCP23017Driver(0) expectedPort := getBank(0).PortA actualPort := mcp.getPort("A") gobot.Assert(t, expectedPort, actualPort) // port b mcp = initTestMCP23017Driver(0) expectedPort = getBank(0).PortB actualPort = mcp.getPort("B") gobot.Assert(t, expectedPort, actualPort) // default mcp = initTestMCP23017Driver(0) expectedPort = getBank(0).PortA actualPort = mcp.getPort("") gobot.Assert(t, expectedPort, actualPort) // port a bank 1 mcp = initTestMCP23017Driver(1) expectedPort = getBank(1).PortA actualPort = mcp.getPort("") gobot.Assert(t, expectedPort, actualPort) }
func TestMavlinkAdaptorConnect(t *testing.T) { a := initTestMavlinkAdaptor() gobot.Assert(t, len(a.Connect()), 0) a.connect = func(port string) (io.ReadWriteCloser, error) { return nil, errors.New("connect error") } gobot.Assert(t, a.Connect()[0], errors.New("connect error")) }
func TestServoDriverMove(t *testing.T) { d := initTestServoDriver() d.Move(100) gobot.Assert(t, d.CurrentAngle, uint8(100)) err := d.Move(200) gobot.Assert(t, err, ErrServoOutOfRange) }
func TestWiichuckDriverUpdateButtons(t *testing.T) { //when data["c"] is 0 chann := make(chan bool) wii := initTestWiichuckDriver() wii.data["c"] = 0 wii.updateButtons() gobot.On(wii.Event(C), func(data interface{}) { gobot.Assert(t, true, data) chann <- true }) <-chann //when data["z"] is 0 chann = make(chan bool) wii = initTestWiichuckDriver() wii.data["z"] = 0 wii.updateButtons() gobot.On(wii.Event(Z), func(data interface{}) { gobot.Assert(t, true, data) chann <- true }) <-chann }
func TestConfigureLocator(t *testing.T) { d := initTestSpheroDriver() d.ConfigureLocator(DefaultLocatorConfig()) data := <-d.packetChannel buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, DefaultLocatorConfig()) gobot.Assert(t, data.body, buf.Bytes()) ret := d.Command("ConfigureLocator")( map[string]interface{}{ "Flags": 1.0, "X": 100.0, "Y": 100.0, "YawTare": 0.0, }, ) gobot.Assert(t, ret, nil) data = <-d.packetChannel lconfig := LocatorConfig{Flags: 1, X: 100, Y: 100, YawTare: 0} buf = new(bytes.Buffer) binary.Write(buf, binary.BigEndian, lconfig) gobot.Assert(t, data.body, buf.Bytes()) }
func TestSpheroDriverSetDataStreaming(t *testing.T) { d := initTestSpheroDriver() d.SetDataStreaming(DefaultDataStreamingConfig()) data := <-d.packetChannel buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, DefaultDataStreamingConfig()) gobot.Assert(t, data.body, buf.Bytes()) ret := d.Command("SetDataStreaming")( map[string]interface{}{ "N": 100.0, "M": 200.0, "Mask": 300.0, "Pcnt": 255.0, "Mask2": 400.0, }, ) gobot.Assert(t, ret, nil) data = <-d.packetChannel dconfig := DataStreamingConfig{N: 100, M: 200, Mask: 300, Pcnt: 255, Mask2: 400} buf = new(bytes.Buffer) binary.Write(buf, binary.BigEndian, dconfig) gobot.Assert(t, data.body, buf.Bytes()) }
func TestDigisparkAdaptorConnect(t *testing.T) { a := NewDigisparkAdaptor("bot") gobot.Assert(t, a.Connect()[0], ErrConnection) a = initTestDigisparkAdaptor() gobot.Assert(t, len(a.Connect()), 0) }
func TestBlinkMDriverFirmwareVersion(t *testing.T) { blinkM, adaptor := initTestBlinkDriverWithStubbedAdaptor() // when len(data) is 2 adaptor.i2cReadImpl = func() ([]byte, error) { return []byte{99, 1}, nil } version, _ := blinkM.FirmwareVersion() gobot.Assert(t, version, "99.1") // when len(data) is not 2 adaptor.i2cReadImpl = func() ([]byte, error) { return []byte{99}, nil } version, _ = blinkM.FirmwareVersion() gobot.Assert(t, version, "") adaptor.i2cWriteImpl = func() error { return errors.New("write error") } version, err := blinkM.FirmwareVersion() gobot.Assert(t, err, errors.New("write error")) }
func TestBlinkMDriverColor(t *testing.T) { blinkM, adaptor := initTestBlinkDriverWithStubbedAdaptor() // when len(data) is 3 adaptor.i2cReadImpl = func() ([]byte, error) { return []byte{99, 1, 2}, nil } color, _ := blinkM.Color() gobot.Assert(t, color, []byte{99, 1, 2}) // when len(data) is not 3 adaptor.i2cReadImpl = func() ([]byte, error) { return []byte{99}, nil } color, _ = blinkM.Color() gobot.Assert(t, color, []byte{}) adaptor.i2cWriteImpl = func() error { return errors.New("write error") } color, err := blinkM.Color() gobot.Assert(t, err, errors.New("write error")) }
func TestExecuteMcpCommand(t *testing.T) { var body interface{} a := initTestAPI() // known command request, _ := http.NewRequest("GET", "/api/commands/TestFunction", bytes.NewBufferString(`{"message":"Beep Boop"}`), ) request.Header.Add("Content-Type", "application/json") response := httptest.NewRecorder() a.ServeHTTP(response, request) json.NewDecoder(response.Body).Decode(&body) gobot.Assert(t, body.(map[string]interface{})["result"], "hey Beep Boop") // unknown command request, _ = http.NewRequest("GET", "/api/commands/TestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`), ) request.Header.Add("Content-Type", "application/json") response = httptest.NewRecorder() a.ServeHTTP(response, request) json.NewDecoder(response.Body).Decode(&body) gobot.Assert(t, body.(map[string]interface{})["error"], "Unknown Command") }
func TestSparkCoreAdaptorDigitalRead(t *testing.T) { // When HIGH response := `{"return_value": 1}` params := []string{"D7"} a := initTestSparkCoreAdaptor() testServer := getDummyResponseForPathWithParams("/"+a.DeviceID+"/digitalread", params, response, t) a.setAPIServer(testServer.URL) val, _ := a.DigitalRead("D7") gobot.Assert(t, val, 1) testServer.Close() // When LOW response = `{"return_value": 0}` testServer = getDummyResponseForPathWithParams("/"+a.DeviceID+"/digitalread", params, response, t) a.setAPIServer(testServer.URL) val, _ = a.DigitalRead("D7") gobot.Assert(t, val, 0) testServer.Close() // When error testServer = createTestServer(func(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) }) defer testServer.Close() val, _ = a.DigitalRead("D7") gobot.Assert(t, val, -1) }
func TestDirectPinDriver(t *testing.T) { var ret map[string]interface{} var err interface{} d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor")) gobot.Assert(t, d.Name(), "bot") gobot.Assert(t, d.Pin(), "1") gobot.Assert(t, d.Connection().Name(), "adaptor") ret = d.Command("DigitalRead")(nil).(map[string]interface{}) gobot.Assert(t, ret["val"].(int), 1) gobot.Assert(t, ret["err"], nil) err = d.Command("DigitalWrite")(map[string]interface{}{"level": "1"}) gobot.Assert(t, err.(error), errors.New("write error")) ret = d.Command("AnalogRead")(nil).(map[string]interface{}) gobot.Assert(t, ret["val"].(int), 80) gobot.Assert(t, ret["err"], nil) err = d.Command("PwmWrite")(map[string]interface{}{"level": "1"}) gobot.Assert(t, err.(error), errors.New("write error")) err = d.Command("ServoWrite")(map[string]interface{}{"level": "1"}) gobot.Assert(t, err.(error), errors.New("write error")) }
func TestArdroneAdaptor(t *testing.T) { a := NewArdroneAdaptor("drone") gobot.Assert(t, a.Name(), "drone") gobot.Assert(t, a.config.Ip, "192.168.1.1") a = NewArdroneAdaptor("drone", "192.168.100.100") gobot.Assert(t, a.config.Ip, "192.168.100.100") }
func TestFirmataAdaptorFinalize(t *testing.T) { a := initTestFirmataAdaptor() gobot.Assert(t, len(a.Finalize()), 0) a = initTestFirmataAdaptor() a.board.(*mockFirmataBoard).disconnectError = errors.New("close error") gobot.Assert(t, a.Finalize()[0], errors.New("close error")) }
func TestFurbyDriverToggle(t *testing.T) { d := initTestFurbyDriver(newGpioTestAdaptor("adaptor"), nil) d.Off() d.Toggle() gobot.Assert(t, d.State(), true) d.Toggle() gobot.Assert(t, d.State(), false) }
func TestMotorDriverToggle(t *testing.T) { d := initTestMotorDriver() d.Off() d.Toggle() gobot.Assert(t, d.IsOn(), true) d.Toggle() gobot.Assert(t, d.IsOn(), false) }
func TestButtonDriver(t *testing.T) { d := NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1") gobot.Assert(t, d.Name(), "bot") gobot.Assert(t, d.Connection().Name(), "adaptor") d = NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1", 30*time.Second) gobot.Assert(t, d.interval, 30*time.Second) }
func TestFirmataAdaptorFinalize(t *testing.T) { a := initTestFirmataAdaptor() gobot.Assert(t, len(a.Finalize()), 0) closeErr = errors.New("close error") a = initTestFirmataAdaptor() gobot.Assert(t, a.Finalize()[0], errors.New("close error")) }
func TestWiichuckDriverDecode(t *testing.T) { wii := initTestWiichuckDriver() gobot.Assert(t, wii.decode(byte(0)), float64(46)) gobot.Assert(t, wii.decode(byte(100)), float64(138)) gobot.Assert(t, wii.decode(byte(200)), float64(246)) gobot.Assert(t, wii.decode(byte(254)), float64(0)) }
func TestWiichuckDriverCalculateJoystickValue(t *testing.T) { wii := initTestWiichuckDriver() gobot.Assert(t, wii.calculateJoystickValue(float64(20), float64(5)), float64(15)) gobot.Assert(t, wii.calculateJoystickValue(float64(1), float64(2)), float64(-1)) gobot.Assert(t, wii.calculateJoystickValue(float64(10), float64(5)), float64(5)) gobot.Assert(t, wii.calculateJoystickValue(float64(5), float64(10)), float64(-5)) }
func TestSparkCoreAdaptorPinLevel(t *testing.T) { a := initTestSparkCoreAdaptor() gobot.Assert(t, a.pinLevel(1), "HIGH") gobot.Assert(t, a.pinLevel(0), "LOW") gobot.Assert(t, a.pinLevel(5), "LOW") }
func TestMotorDriverIsOn(t *testing.T) { d := initTestMotorDriver() d.CurrentMode = "digital" d.CurrentState = 1 gobot.Assert(t, d.IsOn(), true) d.CurrentMode = "analog" d.CurrentSpeed = 100 gobot.Assert(t, d.IsOn(), true) }
func TestLeapMotionDriverStart(t *testing.T) { d := initTestLeapMotionDriver() gobot.Assert(t, len(d.Start()), 0) d = initTestLeapMotionDriver() writeError = errors.New("write error") gobot.Assert(t, d.Start()[0], errors.New("write error")) }
func TestMavlinkAdaptorFinalize(t *testing.T) { a := initTestMavlinkAdaptor() gobot.Assert(t, len(a.Finalize()), 0) testAdaptorClose = func() error { return errors.New("close error") } gobot.Assert(t, a.Finalize()[0], errors.New("close error")) }
func TestNeuroskyAdaptorConnect(t *testing.T) { a := initTestNeuroskyAdaptor() gobot.Assert(t, len(a.Connect()), 0) a.connect = func(n *NeuroskyAdaptor) (io.ReadWriteCloser, error) { return nil, errors.New("connection error") } gobot.Assert(t, a.Connect()[0], errors.New("connection error")) }
func TestWiichuckDriver(t *testing.T) { wii := initTestWiichuckDriver() gobot.Assert(t, wii.Name(), "bot") gobot.Assert(t, wii.Connection().Name(), "adaptor") gobot.Assert(t, wii.interval, 10*time.Millisecond) wii = NewWiichuckDriver(newI2cTestAdaptor("adaptor"), "bot", 100*time.Millisecond) gobot.Assert(t, wii.interval, 100*time.Millisecond) }
func TestNeuroskyAdaptorFinalize(t *testing.T) { a := initTestNeuroskyAdaptor() a.Connect() gobot.Assert(t, len(a.Finalize()), 0) closeError = errors.New("close error") a.Connect() gobot.Assert(t, a.Finalize()[0], errors.New("close error")) }
func TestDirectPinDriverAnalogRead(t *testing.T) { d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor")) ret, err := d.AnalogRead() gobot.Assert(t, ret, 80) d = initTestDirectPinDriver(&gpioTestBareAdaptor{}) ret, err = d.AnalogRead() gobot.Assert(t, err, ErrAnalogReadUnsupported) }
func TestMPU6050Driver(t *testing.T) { mpu := initTestMPU6050Driver() gobot.Assert(t, mpu.Name(), "bot") gobot.Assert(t, mpu.Connection().Name(), "adaptor") gobot.Assert(t, mpu.interval, 10*time.Millisecond) mpu = NewMPU6050Driver(newI2cTestAdaptor("adaptor"), "bot", 100*time.Millisecond) gobot.Assert(t, mpu.interval, 100*time.Millisecond) }