Ejemplo n.º 1
0
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"))
}
Ejemplo n.º 2
0
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)
}
Ejemplo n.º 3
0
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"))
}
Ejemplo n.º 4
0
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)
}
Ejemplo n.º 5
0
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
}
Ejemplo 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())

	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())
}
Ejemplo n.º 7
0
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())
}
Ejemplo n.º 8
0
func TestDigisparkAdaptorConnect(t *testing.T) {
	a := NewDigisparkAdaptor("bot")
	gobot.Assert(t, a.Connect()[0], ErrConnection)

	a = initTestDigisparkAdaptor()
	gobot.Assert(t, len(a.Connect()), 0)
}
Ejemplo n.º 9
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"))
}
Ejemplo n.º 10
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()
	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"))

}
Ejemplo n.º 11
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)
	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")
}
Ejemplo n.º 12
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")
	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)
}
Ejemplo n.º 13
0
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"))
}
Ejemplo n.º 14
0
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")
}
Ejemplo n.º 15
0
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"))
}
Ejemplo n.º 16
0
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)
}
Ejemplo n.º 17
0
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)
}
Ejemplo n.º 18
0
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)
}
Ejemplo n.º 19
0
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"))
}
Ejemplo n.º 20
0
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))
}
Ejemplo n.º 21
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))
}
Ejemplo n.º 22
0
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")
}
Ejemplo n.º 23
0
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)
}
Ejemplo n.º 24
0
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"))

}
Ejemplo n.º 25
0
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"))
}
Ejemplo n.º 26
0
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"))
}
Ejemplo n.º 27
0
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)
}
Ejemplo n.º 28
0
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"))
}
Ejemplo n.º 29
0
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)
}
Ejemplo n.º 30
0
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)
}