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

     1  package system
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"gobot.io/x/gobot/v2"
     8  	"gobot.io/x/gobot/v2/gobottest"
     9  )
    10  
    11  var _ gobot.DigitalPinOptioner = (*digitalPinConfig)(nil)
    12  
    13  func Test_newDigitalPinConfig(t *testing.T) {
    14  	// arrange
    15  	const (
    16  		label = "gobotio17"
    17  	)
    18  	// act
    19  	d := newDigitalPinConfig(label)
    20  	// assert
    21  	gobottest.Refute(t, d, nil)
    22  	gobottest.Assert(t, d.label, label)
    23  	gobottest.Assert(t, d.direction, IN)
    24  	gobottest.Assert(t, d.outInitialState, 0)
    25  }
    26  
    27  func Test_newDigitalPinConfigWithOption(t *testing.T) {
    28  	// arrange
    29  	const label = "gobotio18"
    30  	// act
    31  	d := newDigitalPinConfig("not used", WithPinLabel(label))
    32  	// assert
    33  	gobottest.Refute(t, d, nil)
    34  	gobottest.Assert(t, d.label, label)
    35  }
    36  
    37  func TestWithPinLabel(t *testing.T) {
    38  	const (
    39  		oldLabel = "old label"
    40  		newLabel = "my optional label"
    41  	)
    42  	var tests = map[string]struct {
    43  		setLabel string
    44  		want     bool
    45  	}{
    46  		"no_change": {
    47  			setLabel: oldLabel,
    48  		},
    49  		"change": {
    50  			setLabel: newLabel,
    51  			want:     true,
    52  		},
    53  	}
    54  	for name, tc := range tests {
    55  		t.Run(name, func(t *testing.T) {
    56  			// arrange
    57  			dpc := &digitalPinConfig{label: oldLabel}
    58  			// act
    59  			got := WithPinLabel(tc.setLabel)(dpc)
    60  			// assert
    61  			gobottest.Assert(t, got, tc.want)
    62  			gobottest.Assert(t, dpc.label, tc.setLabel)
    63  		})
    64  	}
    65  }
    66  
    67  func TestWithPinDirectionOutput(t *testing.T) {
    68  	const (
    69  		// values other than 0, 1 are normally not useful, just to test
    70  		oldVal = 3
    71  		newVal = 5
    72  	)
    73  	var tests = map[string]struct {
    74  		oldDir  string
    75  		want    bool
    76  		wantVal int
    77  	}{
    78  		"no_change": {
    79  			oldDir:  "out",
    80  			wantVal: oldVal,
    81  		},
    82  		"change": {
    83  			oldDir:  "in",
    84  			want:    true,
    85  			wantVal: newVal,
    86  		},
    87  	}
    88  	for name, tc := range tests {
    89  		t.Run(name, func(t *testing.T) {
    90  			// arrange
    91  			dpc := &digitalPinConfig{direction: tc.oldDir, outInitialState: oldVal}
    92  			// act
    93  			got := WithPinDirectionOutput(newVal)(dpc)
    94  			// assert
    95  			gobottest.Assert(t, got, tc.want)
    96  			gobottest.Assert(t, dpc.direction, "out")
    97  			gobottest.Assert(t, dpc.outInitialState, tc.wantVal)
    98  		})
    99  	}
   100  }
   101  
   102  func TestWithPinDirectionInput(t *testing.T) {
   103  	var tests = map[string]struct {
   104  		oldDir string
   105  		want   bool
   106  	}{
   107  		"no_change": {
   108  			oldDir: "in",
   109  		},
   110  		"change": {
   111  			oldDir: "out",
   112  			want:   true,
   113  		},
   114  	}
   115  	for name, tc := range tests {
   116  		t.Run(name, func(t *testing.T) {
   117  			// arrange
   118  			const initValOut = 2 // 2 is normally not useful, just to test that is not touched
   119  			dpc := &digitalPinConfig{direction: tc.oldDir, outInitialState: initValOut}
   120  			// act
   121  			got := WithPinDirectionInput()(dpc)
   122  			// assert
   123  			gobottest.Assert(t, got, tc.want)
   124  			gobottest.Assert(t, dpc.direction, "in")
   125  			gobottest.Assert(t, dpc.outInitialState, initValOut)
   126  		})
   127  	}
   128  }
   129  
   130  func TestWithPinActiveLow(t *testing.T) {
   131  	var tests = map[string]struct {
   132  		oldActiveLow bool
   133  		want         bool
   134  	}{
   135  		"no_change": {
   136  			oldActiveLow: true,
   137  		},
   138  		"change": {
   139  			oldActiveLow: false,
   140  			want:         true,
   141  		},
   142  	}
   143  	for name, tc := range tests {
   144  		t.Run(name, func(t *testing.T) {
   145  			// arrange
   146  			dpc := &digitalPinConfig{activeLow: tc.oldActiveLow}
   147  			// act
   148  			got := WithPinActiveLow()(dpc)
   149  			// assert
   150  			gobottest.Assert(t, got, tc.want)
   151  			gobottest.Assert(t, dpc.activeLow, true)
   152  		})
   153  	}
   154  }
   155  
   156  func TestWithPinPullDown(t *testing.T) {
   157  	var tests = map[string]struct {
   158  		oldBias int
   159  		want    bool
   160  		wantVal int
   161  	}{
   162  		"no_change": {
   163  			oldBias: digitalPinBiasPullDown,
   164  		},
   165  		"change": {
   166  			oldBias: digitalPinBiasPullUp,
   167  			want:    true,
   168  		},
   169  	}
   170  	for name, tc := range tests {
   171  		t.Run(name, func(t *testing.T) {
   172  			// arrange
   173  			dpc := &digitalPinConfig{bias: tc.oldBias}
   174  			// act
   175  			got := WithPinPullDown()(dpc)
   176  			// assert
   177  			gobottest.Assert(t, got, tc.want)
   178  			gobottest.Assert(t, dpc.bias, digitalPinBiasPullDown)
   179  		})
   180  	}
   181  }
   182  
   183  func TestWithPinPullUp(t *testing.T) {
   184  	var tests = map[string]struct {
   185  		oldBias int
   186  		want    bool
   187  		wantVal int
   188  	}{
   189  		"no_change": {
   190  			oldBias: digitalPinBiasPullUp,
   191  		},
   192  		"change": {
   193  			oldBias: digitalPinBiasPullDown,
   194  			want:    true,
   195  		},
   196  	}
   197  	for name, tc := range tests {
   198  		t.Run(name, func(t *testing.T) {
   199  			// arrange
   200  			dpc := &digitalPinConfig{bias: tc.oldBias}
   201  			// act
   202  			got := WithPinPullUp()(dpc)
   203  			// assert
   204  			gobottest.Assert(t, got, tc.want)
   205  			gobottest.Assert(t, dpc.bias, digitalPinBiasPullUp)
   206  		})
   207  	}
   208  }
   209  
   210  func TestWithPinOpenDrain(t *testing.T) {
   211  	var tests = map[string]struct {
   212  		oldDrive int
   213  		want     bool
   214  		wantVal  int
   215  	}{
   216  		"no_change": {
   217  			oldDrive: digitalPinDriveOpenDrain,
   218  		},
   219  		"change_from_pushpull": {
   220  			oldDrive: digitalPinDrivePushPull,
   221  			want:     true,
   222  		},
   223  		"change_from_opensource": {
   224  			oldDrive: digitalPinDriveOpenSource,
   225  			want:     true,
   226  		},
   227  	}
   228  	for name, tc := range tests {
   229  		t.Run(name, func(t *testing.T) {
   230  			// arrange
   231  			dpc := &digitalPinConfig{drive: tc.oldDrive}
   232  			// act
   233  			got := WithPinOpenDrain()(dpc)
   234  			// assert
   235  			gobottest.Assert(t, got, tc.want)
   236  			gobottest.Assert(t, dpc.drive, digitalPinDriveOpenDrain)
   237  		})
   238  	}
   239  }
   240  
   241  func TestWithPinOpenSource(t *testing.T) {
   242  	var tests = map[string]struct {
   243  		oldDrive int
   244  		want     bool
   245  		wantVal  int
   246  	}{
   247  		"no_change": {
   248  			oldDrive: digitalPinDriveOpenSource,
   249  		},
   250  		"change_from_pushpull": {
   251  			oldDrive: digitalPinDrivePushPull,
   252  			want:     true,
   253  		},
   254  		"change_from_opendrain": {
   255  			oldDrive: digitalPinDriveOpenDrain,
   256  			want:     true,
   257  		},
   258  	}
   259  	for name, tc := range tests {
   260  		t.Run(name, func(t *testing.T) {
   261  			// arrange
   262  			dpc := &digitalPinConfig{drive: tc.oldDrive}
   263  			// act
   264  			got := WithPinOpenSource()(dpc)
   265  			// assert
   266  			gobottest.Assert(t, got, tc.want)
   267  			gobottest.Assert(t, dpc.drive, digitalPinDriveOpenSource)
   268  		})
   269  	}
   270  }
   271  
   272  func TestWithPinDebounce(t *testing.T) {
   273  	const (
   274  		oldVal = time.Duration(10)
   275  		newVal = time.Duration(14)
   276  	)
   277  	var tests = map[string]struct {
   278  		oldDebouncePeriod time.Duration
   279  		want              bool
   280  		wantVal           time.Duration
   281  	}{
   282  		"no_change": {
   283  			oldDebouncePeriod: newVal,
   284  		},
   285  		"change": {
   286  			oldDebouncePeriod: oldVal,
   287  			want:              true,
   288  		},
   289  	}
   290  	for name, tc := range tests {
   291  		t.Run(name, func(t *testing.T) {
   292  			// arrange
   293  			dpc := &digitalPinConfig{debouncePeriod: tc.oldDebouncePeriod}
   294  			// act
   295  			got := WithPinDebounce(newVal)(dpc)
   296  			// assert
   297  			gobottest.Assert(t, got, tc.want)
   298  			gobottest.Assert(t, dpc.debouncePeriod, newVal)
   299  		})
   300  	}
   301  }
   302  
   303  func TestWithPinEventOnFallingEdge(t *testing.T) {
   304  	const (
   305  		oldVal = digitalPinEventNone
   306  		newVal = digitalPinEventOnFallingEdge
   307  	)
   308  	var tests = map[string]struct {
   309  		oldEdge int
   310  		want    bool
   311  		wantVal int
   312  	}{
   313  		"no_change": {
   314  			oldEdge: newVal,
   315  		},
   316  		"change": {
   317  			oldEdge: oldVal,
   318  			want:    true,
   319  		},
   320  	}
   321  	for name, tc := range tests {
   322  		t.Run(name, func(t *testing.T) {
   323  			// arrange
   324  			dpc := &digitalPinConfig{edge: tc.oldEdge}
   325  			handler := func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32, lseqno uint32) {}
   326  			// act
   327  			got := WithPinEventOnFallingEdge(handler)(dpc)
   328  			// assert
   329  			gobottest.Assert(t, got, tc.want)
   330  			gobottest.Assert(t, dpc.edge, newVal)
   331  			gobottest.Refute(t, dpc.edgeEventHandler, nil)
   332  		})
   333  	}
   334  }
   335  
   336  func TestWithPinEventOnRisingEdge(t *testing.T) {
   337  	const (
   338  		oldVal = digitalPinEventNone
   339  		newVal = digitalPinEventOnRisingEdge
   340  	)
   341  	var tests = map[string]struct {
   342  		oldEdge int
   343  		want    bool
   344  		wantVal int
   345  	}{
   346  		"no_change": {
   347  			oldEdge: newVal,
   348  		},
   349  		"change": {
   350  			oldEdge: oldVal,
   351  			want:    true,
   352  		},
   353  	}
   354  	for name, tc := range tests {
   355  		t.Run(name, func(t *testing.T) {
   356  			// arrange
   357  			dpc := &digitalPinConfig{edge: tc.oldEdge}
   358  			handler := func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32, lseqno uint32) {}
   359  			// act
   360  			got := WithPinEventOnRisingEdge(handler)(dpc)
   361  			// assert
   362  			gobottest.Assert(t, got, tc.want)
   363  			gobottest.Assert(t, dpc.edge, newVal)
   364  			gobottest.Refute(t, dpc.edgeEventHandler, nil)
   365  		})
   366  	}
   367  }
   368  
   369  func TestWithPinEventOnBothEdges(t *testing.T) {
   370  	const (
   371  		oldVal = digitalPinEventNone
   372  		newVal = digitalPinEventOnBothEdges
   373  	)
   374  	var tests = map[string]struct {
   375  		oldEdge int
   376  		want    bool
   377  		wantVal int
   378  	}{
   379  		"no_change": {
   380  			oldEdge: newVal,
   381  		},
   382  		"change": {
   383  			oldEdge: oldVal,
   384  			want:    true,
   385  		},
   386  	}
   387  	for name, tc := range tests {
   388  		t.Run(name, func(t *testing.T) {
   389  			// arrange
   390  			dpc := &digitalPinConfig{edge: tc.oldEdge}
   391  			handler := func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32, lseqno uint32) {}
   392  			// act
   393  			got := WithPinEventOnBothEdges(handler)(dpc)
   394  			// assert
   395  			gobottest.Assert(t, got, tc.want)
   396  			gobottest.Assert(t, dpc.edge, newVal)
   397  			gobottest.Refute(t, dpc.edgeEventHandler, nil)
   398  		})
   399  	}
   400  }