gobot.io/x/gobot@v1.16.0/robot_test.go (about) 1 package gobot 2 3 import ( 4 "testing" 5 "time" 6 7 "gobot.io/x/gobot/gobottest" 8 ) 9 10 func TestRobotConnectionEach(t *testing.T) { 11 r := newTestRobot("Robot1") 12 13 i := 0 14 r.Connections().Each(func(conn Connection) { 15 i++ 16 }) 17 gobottest.Assert(t, r.Connections().Len(), i) 18 } 19 20 func TestRobotToJSON(t *testing.T) { 21 r := newTestRobot("Robot99") 22 r.AddCommand("test_function", func(params map[string]interface{}) interface{} { 23 return nil 24 }) 25 json := NewJSONRobot(r) 26 gobottest.Assert(t, len(json.Devices), r.Devices().Len()) 27 gobottest.Assert(t, len(json.Commands), len(r.Commands())) 28 } 29 30 func TestRobotDevicesToJSON(t *testing.T) { 31 r := newTestRobot("Robot99") 32 json := NewJSONRobot(r) 33 gobottest.Assert(t, len(json.Devices), r.Devices().Len()) 34 gobottest.Assert(t, json.Devices[0].Name, "Device1") 35 gobottest.Assert(t, json.Devices[0].Driver, "*gobot.testDriver") 36 gobottest.Assert(t, json.Devices[0].Connection, "Connection1") 37 gobottest.Assert(t, len(json.Devices[0].Commands), 1) 38 } 39 40 func TestRobotStart(t *testing.T) { 41 r := newTestRobot("Robot99") 42 gobottest.Assert(t, r.Start(), nil) 43 gobottest.Assert(t, r.Stop(), nil) 44 gobottest.Assert(t, r.Running(), false) 45 } 46 47 func TestRobotStartAutoRun(t *testing.T) { 48 adaptor1 := newTestAdaptor("Connection1", "/dev/null") 49 driver1 := newTestDriver(adaptor1, "Device1", "0") 50 //work := func() {} 51 r := NewRobot("autorun", 52 []Connection{adaptor1}, 53 []Device{driver1}, 54 //work, 55 ) 56 57 go func() { 58 gobottest.Assert(t, r.Start(), nil) 59 }() 60 61 time.Sleep(10 * time.Millisecond) 62 gobottest.Assert(t, r.Running(), true) 63 64 // stop it 65 gobottest.Assert(t, r.Stop(), nil) 66 gobottest.Assert(t, r.Running(), false) 67 }