예제 #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)
	gobot.Refute(t, len(rect), 0)
	gobot.Refute(t, DrawRectangles(image, rect, 0, 0, 0, 0), nil)
}
예제 #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)
	gobot.Refute(t, body["MCP"].(map[string]interface{})["robots"], nil)
	gobot.Refute(t, body["MCP"].(map[string]interface{})["commands"], nil)
}
예제 #3
0
func TestDirectPinDriverPwmWrite(t *testing.T) {
	d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
	gobot.Refute(t, d.PwmWrite(1), nil)

	d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
	gobot.Assert(t, d.PwmWrite(1), ErrPwmWriteUnsupported)
}
예제 #4
0
func TestSparkCoreAdaptorSetAPIServer(t *testing.T) {
	a := initTestSparkCoreAdaptor()
	apiServer := "new_api_server"
	gobot.Refute(t, a.APIServer, apiServer)

	a.setAPIServer(apiServer)
	gobot.Assert(t, a.APIServer, apiServer)
}
예제 #5
0
func TestEdisonAdaptorConnect(t *testing.T) {
	a, _ := initTestEdisonAdaptor()
	gobot.Assert(t, len(a.Connect()), 0)

	a = NewEdisonAdaptor("myAdaptor")
	sysfs.SetFilesystem(sysfs.NewMockFilesystem([]string{}))
	gobot.Refute(t, len(a.Connect()), 0)
}
예제 #6
0
func TestMockFilesystemOpen(t *testing.T) {
	fs := NewMockFilesystem([]string{"foo"})
	f1 := fs.Files["foo"]

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

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

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

	fs.Add("bar")
	f4, err := fs.OpenFile("bar", 0, 0666)
	gobot.Refute(t, f4.Fd(), f1.Fd())
}
예제 #7
0
func TestEdisonAdaptorFinalize(t *testing.T) {
	a, _ := initTestEdisonAdaptor()
	a.DigitalWrite("3", 1)
	a.PwmWrite("5", 100)
	a.i2cDevice = &NullReadWriteCloser{}
	gobot.Assert(t, len(a.Finalize()), 0)

	closeErr = errors.New("close error")
	sysfs.SetFilesystem(sysfs.NewMockFilesystem([]string{}))
	gobot.Refute(t, len(a.Finalize()), 0)
}
예제 #8
0
func TestNewI2cDevice(t *testing.T) {
	fs := NewMockFilesystem([]string{})
	SetFilesystem(fs)

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

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

	SetFilesystem(fs)

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

	SetSyscall(&MockSyscall{})

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

	gobot.Assert(t, err, nil)

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

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

	n, err := i.Write(buf)

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

	buf = make([]byte, 4)

	n, err = i.Read(buf)

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

}
예제 #9
0
func TestNewI2cDevice(t *testing.T) {
	fs := NewMockFilesystem([]string{})
	SetFilesystem(fs)

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

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

	SetFilesystem(fs)

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

	SetSyscall(&MockSyscall{})

	i, err = NewI2cDevice("/dev/i2c-1", 0xff)
	gobot.Assert(t, err, nil)
	var _ io.ReadWriteCloser = i
}
예제 #10
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)
	gobot.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)
	gobot.Refute(t, response.Header()["Access-Control-Allow-Origin"], disallowedOrigin)
	gobot.Refute(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)
}
예제 #11
0
func TestEdisonAdaptorFinalize(t *testing.T) {
	a, _ := initTestEdisonAdaptor()
	a.DigitalWrite("3", 1)
	a.PwmWrite("5", 100)

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

	gobot.Assert(t, len(a.Finalize()), 0)

	closeErr = errors.New("close error")
	sysfs.SetFilesystem(sysfs.NewMockFilesystem([]string{}))
	gobot.Refute(t, len(a.Finalize()), 0)
}
예제 #12
0
func TestDigisparkAdaptorDigitalWrite(t *testing.T) {
	a := initTestDigisparkAdaptor()
	err := a.DigitalWrite("0", uint8(1))
	gobot.Assert(t, err, nil)
	gobot.Assert(t, a.littleWire.(*mock).pin, uint8(0))
	gobot.Assert(t, a.littleWire.(*mock).state, uint8(1))

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

	errorFunc = func() error { return errors.New("pin mode error") }
	err = a.DigitalWrite("0", uint8(1))
	gobot.Assert(t, err, errors.New("pin mode error"))
}
예제 #13
0
func TestCameraDriverStart(t *testing.T) {
	sem := make(chan bool)
	d := initTestCameraDriver()
	gobot.Assert(t, len(d.Start()), 0)
	gobot.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("bot", "")
	gobot.Assert(t, len(d.Start()), 0)

	d = NewCameraDriver("bot", true)
	gobot.Refute(t, len(d.Start()), 0)

}
예제 #14
0
func ExampleRefute() {
	t := &testing.T{}
	var a int = 100
	var b int = 200
	gobot.Refute(t, a, b)
}
예제 #15
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)
	gobot.Assert(t, pin.pin, "10")
	gobot.Assert(t, pin.label, "custom")

	pin = NewDigitalPin(10).(*digitalPin)
	gobot.Assert(t, pin.label, "gpio10")

	pin.Unexport()
	gobot.Assert(t, fs.Files["/sys/class/gpio/unexport"].Contents, "10")

	pin.Export()
	gobot.Assert(t, fs.Files["/sys/class/gpio/unexport"].Contents, "10")

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

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

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

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

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

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

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

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

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

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

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

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

	err = pin.Export()
	gobot.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
}