gobot.io/x/gobot/v2@v2.1.0/platforms/dragonboard/dragonboard_adaptor_test.go (about) 1 package dragonboard 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 "testing" 8 9 "gobot.io/x/gobot/v2" 10 "gobot.io/x/gobot/v2/drivers/gpio" 11 "gobot.io/x/gobot/v2/drivers/i2c" 12 "gobot.io/x/gobot/v2/gobottest" 13 ) 14 15 // make sure that this Adaptor fulfills all the required interfaces 16 var _ gobot.Adaptor = (*Adaptor)(nil) 17 var _ gobot.DigitalPinnerProvider = (*Adaptor)(nil) 18 var _ gpio.DigitalReader = (*Adaptor)(nil) 19 var _ gpio.DigitalWriter = (*Adaptor)(nil) 20 var _ i2c.Connector = (*Adaptor)(nil) 21 22 func initTestAdaptor(t *testing.T) *Adaptor { 23 a := NewAdaptor() 24 if err := a.Connect(); err != nil { 25 panic(err) 26 } 27 return a 28 } 29 30 func TestName(t *testing.T) { 31 a := initTestAdaptor(t) 32 gobottest.Assert(t, strings.HasPrefix(a.Name(), "DragonBoard"), true) 33 a.SetName("NewName") 34 gobottest.Assert(t, a.Name(), "NewName") 35 } 36 37 func TestDigitalIO(t *testing.T) { 38 a := initTestAdaptor(t) 39 mockPaths := []string{ 40 "/sys/class/gpio/export", 41 "/sys/class/gpio/unexport", 42 "/sys/class/gpio/gpio36/value", 43 "/sys/class/gpio/gpio36/direction", 44 "/sys/class/gpio/gpio12/value", 45 "/sys/class/gpio/gpio12/direction", 46 } 47 fs := a.sys.UseMockFilesystem(mockPaths) 48 49 _ = a.DigitalWrite("GPIO_B", 1) 50 gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio12/value"].Contents, "1") 51 52 fs.Files["/sys/class/gpio/gpio36/value"].Contents = "1" 53 i, _ := a.DigitalRead("GPIO_A") 54 gobottest.Assert(t, i, 1) 55 56 gobottest.Assert(t, a.DigitalWrite("GPIO_M", 1), errors.New("'GPIO_M' is not a valid id for a digital pin")) 57 gobottest.Assert(t, a.Finalize(), nil) 58 } 59 60 func TestFinalizeErrorAfterGPIO(t *testing.T) { 61 a := initTestAdaptor(t) 62 mockPaths := []string{ 63 "/sys/class/gpio/export", 64 "/sys/class/gpio/unexport", 65 "/sys/class/gpio/gpio36/value", 66 "/sys/class/gpio/gpio36/direction", 67 "/sys/class/gpio/gpio12/value", 68 "/sys/class/gpio/gpio12/direction", 69 } 70 fs := a.sys.UseMockFilesystem(mockPaths) 71 72 gobottest.Assert(t, a.Connect(), nil) 73 gobottest.Assert(t, a.DigitalWrite("GPIO_B", 1), nil) 74 75 fs.WithWriteError = true 76 77 err := a.Finalize() 78 gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true) 79 } 80 81 func TestI2cDefaultBus(t *testing.T) { 82 a := initTestAdaptor(t) 83 gobottest.Assert(t, a.DefaultI2cBus(), 0) 84 } 85 86 func TestI2cFinalizeWithErrors(t *testing.T) { 87 // arrange 88 a := NewAdaptor() 89 a.sys.UseMockSyscall() 90 fs := a.sys.UseMockFilesystem([]string{"/dev/i2c-1"}) 91 gobottest.Assert(t, a.Connect(), nil) 92 con, err := a.GetI2cConnection(0xff, 1) 93 gobottest.Assert(t, err, nil) 94 _, err = con.Write([]byte{0xbf}) 95 gobottest.Assert(t, err, nil) 96 fs.WithCloseError = true 97 // act 98 err = a.Finalize() 99 // assert 100 gobottest.Assert(t, strings.Contains(err.Error(), "close error"), true) 101 } 102 103 func Test_validateI2cBusNumber(t *testing.T) { 104 var tests = map[string]struct { 105 busNr int 106 wantErr error 107 }{ 108 "number_negative_error": { 109 busNr: -1, 110 wantErr: fmt.Errorf("Bus number -1 out of range"), 111 }, 112 "number_0_ok": { 113 busNr: 0, 114 }, 115 "number_1_ok": { 116 busNr: 1, 117 }, 118 "number_2_error": { 119 busNr: 2, 120 wantErr: fmt.Errorf("Bus number 2 out of range"), 121 }, 122 } 123 for name, tc := range tests { 124 t.Run(name, func(t *testing.T) { 125 // arrange 126 a := NewAdaptor() 127 // act 128 err := a.validateI2cBusNumber(tc.busNr) 129 // assert 130 gobottest.Assert(t, err, tc.wantErr) 131 }) 132 } 133 }