Exemple #1
0
func TestUtils(t *testing.T) {
	_, currentfile, _, _ := runtime.Caller(0)
	image := cv.LoadImage(path.Join(path.Dir(currentfile), "lena-256x256.jpg"))
	rect := DetectFaces("haarcascade_frontalface_alt.xml", image)
	gobottest.Refute(t, len(rect), 0)
	gobottest.Refute(t, DrawRectangles(image, rect, 0, 0, 0, 0), nil)
}
Exemple #2
0
func TestMcp(t *testing.T) {
	a := initTestAPI()
	request, _ := http.NewRequest("GET", "/api/", nil)
	response := httptest.NewRecorder()
	a.ServeHTTP(response, request)

	var body map[string]interface{}
	json.NewDecoder(response.Body).Decode(&body)
	gobottest.Refute(t, body["MCP"].(map[string]interface{})["robots"], nil)
	gobottest.Refute(t, body["MCP"].(map[string]interface{})["commands"], nil)
}
func TestDirectPinDriverPwmWrite(t *testing.T) {
	d := initTestDirectPinDriver(newGpioTestAdaptor())
	gobottest.Refute(t, d.PwmWrite(1), nil)

	d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
	gobottest.Assert(t, d.PwmWrite(1), ErrPwmWriteUnsupported)
}
Exemple #4
0
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 TestButtonDriver(t *testing.T) {
	d := NewButtonDriver(newGpioTestAdaptor(), "1")
	gobottest.Refute(t, d.Connection(), nil)

	d = NewButtonDriver(newGpioTestAdaptor(), "1", 30*time.Second)
	gobottest.Assert(t, d.interval, 30*time.Second)
}
func TestRgbLedDriver(t *testing.T) {
	var err interface{}

	d := initTestRgbLedDriver(newGpioTestAdaptor())

	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.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("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"))

}
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 TestAdaptorConnect(t *testing.T) {
	a, _ := initTestAdaptor()
	gobottest.Assert(t, a.Connect(), nil)

	a = NewAdaptor()
	sysfs.SetFilesystem(sysfs.NewMockFilesystem([]string{}))
	gobottest.Refute(t, a.Connect(), 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 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)
}
Exemple #11
0
func TestAdaptorSetAPIServer(t *testing.T) {
	a := initTestAdaptor()
	apiServer := "new_api_server"
	gobottest.Refute(t, a.APIServer, apiServer)

	a.setAPIServer(apiServer)
	gobottest.Assert(t, a.APIServer, apiServer)
}
func TestMakeyButtonDriver(t *testing.T) {
	d := initTestMakeyButtonDriver()
	gobottest.Assert(t, d.Pin(), "1")
	gobottest.Refute(t, d.Connection(), nil)
	gobottest.Assert(t, d.interval, 10*time.Millisecond)

	d = NewMakeyButtonDriver(newGpioTestAdaptor(), "1", 30*time.Second)
	gobottest.Assert(t, d.interval, MAKEY_TEST_DELAY*time.Second)
}
Exemple #13
0
func TestMockFilesystemOpen(t *testing.T) {
	fs := NewMockFilesystem([]string{"foo"})
	f1 := fs.Files["foo"]

	gobottest.Assert(t, f1.Opened, false)
	f2, err := fs.OpenFile("foo", 0, 0666)
	gobottest.Assert(t, f1, f2)
	gobottest.Assert(t, err, nil)

	err = f2.Sync()
	gobottest.Assert(t, err, nil)

	_, err = fs.OpenFile("bar", 0, 0666)
	gobottest.Refute(t, err, nil)

	fs.Add("bar")
	f4, _ := fs.OpenFile("bar", 0, 0666)
	gobottest.Refute(t, f4.Fd(), f1.Fd())
}
func TestNewMCP23017Driver(t *testing.T) {
	var bm interface{} = NewMCP23017Driver(newMcpI2cTestAdaptor(), MCP23017Config{}, 0x20)
	_, ok := bm.(*MCP23017Driver)
	if !ok {
		t.Errorf("NewMCP23017Driver() should have returned a *MCP23017Driver")
	}

	b := NewMCP23017Driver(newMcpI2cTestAdaptor(), MCP23017Config{}, 0x20)
	gobottest.Refute(t, b.Connection(), nil)
}
func TestAudioDriver(t *testing.T) {
	d := NewDriver(NewAdaptor(), "../../examples/laser.mp3")

	gobottest.Assert(t, d.Filename(), "../../examples/laser.mp3")

	gobottest.Refute(t, d.Connection(), nil)

	gobottest.Assert(t, d.Start(), nil)

	gobottest.Assert(t, d.Halt(), nil)
}
func TestNewHMC6352Driver(t *testing.T) {
	// Does it return a pointer to an instance of HMC6352Driver?
	var bm interface{} = NewHMC6352Driver(newI2cTestAdaptor())
	_, ok := bm.(*HMC6352Driver)
	if !ok {
		t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver")
	}

	b := NewHMC6352Driver(newI2cTestAdaptor())
	gobottest.Refute(t, b.Connection(), nil)
}
func TestMavlinkDriver(t *testing.T) {
	m := NewAdaptor("/dev/null")
	m.sp = nullReadWriteCloser{}
	m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }

	d := NewDriver(m)
	gobottest.Refute(t, d.Connection(), nil)
	gobottest.Assert(t, d.interval, 10*time.Millisecond)

	d = NewDriver(m, 100*time.Millisecond)
	gobottest.Assert(t, d.interval, 100*time.Millisecond)
}
Exemple #18
0
func TestNewI2cDevice(t *testing.T) {
	fs := NewMockFilesystem([]string{})
	SetFilesystem(fs)

	i, err := NewI2cDevice(os.DevNull, 0xff)
	gobottest.Refute(t, err, nil)

	fs = NewMockFilesystem([]string{
		"/dev/i2c-1",
	})

	SetFilesystem(fs)

	i, err = NewI2cDevice("/dev/i2c-1", 0xff)
	gobottest.Refute(t, err, nil)

	SetSyscall(&MockSyscall{})

	i, err = NewI2cDevice("/dev/i2c-1", 0xff)
	var _ I2cDevice = i

	gobottest.Assert(t, err, nil)

	gobottest.Assert(t, i.SetAddress(0xff), nil)

	buf := []byte{0x01, 0x02, 0x03}

	n, err := i.Write(buf)

	gobottest.Assert(t, n, len(buf))
	gobottest.Assert(t, err, nil)

	buf = make([]byte, 4)

	n, err = i.Read(buf)

	gobottest.Assert(t, n, 3)
	gobottest.Assert(t, err, nil)

}
Exemple #19
0
func TestCORS(t *testing.T) {
	api := initTestAPI()

	// Accepted origin
	allowedOrigin := []string{"http://server.com"}
	api.AddHandler(AllowRequestsFrom(allowedOrigin[0]))

	request, _ := http.NewRequest("GET", "/api/", nil)
	request.Header.Set("Origin", allowedOrigin[0])
	response := httptest.NewRecorder()
	api.ServeHTTP(response, request)
	gobottest.Assert(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)

	// Not accepted Origin
	disallowedOrigin := []string{"http://disallowed.com"}
	request, _ = http.NewRequest("GET", "/api/", nil)
	request.Header.Set("Origin", disallowedOrigin[0])
	response = httptest.NewRecorder()
	api.ServeHTTP(response, request)
	gobottest.Refute(t, response.Header()["Access-Control-Allow-Origin"], disallowedOrigin)
	gobottest.Refute(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)
}
func TestAdaptorDigitalWrite(t *testing.T) {
	a := initTestAdaptor()
	err := a.DigitalWrite("0", uint8(1))
	gobottest.Assert(t, err, nil)
	gobottest.Assert(t, a.littleWire.(*mock).pin, uint8(0))
	gobottest.Assert(t, a.littleWire.(*mock).state, uint8(1))

	err = a.DigitalWrite("?", uint8(1))
	gobottest.Refute(t, err, nil)

	errorFunc = func() error { return errors.New("pin mode error") }
	err = a.DigitalWrite("0", uint8(1))
	gobottest.Assert(t, err, errors.New("pin mode error"))
}
func TestAdaptorFinalize(t *testing.T) {
	a, _ := initTestAdaptor()
	a.DigitalWrite("1", 1)
	a.PwmWrite("25", 100)

	sysfs.SetSyscall(&sysfs.MockSyscall{})
	a.I2cStart(0xff)

	gobottest.Assert(t, a.Finalize(), nil)

	closeErr = errors.New("close error")
	sysfs.SetFilesystem(sysfs.NewMockFilesystem([]string{}))
	gobottest.Refute(t, a.Finalize(), nil)
}
Exemple #22
0
func TestCommaner(t *testing.T) {
	c := NewCommander()
	c.AddCommand("test", func(map[string]interface{}) interface{} {
		return "hi"
	})

	if _, ok := c.Commands()["test"]; !ok {
		t.Errorf("Could not add command to list of Commands")
	}

	command := c.Command("test")
	gobottest.Refute(t, command, nil)

	command = c.Command("booyeah")
	gobottest.Assert(t, command, (func(map[string]interface{}) interface{})(nil))
}
func TestAnalogSensorDriver(t *testing.T) {
	d := NewAnalogSensorDriver(newAioTestAdaptor(), "1")
	gobottest.Refute(t, d.Connection(), nil)
	// default interval
	gobottest.Assert(t, d.interval, 10*time.Millisecond)

	d = NewAnalogSensorDriver(newAioTestAdaptor(), "42", 30*time.Second)
	gobottest.Assert(t, d.Pin(), "42")
	gobottest.Assert(t, d.interval, 30*time.Second)

	testAdaptorAnalogRead = func() (val int, err error) {
		val = 100
		return
	}
	ret := d.Command("Read")(nil).(map[string]interface{})

	gobottest.Assert(t, ret["val"].(int), 100)
	gobottest.Assert(t, ret["err"], nil)
}
func TestCameraDriverStart(t *testing.T) {
	sem := make(chan bool)
	d := initTestCameraDriver()
	gobottest.Assert(t, d.Start(), nil)
	d.On(d.Event("frame"), func(data interface{}) {
		sem <- true
	})
	select {
	case <-sem:
	case <-time.After(100 * time.Millisecond):
		t.Errorf("Event \"frame\" was not published")
	}

	d = NewCameraDriver("")
	gobottest.Assert(t, d.Start(), nil)

	d = NewCameraDriver(true)
	gobottest.Refute(t, d.Start(), nil)
}
func TestDirectPinDriver(t *testing.T) {
	var ret map[string]interface{}
	var err interface{}

	d := initTestDirectPinDriver(newGpioTestAdaptor())
	gobottest.Assert(t, d.Pin(), "1")
	gobottest.Refute(t, d.Connection(), nil)

	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"))

	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"))
}
// Test Heater and getStatusRegister
func TestSHT3xDriverHeater(t *testing.T) {
	sht3x, adaptor := initTestSHT3xDriverWithStubbedAdaptor()

	// heater enabled
	adaptor.i2cReadImpl = func() ([]byte, error) {
		return []byte{0x20, 0x00, 0x5d}, nil
	}

	status, err := sht3x.Heater()
	gobottest.Assert(t, err, nil)
	gobottest.Assert(t, status, true)

	// heater disabled
	adaptor.i2cReadImpl = func() ([]byte, error) {
		return []byte{0x00, 0x00, 0x81}, nil
	}

	status, err = sht3x.Heater()
	gobottest.Assert(t, err, nil)
	gobottest.Assert(t, status, false)

	// heater crc failed
	adaptor.i2cReadImpl = func() ([]byte, error) {
		return []byte{0x00, 0x00, 0x00}, nil
	}

	status, err = sht3x.Heater()
	gobottest.Assert(t, err, ErrInvalidCrc)

	// heater read failed
	adaptor.i2cReadImpl = func() ([]byte, error) {
		return []byte{0x00, 0x00}, nil
	}

	status, err = sht3x.Heater()
	gobottest.Refute(t, err, nil)
}
Exemple #27
0
func TestDigitalPin(t *testing.T) {
	fs := NewMockFilesystem([]string{
		"/sys/class/gpio/export",
		"/sys/class/gpio/unexport",
		"/sys/class/gpio/gpio10/value",
		"/sys/class/gpio/gpio10/direction",
	})

	SetFilesystem(fs)

	pin := NewDigitalPin(10, "custom").(*digitalPin)
	gobottest.Assert(t, pin.pin, "10")
	gobottest.Assert(t, pin.label, "custom")

	pin = NewDigitalPin(10).(*digitalPin)
	gobottest.Assert(t, pin.pin, "10")
	gobottest.Assert(t, pin.label, "gpio10")
	gobottest.Assert(t, pin.value, nil)

	err := pin.Unexport()
	gobottest.Assert(t, err, nil)
	gobottest.Assert(t, fs.Files["/sys/class/gpio/unexport"].Contents, "10")

	err = pin.Export()
	gobottest.Assert(t, err, nil)
	gobottest.Assert(t, fs.Files["/sys/class/gpio/export"].Contents, "10")
	gobottest.Refute(t, pin.value, nil)

	err = pin.Write(1)
	gobottest.Assert(t, err, nil)
	gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio10/value"].Contents, "1")

	err = pin.Direction(IN)
	gobottest.Assert(t, err, nil)
	gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio10/direction"].Contents, "in")

	data, _ := pin.Read()
	gobottest.Assert(t, 1, data)

	pin2 := NewDigitalPin(30, "custom")
	err = pin2.Write(1)
	gobottest.Refute(t, err, nil)

	data, err = pin2.Read()
	gobottest.Refute(t, err, nil)
	gobottest.Assert(t, data, 0)

	writeFile = func(File, []byte) (int, error) {
		return 0, &os.PathError{Err: syscall.EINVAL}
	}

	err = pin.Unexport()
	gobottest.Assert(t, err, nil)

	writeFile = func(File, []byte) (int, error) {
		return 0, &os.PathError{Err: errors.New("write error")}
	}

	err = pin.Unexport()
	gobottest.Assert(t, err.(*os.PathError).Err, errors.New("write error"))

	writeFile = func(File, []byte) (int, error) {
		return 0, &os.PathError{Err: syscall.EBUSY}
	}

	err = pin.Export()
	gobottest.Assert(t, err, nil)

	writeFile = func(File, []byte) (int, error) {
		return 0, &os.PathError{Err: errors.New("write error")}
	}

	err = pin.Export()
	gobottest.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
}
func TestBMP180Driver(t *testing.T) {
	bmp180 := initTestBMP180Driver()
	gobottest.Refute(t, bmp180.Connection(), nil)
}
func TestL3GD20HDriver(t *testing.T) {
	d := initTestL3GD20HDriver()
	gobottest.Refute(t, d.Connection(), nil)
}
Exemple #30
0
func ExampleRefute() {
	t := &testing.T{}
	var a int = 100
	var b int = 200
	gobottest.Refute(t, a, b)
}