예제 #1
0
파일: api_test.go 프로젝트: ympons/gobot
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)
}
예제 #2
0
func TestDirectPinDriverDigitalWrite(t *testing.T) {
	d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
	gobottest.Refute(t, d.DigitalWrite(1), nil)

	d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
	gobottest.Assert(t, d.DigitalWrite(1), ErrDigitalWriteUnsupported)
}
예제 #3
0
func TestSparkCoreAdaptorSetAPIServer(t *testing.T) {
	a := initTestSparkCoreAdaptor()
	apiServer := "new_api_server"
	gobottest.Refute(t, a.APIServer, apiServer)

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

	a = NewEdisonAdaptor("myAdaptor")
	sysfs.SetFilesystem(sysfs.NewMockFilesystem([]string{}))
	gobottest.Refute(t, len(a.Connect()), 0)
}
예제 #5
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, err := fs.OpenFile("bar", 0, 0666)
	gobottest.Refute(t, f4.Fd(), f1.Fd())
}
예제 #6
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)

}
예제 #7
0
파일: cors_test.go 프로젝트: nathany/gobot
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)
}
예제 #8
0
func TestDigisparkAdaptorDigitalWrite(t *testing.T) {
	a := initTestDigisparkAdaptor()
	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"))
}
예제 #9
0
func TestEdisonAdaptorFinalize(t *testing.T) {
	a, _ := initTestEdisonAdaptor()
	a.DigitalWrite("3", 1)
	a.PwmWrite("5", 100)

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

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

	closeErr = errors.New("close error")
	sysfs.SetFilesystem(sysfs.NewMockFilesystem([]string{}))
	gobottest.Refute(t, len(a.Finalize()), 0)
}
예제 #10
0
func TestEventer(t *testing.T) {
	e := NewEventer()
	e.AddEvent("test")

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

	event := e.Event("test")
	gobottest.Refute(t, event, nil)

	event = e.Event("booyeah")
	gobottest.Assert(t, event, (*Event)(nil))
}
예제 #11
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))
}
예제 #12
0
func TestCameraDriverStart(t *testing.T) {
	sem := make(chan bool)
	d := initTestCameraDriver()
	gobottest.Assert(t, len(d.Start()), 0)
	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("bot", "")
	gobottest.Assert(t, len(d.Start()), 0)

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

}
예제 #13
0
func TestParseNotArrowKeys(t *testing.T) {
	gobottest.Refute(t, Parse(bytes{27, 91, 65}).Key, Escape)
	gobottest.Refute(t, Parse(bytes{27, 91, 70}).Key, 70)
}
예제 #14
0
func TestParseNotAlphaKeys(t *testing.T) {
	gobottest.Refute(t, Parse(bytes{132, 0, 0}).Key, 132)
}
예제 #15
0
func TestParseNotEscape(t *testing.T) {
	gobottest.Refute(t, Parse(bytes{27, 91, 65}).Key, Escape)
}
예제 #16
0
func ExampleRefute() {
	t := &testing.T{}
	var a int = 100
	var b int = 200
	gobottest.Refute(t, a, b)
}
예제 #17
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"))
}
예제 #18
0
func TestAudioAdaptorCommandsUnknown(t *testing.T) {
	cmd, err := CommandName("whatever.unk")
	gobottest.Refute(t, cmd, "mpg123")
	gobottest.Assert(t, err.Error(), "Unknown filetype for audio file.")
}