func TestAdaptorConnect(t *testing.T) { a := NewAdaptor() gobottest.Assert(t, a.Connect(), ErrConnection) a = initTestAdaptor() gobottest.Assert(t, a.Connect(), nil) }
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) }
func TestBMP180DriverMeasurements(t *testing.T) { bmp180, adaptor := initTestBMP180DriverWithStubbedAdaptor() adaptor.i2cReadImpl = func() ([]byte, error) { buf := new(bytes.Buffer) // Values from the datasheet example. if adaptor.written[len(adaptor.written)-1] == bmp180RegisterAC1MSB { binary.Write(buf, binary.BigEndian, int16(408)) binary.Write(buf, binary.BigEndian, int16(-72)) binary.Write(buf, binary.BigEndian, int16(-14383)) binary.Write(buf, binary.BigEndian, uint16(32741)) binary.Write(buf, binary.BigEndian, uint16(32757)) binary.Write(buf, binary.BigEndian, uint16(23153)) binary.Write(buf, binary.BigEndian, int16(6190)) binary.Write(buf, binary.BigEndian, int16(4)) binary.Write(buf, binary.BigEndian, int16(-32768)) binary.Write(buf, binary.BigEndian, int16(-8711)) binary.Write(buf, binary.BigEndian, int16(2868)) } else if adaptor.written[len(adaptor.written)-2] == bmp180CmdTemp && adaptor.written[len(adaptor.written)-1] == bmp180RegisterTempMSB { binary.Write(buf, binary.BigEndian, int16(27898)) } else if adaptor.written[len(adaptor.written)-2] == bmp180CmdPressure && adaptor.written[len(adaptor.written)-1] == bmp180RegisterPressureMSB { binary.Write(buf, binary.BigEndian, int16(23843)) // XLSB, not used in this test. buf.WriteByte(0) } return buf.Bytes(), nil } bmp180.Start() temp, err := bmp180.Temperature() gobottest.Assert(t, err, nil) gobottest.Assert(t, temp, float32(15.0)) pressure, err := bmp180.Pressure(BMP180UltraLowPower) gobottest.Assert(t, err, nil) gobottest.Assert(t, pressure, float32(69964)) }
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") }
func TestAdaptorConnect(t *testing.T) { a := initTestAdaptor() gobottest.Assert(t, a.Connect(), nil) a = NewAdaptor() gobottest.Assert(t, a.Connect(), errors.New("No joystick available")) }
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) } }
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()) }
func TestArdroneAdaptor(t *testing.T) { a := NewAdaptor() gobottest.Assert(t, a.config.Ip, "192.168.1.1") a = NewAdaptor("192.168.100.100") gobottest.Assert(t, a.config.Ip, "192.168.100.100") }
// Test Name & SetName func TestSHT3xDriverName(t *testing.T) { sht3x := initTestSHT3xDriver() gobottest.Assert(t, sht3x.Name(), "SHT3x") sht3x.SetName("Sensor") gobottest.Assert(t, sht3x.Name(), "Sensor") }
func TestL3GD20HDriverScale(t *testing.T) { d := initTestL3GD20HDriver() gobottest.Assert(t, d.Scale(), L3GD20HScale250dps) d.SetScale(L3GD20HScale500dps) gobottest.Assert(t, d.Scale(), L3GD20HScale500dps) }
func TestServoDriver(t *testing.T) { var err interface{} d := initTestServoDriver() gobottest.Assert(t, d.Pin(), "1") gobottest.Refute(t, d.Connection(), nil) testAdaptorServoWrite = func() (err error) { return errors.New("pwm error") } err = d.Command("Min")(nil) gobottest.Assert(t, err.(error), errors.New("pwm error")) err = d.Command("Center")(nil) gobottest.Assert(t, err.(error), errors.New("pwm error")) err = d.Command("Max")(nil) gobottest.Assert(t, err.(error), errors.New("pwm error")) err = d.Command("Move")(map[string]interface{}{"angle": 100.0}) gobottest.Assert(t, err.(error), errors.New("pwm error")) }
func TestAdaptorPinLevel(t *testing.T) { a := initTestAdaptor() gobottest.Assert(t, a.pinLevel(1), "HIGH") gobottest.Assert(t, a.pinLevel(0), "LOW") gobottest.Assert(t, a.pinLevel(5), "LOW") }
func TestGroveTemperatureSensorDriver(t *testing.T) { testAdaptor := newAioTestAdaptor() d := NewGroveTemperatureSensorDriver(testAdaptor, "123") gobottest.Assert(t, d.Connection(), testAdaptor) gobottest.Assert(t, d.Pin(), "123") gobottest.Assert(t, d.interval, 10*time.Millisecond) }
func TestMavlinkAdaptorConnect(t *testing.T) { a := initTestMavlinkAdaptor() gobottest.Assert(t, a.Connect(), nil) a.connect = func(port string) (io.ReadWriteCloser, error) { return nil, errors.New("connect error") } gobottest.Assert(t, a.Connect(), errors.New("connect 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() 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")) }
func TestAdaptorDigitalRead(t *testing.T) { // When HIGH response := `{"return_value": 1}` params := []string{"D7"} a := initTestAdaptor() 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) }
// Test internal sendCommandDelayGetResponse func TestSHT3xDriverSCDGRIoFailures(t *testing.T) { sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor() invalidRead := errors.New("Read error") invalidWrite := errors.New("Write error") // Only send 5 bytes adaptor.i2cReadImpl = func() ([]byte, error) { return []byte{0xbe, 0xef, 0x92, 0xbe, 0xef}, nil } _, err := sht3x.sendCommandDelayGetResponse(nil, nil, 2) gobottest.Assert(t, err, ErrNotEnoughBytes) // Don't read any bytes and return an error adaptor.i2cReadImpl = func() ([]byte, error) { return nil, invalidRead } _, err = sht3x.sendCommandDelayGetResponse(nil, nil, 1) gobottest.Assert(t, err, invalidRead) // Don't write any bytes and return an error adaptor.i2cWriteImpl = func() error { return invalidWrite } _, err = sht3x.sendCommandDelayGetResponse(nil, nil, 1) gobottest.Assert(t, err, invalidWrite) }
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")) }
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) }
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()) }
func TestLedDriver(t *testing.T) { var err interface{} d := initTestLedDriver(newGpioTestAdaptor()) gobottest.Assert(t, d.Pin(), "1") gobottest.Refute(t, d.Connection(), nil) 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("write error")) err = d.Command("On")(nil) gobottest.Assert(t, err.(error), errors.New("write error")) err = d.Command("Off")(nil) gobottest.Assert(t, err.(error), errors.New("write error")) err = d.Command("Brightness")(map[string]interface{}{"level": 100.0}) gobottest.Assert(t, err.(error), errors.New("pwm error")) }
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) }
func TestLedDriverToggle(t *testing.T) { d := initTestLedDriver(newGpioTestAdaptor()) d.Off() d.Toggle() gobottest.Assert(t, d.State(), true) d.Toggle() gobottest.Assert(t, d.State(), false) }
func TestAdaptorFinalize(t *testing.T) { a := initTestAdaptor() gobottest.Assert(t, a.Finalize(), nil) a = initTestAdaptor() a.board.(*mockFirmataBoard).disconnectError = errors.New("close error") gobottest.Assert(t, a.Finalize(), errors.New("close error")) }
func TestLeapMotionDriverStart(t *testing.T) { d := initTestLeapMotionDriver() gobottest.Assert(t, d.Start(), nil) d = initTestLeapMotionDriver() writeError = errors.New("write error") gobottest.Assert(t, d.Start(), errors.New("write error")) }
func TestMPU6050Driver(t *testing.T) { mpu := initTestMPU6050Driver() gobottest.Refute(t, mpu.Connection(), nil) gobottest.Assert(t, mpu.interval, 10*time.Millisecond) mpu = NewMPU6050Driver(newI2cTestAdaptor(), 100*time.Millisecond) gobottest.Assert(t, mpu.interval, 100*time.Millisecond) }
func TestSetHatAddresses(t *testing.T) { ada, _ := initTestAdafruitMotorHatDriverWithStubbedAdaptor() motorHatAddr := 0x61 servoHatAddr := 0x41 gobottest.Assert(t, ada.SetMotorHatAddress(motorHatAddr), nil) gobottest.Assert(t, ada.SetServoHatAddress(servoHatAddr), nil) }
func TestWiichuckDriver(t *testing.T) { wii := initTestWiichuckDriver() gobottest.Refute(t, wii.Connection(), nil) gobottest.Assert(t, wii.interval, 10*time.Millisecond) wii = NewWiichuckDriver(newI2cTestAdaptor(), 100*time.Millisecond) gobottest.Assert(t, wii.interval, 100*time.Millisecond) }
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)) }
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) }