Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/module/gpio/gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ func (s *Switch) Init() error {
return s.on()
}

s.state = off

return s.off()
}

Expand All @@ -274,7 +276,7 @@ func (s *Switch) Run(_ context.Context, sesh module.Session, args ...string) err
log.Println("gpio.Switch module: Run called")

if len(args) == 0 {
sesh.Print(fmt.Sprintf("Current state: %s", s.state))
sesh.Printf("Current state: %s\n", s.state)

return nil
}
Expand Down
48 changes: 35 additions & 13 deletions pkg/module/gpio/gpio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,12 @@ func TestButtonHelp(t *testing.T) {

func TestSwitchInit(t *testing.T) {
tests := []struct {
name string
swtch Switch
mockErr error
expectFunc func(mock *MockGpio) bool
expectErr bool
name string
swtch Switch
mockErr error
expectFunc func(mock *MockGpio) bool
expectErr bool
expectedState switchState
}{
{
name: "Init with Initial 'on' and ActiveLow false",
Expand All @@ -305,6 +306,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.HighCalled
},
expectedState: on,
},
{
name: "Init with Initial 'On' and ActiveLow false",
Expand All @@ -315,6 +317,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.HighCalled
},
expectedState: on,
},
{
name: "Init with Initial 'off' and ActiveLow false",
Expand All @@ -325,6 +328,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.LowCalled
},
expectedState: off,
},
{
name: "Init with Initial 'Off' and ActiveLow false",
Expand All @@ -335,6 +339,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.LowCalled
},
expectedState: off,
},
{
name: "Init with Initial 'on' and ActiveLow true",
Expand All @@ -345,6 +350,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.LowCalled
},
expectedState: on,
},
{
name: "Init with Initial 'On' and ActiveLow true",
Expand All @@ -355,6 +361,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.LowCalled
},
expectedState: on,
},
{
name: "Init with Initial 'off' and ActiveLow true",
Expand All @@ -365,6 +372,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.HighCalled
},
expectedState: off,
},
{
name: "Init with Initial 'Off' and ActiveLow true",
Expand All @@ -375,6 +383,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.HighCalled
},
expectedState: off,
},
{
name: "Init with Initial '' and ActiveLow false",
Expand All @@ -385,6 +394,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.LowCalled
},
expectedState: off,
},
{
name: "Init with Initial 'unknown' and ActiveLow false",
Expand All @@ -395,6 +405,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.LowCalled
},
expectedState: off,
},
{
name: "Init with Initial '' and ActiveLow true",
Expand All @@ -405,6 +416,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.HighCalled
},
expectedState: off,
},
{
name: "Init with Initial 'unknown' and ActiveLow true",
Expand All @@ -415,6 +427,7 @@ func TestSwitchInit(t *testing.T) {
expectFunc: func(mock *MockGpio) bool {
return mock.HighCalled
},
expectedState: off,
},
}

Expand All @@ -429,6 +442,9 @@ func TestSwitchInit(t *testing.T) {
if tt.expectFunc != nil && !tt.expectFunc(tt.swtch.gpio.(*MockGpio)) {
t.Errorf("expected function not called for %s", tt.name)
}
if tt.swtch.state != tt.expectedState {
t.Errorf("expected state %q after Init, got %q", tt.expectedState, tt.swtch.state)
}
})
}
}
Expand Down Expand Up @@ -470,12 +486,13 @@ func TestSwitchDeinit(t *testing.T) {

func TestSwitchRun(t *testing.T) {
tests := []struct {
name string
swtch Switch
args []string
mockErr error
expectFunc func(mock *MockGpio) bool
expectErr bool
name string
swtch Switch
args []string
mockErr error
expectFunc func(mock *MockGpio) bool
expectErr bool
expectPrint string // non-empty: assert mock.Session.PrintText equals this value
}{
{
name: "Run with state 'on', ActiveLow false, and args 'on'",
Expand Down Expand Up @@ -534,7 +551,8 @@ func TestSwitchRun(t *testing.T) {
state: on,
ActiveLow: false,
},
expectErr: false,
expectErr: false,
expectPrint: "Current state: on\n",
},
{
name: "Run with state 'off', ActiveLow true, and args 'on'",
Expand Down Expand Up @@ -593,7 +611,8 @@ func TestSwitchRun(t *testing.T) {
state: off,
ActiveLow: true,
},
expectErr: false,
expectErr: false,
expectPrint: "Current state: off\n",
},
{
name: "Run with args 'on' with GPIO error",
Expand Down Expand Up @@ -647,6 +666,9 @@ func TestSwitchRun(t *testing.T) {
if tt.expectFunc != nil && !tt.expectFunc(mockGpio) {
t.Errorf("expected function not called for %s", tt.name)
}
if tt.expectPrint != "" && sesh.PrintText != tt.expectPrint {
t.Errorf("expected print %q, got %q", tt.expectPrint, sesh.PrintText)
}
})
}
}
Expand Down