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