gobot.io/x/gobot/v2@v2.1.0/drivers/gpio/tm1638_driver_test.go (about) 1 package gpio 2 3 import ( 4 "strings" 5 "testing" 6 7 "gobot.io/x/gobot/v2" 8 "gobot.io/x/gobot/v2/gobottest" 9 ) 10 11 var _ gobot.Driver = (*TM1638Driver)(nil) 12 13 // --------- HELPERS 14 func initTestTM1638Driver() (driver *TM1638Driver) { 15 driver, _ = initTestTM1638DriverWithStubbedAdaptor() 16 return 17 } 18 19 func initTestTM1638DriverWithStubbedAdaptor() (*TM1638Driver, *gpioTestAdaptor) { 20 adaptor := newGpioTestAdaptor() 21 return NewTM1638Driver(adaptor, "1", "2", "3"), adaptor 22 } 23 24 // --------- TESTS 25 func TestTM1638Driver(t *testing.T) { 26 var a interface{} = initTestTM1638Driver() 27 _, ok := a.(*TM1638Driver) 28 if !ok { 29 t.Errorf("NewTM1638Driver() should have returned a *TM1638Driver") 30 } 31 } 32 33 func TestTM1638DriverStart(t *testing.T) { 34 d := initTestTM1638Driver() 35 gobottest.Assert(t, d.Start(), nil) 36 } 37 38 func TestTM1638DriverHalt(t *testing.T) { 39 d := initTestTM1638Driver() 40 gobottest.Assert(t, d.Halt(), nil) 41 } 42 43 func TestTM1638DriverDefaultName(t *testing.T) { 44 d := initTestTM1638Driver() 45 gobottest.Assert(t, strings.HasPrefix(d.Name(), "TM1638"), true) 46 } 47 48 func TestTM1638DriverSetName(t *testing.T) { 49 d := initTestTM1638Driver() 50 d.SetName("mybot") 51 gobottest.Assert(t, d.Name(), "mybot") 52 } 53 54 func TestTM1638DriverFromStringToByteArray(t *testing.T) { 55 d := initTestTM1638Driver() 56 data := d.fromStringToByteArray("Hello World") 57 gobottest.Assert(t, []byte{0x76, 0x7B, 0x30, 0x30, 0x5C, 0x00, 0x1D, 0x5C, 0x50, 0x30, 0x5E}, data) 58 } 59 60 func TestTM1638DriverAddFonts(t *testing.T) { 61 d := initTestTM1638Driver() 62 d.AddFonts(map[string]byte{"µ": 0x1C, "ß": 0x7F}) 63 data := d.fromStringToByteArray("µß") 64 gobottest.Assert(t, []byte{0x1C, 0x7F}, data) 65 } 66 67 func TestTM1638DriverClearFonts(t *testing.T) { 68 d := initTestTM1638Driver() 69 d.ClearFonts() 70 data := d.fromStringToByteArray("Hello World") 71 gobottest.Assert(t, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, data) 72 }