Skip to content

Commit

Permalink
Wire up tests for reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 11, 2024
1 parent ecdf428 commit 5ede8a5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions remote/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// SetResetDelay sets a timeout, in minutes, for when to reset.
func (c *Control) SetResetDelay(delay uint8) error {
number := strconv.FormatInt(int64(delay), 10)
number := strconv.FormatUint(uint64(delay), 10)
_, err := fmt.Fprintf(c.conn, commandFormat, "r", number)
return err
}
Expand All @@ -26,14 +26,14 @@ func (c *Control) GetResetDelay() (uint8, bool, error) {
return 0, false, err
}

resp := [6]byte{}
resp := [7]byte{}
n, err := c.conn.Read(resp[:])
if err != nil {
return 0, false, err
}

// Check if reset is stopped or not enabled.
if n == 4 && resp[3] == '~' {
if n >= 4 && resp[3] == '~' {
return 0, true, nil
}

Expand Down
57 changes: 57 additions & 0 deletions remote/reset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package remote

import (
"testing"
)

func TestSetResetDelay(t *testing.T) {
control, mock := newControlMock()

err := control.SetResetDelay(0)
if err != nil || mock.writeBuf.String() != "-r.0\r" {
t.Fail()
}
mock.writeBuf.Reset()

err = control.SetResetDelay(255)
if err != nil || mock.writeBuf.String() != "-r.255\r" {
t.Fail()
}
mock.writeBuf.Reset()
}

func TestStopResetDelay(t *testing.T) {
control, mock := newControlMock()

err := control.StopResetDelay()
if err != nil || mock.writeBuf.String() != "-r.~\r" {
t.Fail()
}
mock.writeBuf.Reset()
}

func TestGetResetDelay(t *testing.T) {
control, mock := newControlMock()

// Set state to stopped.
control.StopResetDelay()
mock.readBuf.Write(mock.writeBuf.Bytes())
mock.writeBuf.Reset()

delay, stopped, err := control.GetResetDelay()
if err != nil || delay != 0 || !stopped || mock.writeBuf.String() != "-r.?\r" {
t.Fail()
}
mock.Close()

// Set state delay to 255.
control.SetResetDelay(255)
mock.readBuf.Write(mock.writeBuf.Bytes())
mock.writeBuf.Reset()

delay, stopped, err = control.GetResetDelay()
if err != nil || delay != 255 || stopped || mock.writeBuf.String() != "-r.?\r" {
t.Fail()
}
mock.Close()
}

0 comments on commit 5ede8a5

Please sign in to comment.