Skip to content

Commit 9daec79

Browse files
committed
test(gpio): assert Switch state after Init and Run output format
Signed-off-by: Fabian Wienand <fabian.wienand@blindspot.software>
1 parent c51ca44 commit 9daec79

1 file changed

Lines changed: 35 additions & 13 deletions

File tree

pkg/module/gpio/gpio_test.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,12 @@ func TestButtonHelp(t *testing.T) {
290290

291291
func TestSwitchInit(t *testing.T) {
292292
tests := []struct {
293-
name string
294-
swtch Switch
295-
mockErr error
296-
expectFunc func(mock *MockGpio) bool
297-
expectErr bool
293+
name string
294+
swtch Switch
295+
mockErr error
296+
expectFunc func(mock *MockGpio) bool
297+
expectErr bool
298+
expectedState switchState
298299
}{
299300
{
300301
name: "Init with Initial 'on' and ActiveLow false",
@@ -305,6 +306,7 @@ func TestSwitchInit(t *testing.T) {
305306
expectFunc: func(mock *MockGpio) bool {
306307
return mock.HighCalled
307308
},
309+
expectedState: on,
308310
},
309311
{
310312
name: "Init with Initial 'On' and ActiveLow false",
@@ -315,6 +317,7 @@ func TestSwitchInit(t *testing.T) {
315317
expectFunc: func(mock *MockGpio) bool {
316318
return mock.HighCalled
317319
},
320+
expectedState: on,
318321
},
319322
{
320323
name: "Init with Initial 'off' and ActiveLow false",
@@ -325,6 +328,7 @@ func TestSwitchInit(t *testing.T) {
325328
expectFunc: func(mock *MockGpio) bool {
326329
return mock.LowCalled
327330
},
331+
expectedState: off,
328332
},
329333
{
330334
name: "Init with Initial 'Off' and ActiveLow false",
@@ -335,6 +339,7 @@ func TestSwitchInit(t *testing.T) {
335339
expectFunc: func(mock *MockGpio) bool {
336340
return mock.LowCalled
337341
},
342+
expectedState: off,
338343
},
339344
{
340345
name: "Init with Initial 'on' and ActiveLow true",
@@ -345,6 +350,7 @@ func TestSwitchInit(t *testing.T) {
345350
expectFunc: func(mock *MockGpio) bool {
346351
return mock.LowCalled
347352
},
353+
expectedState: on,
348354
},
349355
{
350356
name: "Init with Initial 'On' and ActiveLow true",
@@ -355,6 +361,7 @@ func TestSwitchInit(t *testing.T) {
355361
expectFunc: func(mock *MockGpio) bool {
356362
return mock.LowCalled
357363
},
364+
expectedState: on,
358365
},
359366
{
360367
name: "Init with Initial 'off' and ActiveLow true",
@@ -365,6 +372,7 @@ func TestSwitchInit(t *testing.T) {
365372
expectFunc: func(mock *MockGpio) bool {
366373
return mock.HighCalled
367374
},
375+
expectedState: off,
368376
},
369377
{
370378
name: "Init with Initial 'Off' and ActiveLow true",
@@ -375,6 +383,7 @@ func TestSwitchInit(t *testing.T) {
375383
expectFunc: func(mock *MockGpio) bool {
376384
return mock.HighCalled
377385
},
386+
expectedState: off,
378387
},
379388
{
380389
name: "Init with Initial '' and ActiveLow false",
@@ -385,6 +394,7 @@ func TestSwitchInit(t *testing.T) {
385394
expectFunc: func(mock *MockGpio) bool {
386395
return mock.LowCalled
387396
},
397+
expectedState: off,
388398
},
389399
{
390400
name: "Init with Initial 'unknown' and ActiveLow false",
@@ -395,6 +405,7 @@ func TestSwitchInit(t *testing.T) {
395405
expectFunc: func(mock *MockGpio) bool {
396406
return mock.LowCalled
397407
},
408+
expectedState: off,
398409
},
399410
{
400411
name: "Init with Initial '' and ActiveLow true",
@@ -405,6 +416,7 @@ func TestSwitchInit(t *testing.T) {
405416
expectFunc: func(mock *MockGpio) bool {
406417
return mock.HighCalled
407418
},
419+
expectedState: off,
408420
},
409421
{
410422
name: "Init with Initial 'unknown' and ActiveLow true",
@@ -415,6 +427,7 @@ func TestSwitchInit(t *testing.T) {
415427
expectFunc: func(mock *MockGpio) bool {
416428
return mock.HighCalled
417429
},
430+
expectedState: off,
418431
},
419432
}
420433

@@ -429,6 +442,9 @@ func TestSwitchInit(t *testing.T) {
429442
if tt.expectFunc != nil && !tt.expectFunc(tt.swtch.gpio.(*MockGpio)) {
430443
t.Errorf("expected function not called for %s", tt.name)
431444
}
445+
if tt.swtch.state != tt.expectedState {
446+
t.Errorf("expected state %q after Init, got %q", tt.expectedState, tt.swtch.state)
447+
}
432448
})
433449
}
434450
}
@@ -470,12 +486,13 @@ func TestSwitchDeinit(t *testing.T) {
470486

471487
func TestSwitchRun(t *testing.T) {
472488
tests := []struct {
473-
name string
474-
swtch Switch
475-
args []string
476-
mockErr error
477-
expectFunc func(mock *MockGpio) bool
478-
expectErr bool
489+
name string
490+
swtch Switch
491+
args []string
492+
mockErr error
493+
expectFunc func(mock *MockGpio) bool
494+
expectErr bool
495+
expectPrint string // non-empty: assert mock.Session.PrintText equals this value
479496
}{
480497
{
481498
name: "Run with state 'on', ActiveLow false, and args 'on'",
@@ -534,7 +551,8 @@ func TestSwitchRun(t *testing.T) {
534551
state: on,
535552
ActiveLow: false,
536553
},
537-
expectErr: false,
554+
expectErr: false,
555+
expectPrint: "Current state: on\n",
538556
},
539557
{
540558
name: "Run with state 'off', ActiveLow true, and args 'on'",
@@ -593,7 +611,8 @@ func TestSwitchRun(t *testing.T) {
593611
state: off,
594612
ActiveLow: true,
595613
},
596-
expectErr: false,
614+
expectErr: false,
615+
expectPrint: "Current state: off\n",
597616
},
598617
{
599618
name: "Run with args 'on' with GPIO error",
@@ -647,6 +666,9 @@ func TestSwitchRun(t *testing.T) {
647666
if tt.expectFunc != nil && !tt.expectFunc(mockGpio) {
648667
t.Errorf("expected function not called for %s", tt.name)
649668
}
669+
if tt.expectPrint != "" && sesh.PrintText != tt.expectPrint {
670+
t.Errorf("expected print %q, got %q", tt.expectPrint, sesh.PrintText)
671+
}
650672
})
651673
}
652674
}

0 commit comments

Comments
 (0)