Exemplo n.º 1
0
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)
	gobottest.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)
	gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
}
Exemplo n.º 2
0
func TestSpheroDriverSetDataStreaming(t *testing.T) {
	d := initTestSpheroDriver()
	d.SetDataStreaming(DefaultDataStreamingConfig())

	data := <-d.packetChannel

	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, DefaultDataStreamingConfig())

	gobottest.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,
		},
	)
	gobottest.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)

	gobottest.Assert(t, data.body, buf.Bytes())
}
Exemplo n.º 3
0
func TestRgbLedDriver(t *testing.T) {
	var err interface{}

	d := initTestRgbLedDriver(newGpioTestAdaptor("adaptor"))

	gobottest.Assert(t, d.Name(), "bot")
	gobottest.Assert(t, d.Pin(), "r=1, g=2, b=3")
	gobottest.Assert(t, d.RedPin(), "1")
	gobottest.Assert(t, d.GreenPin(), "2")
	gobottest.Assert(t, d.BluePin(), "3")
	gobottest.Assert(t, d.Connection().Name(), "adaptor")

	testAdaptorDigitalWrite = func() (err error) {
		return errors.New("write error")
	}
	testAdaptorPwmWrite = func() (err error) {
		return errors.New("pwm error")
	}

	err = d.Command("Toggle")(nil)
	gobottest.Assert(t, err.(error), errors.New("pwm error"))

	err = d.Command("On")(nil)
	gobottest.Assert(t, err.(error), errors.New("pwm error"))

	err = d.Command("Off")(nil)
	gobottest.Assert(t, err.(error), errors.New("pwm error"))

	err = d.Command("SetRGB")(map[string]interface{}{"r": 0xff, "g": 0xff, "b": 0xff})
	gobottest.Assert(t, err.(error), errors.New("pwm error"))

}
Exemplo n.º 4
0
func TestJoystickAdaptorConnect(t *testing.T) {
	a := initTestJoystickAdaptor()
	gobottest.Assert(t, len(a.Connect()), 0)

	a = NewJoystickAdaptor("bot")
	gobottest.Assert(t, a.Connect()[0], errors.New("No joystick available"))
}
Exemplo n.º 5
0
func TestServoConfig(t *testing.T) {
	b := New()
	b.connection = readWriteCloser{}

	tests := []struct {
		description string
		arguments   [3]int
		expected    []byte
		result      error
	}{
		{
			description: "Min values for min & max",
			arguments:   [3]int{9, 0, 0},
			expected:    []byte{0xF0, 0x70, 9, 0, 0, 0, 0, 0xF7},
		},
		{
			description: "Max values for min & max",
			arguments:   [3]int{9, 0x3FFF, 0x3FFF},
			expected:    []byte{0xF0, 0x70, 9, 0x7F, 0x7F, 0x7F, 0x7F, 0xF7},
		},
		{
			description: "Clipped max values for min & max",
			arguments:   [3]int{9, 0xFFFF, 0xFFFF},
			expected:    []byte{0xF0, 0x70, 9, 0x7F, 0x7F, 0x7F, 0x7F, 0xF7},
		},
	}

	for _, test := range tests {
		testWriteData.Reset()
		err := b.ServoConfig(test.arguments[0], test.arguments[1], test.arguments[2])
		gobottest.Assert(t, testWriteData.Bytes(), test.expected)
		gobottest.Assert(t, err, test.result)
	}
}
Exemplo n.º 6
0
func TestConfigureLocator(t *testing.T) {
	d := initTestSpheroDriver()
	d.ConfigureLocator(DefaultLocatorConfig())
	data := <-d.packetChannel

	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, DefaultLocatorConfig())

	gobottest.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,
		},
	)
	gobottest.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)

	gobottest.Assert(t, data.body, buf.Bytes())
}
Exemplo n.º 7
0
func TestMavlinkAdaptorConnect(t *testing.T) {
	a := initTestMavlinkAdaptor()
	gobottest.Assert(t, len(a.Connect()), 0)

	a.connect = func(port string) (io.ReadWriteCloser, error) { return nil, errors.New("connect error") }
	gobottest.Assert(t, a.Connect()[0], errors.New("connect error"))
}
Exemplo n.º 8
0
func TestServoDriverMove(t *testing.T) {
	d := initTestServoDriver()
	d.Move(100)
	gobottest.Assert(t, d.CurrentAngle, uint8(100))
	err := d.Move(200)
	gobottest.Assert(t, err, ErrServoOutOfRange)
}
Exemplo n.º 9
0
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")
	gobottest.Assert(t, val, 1)
	testServer.Close()

	// When LOW
	response = `{"return_value": 0}`

	testServer = getDummyResponseForPathWithParams("/"+a.DeviceID+"/digitalread", params, response, t)

	a.setAPIServer(testServer.URL)
	defer testServer.Close()

	val, _ = a.DigitalRead("D7")
	gobottest.Assert(t, val, 0)
}
Exemplo n.º 10
0
func TestDigisparkAdaptorConnect(t *testing.T) {
	a := NewDigisparkAdaptor("bot")
	gobottest.Assert(t, a.Connect()[0], ErrConnection)

	a = initTestDigisparkAdaptor()
	gobottest.Assert(t, len(a.Connect()), 0)
}
Exemplo n.º 11
0
func TestMCP23017DriverGetPort(t *testing.T) {
	// port A
	mcp := initTestMCP23017Driver(0)
	expectedPort := getBank(0).PortA
	actualPort := mcp.getPort("A")
	gobottest.Assert(t, expectedPort, actualPort)

	// port B
	mcp = initTestMCP23017Driver(0)
	expectedPort = getBank(0).PortB
	actualPort = mcp.getPort("B")
	gobottest.Assert(t, expectedPort, actualPort)

	// default
	mcp = initTestMCP23017Driver(0)
	expectedPort = getBank(0).PortA
	actualPort = mcp.getPort("")
	gobottest.Assert(t, expectedPort, actualPort)

	// port A bank 1
	mcp = initTestMCP23017Driver(1)
	expectedPort = getBank(1).PortA
	actualPort = mcp.getPort("")
	gobottest.Assert(t, expectedPort, actualPort)
}
Exemplo n.º 12
0
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()
	gobottest.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()
	gobottest.Assert(t, color, []byte{})

	adaptor.i2cWriteImpl = func() error {
		return errors.New("write error")
	}

	color, err := blinkM.Color()
	gobottest.Assert(t, err, errors.New("write error"))

}
Exemplo n.º 13
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()
	gobottest.Assert(t, version, "99.1")

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

	version, _ = blinkM.FirmwareVersion()
	gobottest.Assert(t, version, "")

	adaptor.i2cWriteImpl = func() error {
		return errors.New("write error")
	}

	version, err := blinkM.FirmwareVersion()
	gobottest.Assert(t, err, errors.New("write error"))
}
Exemplo n.º 14
0
func TestDirectPinDriver(t *testing.T) {
	var ret map[string]interface{}
	var err interface{}

	d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
	gobottest.Assert(t, d.Name(), "bot")
	gobottest.Assert(t, d.Pin(), "1")
	gobottest.Assert(t, d.Connection().Name(), "adaptor")

	ret = d.Command("DigitalRead")(nil).(map[string]interface{})

	gobottest.Assert(t, ret["val"].(int), 1)
	gobottest.Assert(t, ret["err"], nil)

	err = d.Command("DigitalWrite")(map[string]interface{}{"level": "1"})
	gobottest.Assert(t, err.(error), errors.New("write error"))

	ret = d.Command("AnalogRead")(nil).(map[string]interface{})

	gobottest.Assert(t, ret["val"].(int), 80)
	gobottest.Assert(t, ret["err"], nil)

	err = d.Command("PwmWrite")(map[string]interface{}{"level": "1"})
	gobottest.Assert(t, err.(error), errors.New("write error"))

	err = d.Command("ServoWrite")(map[string]interface{}{"level": "1"})
	gobottest.Assert(t, err.(error), errors.New("write error"))
}
Exemplo n.º 15
0
func TestNullReadWriteCloser(t *testing.T) {
	n := &NullReadWriteCloser{}
	i, _ := n.Write([]byte{1, 2, 3})
	gobottest.Assert(t, i, 3)
	i, _ = n.Read(make([]byte, 10))
	gobottest.Assert(t, i, 10)
	gobottest.Assert(t, n.Close(), nil)
}
Exemplo n.º 16
0
func TestSparkCoreAdaptorPinLevel(t *testing.T) {

	a := initTestSparkCoreAdaptor()

	gobottest.Assert(t, a.pinLevel(1), "HIGH")
	gobottest.Assert(t, a.pinLevel(0), "LOW")
	gobottest.Assert(t, a.pinLevel(5), "LOW")
}
Exemplo n.º 17
0
func TestWiichuckDriverDecode(t *testing.T) {
	wii := initTestWiichuckDriver()

	gobottest.Assert(t, wii.decode(byte(0)), float64(46))
	gobottest.Assert(t, wii.decode(byte(100)), float64(138))
	gobottest.Assert(t, wii.decode(byte(200)), float64(246))
	gobottest.Assert(t, wii.decode(byte(254)), float64(0))
}
Exemplo n.º 18
0
func TestWiichuckDriverCalculateJoystickValue(t *testing.T) {
	wii := initTestWiichuckDriver()

	gobottest.Assert(t, wii.calculateJoystickValue(float64(20), float64(5)), float64(15))
	gobottest.Assert(t, wii.calculateJoystickValue(float64(1), float64(2)), float64(-1))
	gobottest.Assert(t, wii.calculateJoystickValue(float64(10), float64(5)), float64(5))
	gobottest.Assert(t, wii.calculateJoystickValue(float64(5), float64(10)), float64(-5))
}
Exemplo n.º 19
0
func TestRgbLedDriverToggle(t *testing.T) {
	d := initTestRgbLedDriver(newGpioTestAdaptor("adaptor"))
	d.Off()
	d.Toggle()
	gobottest.Assert(t, d.State(), true)
	d.Toggle()
	gobottest.Assert(t, d.State(), false)
}
Exemplo n.º 20
0
func TestButtonDriver(t *testing.T) {
	d := NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
	gobottest.Assert(t, d.Name(), "bot")
	gobottest.Assert(t, d.Connection().Name(), "adaptor")

	d = NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1", 30*time.Second)
	gobottest.Assert(t, d.interval, 30*time.Second)
}
Exemplo n.º 21
0
func TestMotorDriverToggle(t *testing.T) {
	d := initTestMotorDriver()
	d.Off()
	d.Toggle()
	gobottest.Assert(t, d.IsOn(), true)
	d.Toggle()
	gobottest.Assert(t, d.IsOn(), false)
}
Exemplo n.º 22
0
func TestArdroneAdaptor(t *testing.T) {
	a := NewArdroneAdaptor("drone")
	gobottest.Assert(t, a.Name(), "drone")
	gobottest.Assert(t, a.config.Ip, "192.168.1.1")

	a = NewArdroneAdaptor("drone", "192.168.100.100")
	gobottest.Assert(t, a.config.Ip, "192.168.100.100")
}
Exemplo n.º 23
0
func TestFirmataAdaptorFinalize(t *testing.T) {
	a := initTestFirmataAdaptor()
	gobottest.Assert(t, len(a.Finalize()), 0)

	a = initTestFirmataAdaptor()
	a.board.(*mockFirmataBoard).disconnectError = errors.New("close error")
	gobottest.Assert(t, a.Finalize()[0], errors.New("close error"))
}
Exemplo n.º 24
0
func TestSetHatAddresses(t *testing.T) {
	ada, _ := initTestAdafruitMotorHatDriverWithStubbedAdaptor()

	motorHatAddr := 0x61
	servoHatAddr := 0x41
	gobottest.Assert(t, len(ada.SetMotorHatAddress(motorHatAddr)), 0)
	gobottest.Assert(t, len(ada.SetServoHatAddress(servoHatAddr)), 0)
}
Exemplo n.º 25
0
func TestMCP23017DriverWrite(t *testing.T) {
	// clear bit
	mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
	port := mcp.getPort("A")
	adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
		return make([]byte, b), nil
	}
	adaptor.i2cMcpWriteImpl = func() error {
		return nil
	}
	err := mcp.write(port.IODIR, uint8(7), 0)
	gobottest.Assert(t, err, nil)

	// set bit
	mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
	port = mcp.getPort("B")
	adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
		return make([]byte, b), nil
	}
	adaptor.i2cMcpWriteImpl = func() error {
		return nil
	}
	err = mcp.write(port.IODIR, uint8(7), 1)
	gobottest.Assert(t, err, nil)

	// write error
	mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
	adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
		return make([]byte, b), nil
	}
	adaptor.i2cMcpWriteImpl = func() error {
		return errors.New("write error")
	}
	err = mcp.write(port.IODIR, uint8(7), 0)
	gobottest.Assert(t, err, errors.New("write error"))

	// read error
	mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
	adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
		return make([]byte, b), errors.New("read error")
	}
	err = mcp.write(port.IODIR, uint8(7), 0)
	gobottest.Assert(t, err, errors.New("read error"))

	//debug
	debug = true
	log.SetOutput(ioutil.Discard)
	adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
		return make([]byte, b), nil
	}
	adaptor.i2cMcpWriteImpl = func() error {
		return nil
	}
	err = mcp.write(port.IODIR, uint8(7), 1)
	gobottest.Assert(t, err, nil)
	debug = false
	log.SetOutput(os.Stdout)
}
Exemplo n.º 26
0
func TestNeuroskyAdaptorFinalize(t *testing.T) {
	a := initTestNeuroskyAdaptor()
	a.Connect()
	gobottest.Assert(t, len(a.Finalize()), 0)

	closeError = errors.New("close error")
	a.Connect()
	gobottest.Assert(t, a.Finalize()[0], errors.New("close error"))
}
Exemplo n.º 27
0
func TestArdroneAdaptorConnect(t *testing.T) {
	a := initTestArdroneAdaptor()
	gobottest.Assert(t, len(a.Connect()), 0)

	a.connect = func(a *ArdroneAdaptor) (drone, error) {
		return nil, errors.New("connection error")
	}
	gobottest.Assert(t, a.Connect()[0], errors.New("connection error"))
}
Exemplo n.º 28
0
func TestBebopAdaptorConnect(t *testing.T) {
	a := initTestBebopAdaptor()
	gobottest.Assert(t, len(a.Connect()), 0)

	a.connect = func(a *BebopAdaptor) error {
		return errors.New("connection error")
	}
	gobottest.Assert(t, a.Connect()[0], errors.New("connection error"))
}
Exemplo n.º 29
0
func TestMPU6050Driver(t *testing.T) {
	mpu := initTestMPU6050Driver()
	gobottest.Assert(t, mpu.Name(), "bot")
	gobottest.Assert(t, mpu.Connection().Name(), "adaptor")
	gobottest.Assert(t, mpu.interval, 10*time.Millisecond)

	mpu = NewMPU6050Driver(newI2cTestAdaptor("adaptor"), "bot", 100*time.Millisecond)
	gobottest.Assert(t, mpu.interval, 100*time.Millisecond)
}
Exemplo n.º 30
0
func TestLeapMotionDriverStart(t *testing.T) {
	d := initTestLeapMotionDriver()
	gobottest.Assert(t, len(d.Start()), 0)

	d = initTestLeapMotionDriver()
	writeError = errors.New("write error")
	gobottest.Assert(t, d.Start()[0], errors.New("write error"))

}