gobot.io/x/gobot/v2@v2.1.0/system/digitalpin_access_test.go (about)

     1  package system
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gobot.io/x/gobot/v2/gobottest"
     7  )
     8  
     9  func Test_isSupportedSysfs(t *testing.T) {
    10  	// arrange
    11  	dpa := sysfsDigitalPinAccess{}
    12  	// act
    13  	got := dpa.isSupported()
    14  	// assert
    15  	gobottest.Assert(t, got, true)
    16  }
    17  
    18  func Test_isSupportedGpiod(t *testing.T) {
    19  	var tests = map[string]struct {
    20  		mockPaths []string
    21  		want      bool
    22  	}{
    23  		"supported": {
    24  			mockPaths: []string{"/sys/class/gpio/", "/dev/gpiochip3"},
    25  			want:      true,
    26  		},
    27  		"not_supported": {
    28  			mockPaths: []string{"/sys/class/gpio/"},
    29  			want:      false,
    30  		},
    31  	}
    32  	for name, tc := range tests {
    33  		t.Run(name, func(t *testing.T) {
    34  			// arrange
    35  			fs := newMockFilesystem(tc.mockPaths)
    36  			dpa := gpiodDigitalPinAccess{fs: fs}
    37  			// act
    38  			got := dpa.isSupported()
    39  			// assert
    40  			gobottest.Assert(t, got, tc.want)
    41  		})
    42  	}
    43  }
    44  
    45  func Test_createAsSysfs(t *testing.T) {
    46  	// arrange
    47  	dpa := sysfsDigitalPinAccess{}
    48  	// act
    49  	dp := dpa.createPin("chip", 8)
    50  	// assert
    51  	gobottest.Refute(t, dp, nil)
    52  	dps := dp.(*digitalPinSysfs)
    53  	// chip is dropped
    54  	gobottest.Assert(t, dps.label, "gpio8")
    55  }
    56  
    57  func Test_createAsGpiod(t *testing.T) {
    58  	// arrange
    59  	const (
    60  		pin   = 18
    61  		label = "gobotio18"
    62  		chip  = "gpiochip1"
    63  	)
    64  	dpa := gpiodDigitalPinAccess{}
    65  	// act
    66  	dp := dpa.createPin(chip, 18)
    67  	// assert
    68  	gobottest.Refute(t, dp, nil)
    69  	dpg := dp.(*digitalPinGpiod)
    70  	gobottest.Assert(t, dpg.label, label)
    71  	gobottest.Assert(t, dpg.chipName, chip)
    72  }
    73  
    74  func Test_createPinWithOptionsSysfs(t *testing.T) {
    75  	// This is a general test, that options are applied by using "create" with the WithPinLabel() option.
    76  	// All other configuration options will be tested in tests for "digitalPinConfig".
    77  	//
    78  	// arrange
    79  	const label = "my sysfs label"
    80  	dpa := sysfsDigitalPinAccess{}
    81  	// act
    82  	dp := dpa.createPin("", 9, WithPinLabel(label))
    83  	dps := dp.(*digitalPinSysfs)
    84  	// assert
    85  	gobottest.Assert(t, dps.label, label)
    86  }
    87  
    88  func Test_createPinWithOptionsGpiod(t *testing.T) {
    89  	// This is a general test, that options are applied by using "create" with the WithPinLabel() option.
    90  	// All other configuration options will be tested in tests for "digitalPinConfig".
    91  	//
    92  	// arrange
    93  	const label = "my gpiod label"
    94  	dpa := gpiodDigitalPinAccess{}
    95  	// act
    96  	dp := dpa.createPin("", 19, WithPinLabel(label))
    97  	dpg := dp.(*digitalPinGpiod)
    98  	// assert
    99  	gobottest.Assert(t, dpg.label, label)
   100  	// test fallback for empty chip
   101  	gobottest.Assert(t, dpg.chipName, "gpiochip0")
   102  }