Skip to content

Commit

Permalink
Add tests for source APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 11, 2024
1 parent 5bb3457 commit 15d3407
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions remote/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (t *mockConnection) Close() error {

// FlashToReader flushes written data into the read buffer.
func (t *mockConnection) FlushToReader() {
t.readBuf.Reset()
t.readBuf.Write(t.writeBuf.Bytes())
t.writeBuf.Reset()
}
Expand Down
121 changes: 121 additions & 0 deletions remote/source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package remote

import (
"fmt"
"testing"

"github.com/Jacalz/hegelmote/device"
)

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

err := control.SetSourceNumber(0)
if err == nil {
t.Fail()
}

// Command returns the currently set input on success. Fill buffer.
mock.readBuf.WriteString("-i.1\r")

err = control.SetSourceNumber(1)
if err != nil || mock.writeBuf.String() != "-i.1\r" {
t.Fail()
}

// Fill reader but clear writer.
mock.FlushToReader()

err = control.SetSourceNumber(8)
if err != nil || mock.writeBuf.String() != "-i.8\r" {
fmt.Println(err, mock.writeBuf.String())
t.Fail()
}
}

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

mock.readBuf.WriteString("-i.1\r")
control.SetSourceNumber(1)
mock.FlushToReader()

number, err := control.GetSourceNumber()
if err != nil || number != 1 || mock.writeBuf.String() != "-i.?\r" {
t.Fail()
}

mock.Close()
control.SetSourceNumber(8)
mock.FlushToReader()

number, err = control.GetSourceNumber()
if err != nil || number != 8 {
t.Fail()
}
}

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

mock.readBuf.WriteString("-i.1\r")
err := control.SetSourceName(device.H95, "Analog 1")
if err != nil || mock.writeBuf.String() != "-i.1\r" {
t.Fail()
}

mock.FlushToReader()

number, err := control.GetSourceNumber()
if err != nil || number != 1 {
t.Fail()
}

mock.FlushToReader()

err = control.SetSourceName(device.H95, "Network")
if err != nil || mock.writeBuf.String() != "-i.8\r" {
t.Fail()
}

mock.FlushToReader()

number, err = control.GetSourceNumber()
if err != nil || number != 8 {
t.Fail()
}

err = control.SetSourceName(device.H590, "Bogus")
if err == nil {
t.Fail()
}
}

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

mock.readBuf.WriteString("-i.1\r")
control.SetSourceNumber(1)
mock.FlushToReader()

name, err := control.GetSourceName(device.H95)
if err != nil || name != "Analog 1" {
t.Fail()
}

mock.Close()

mock.readBuf.WriteString("-i.8\r")
control.SetSourceNumber(8)
mock.FlushToReader()

name, err = control.GetSourceName(device.H95)
if err != nil || name != "Network" {
t.Fail()
}

_, err = control.GetSourceName(15)
if err == nil {
t.Fail()
}
}

0 comments on commit 15d3407

Please sign in to comment.