Skip to content

Commit

Permalink
chore: fix up testing a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
wolffshots committed Nov 1, 2024
1 parent 1b9df9d commit 58f7499
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
engines:
eslint:
enabled: true
ignore:
- "dummy/**/*"
3 changes: 3 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage:
ignore:
- dummy/**/*.go
2 changes: 1 addition & 1 deletion ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (ip *Port) Read(timeout time.Duration) (string, error) {
func (ip *Port) Write(input string) (int, error) {
_ = crc.Encode(input) // TODO use message
// if ip == nil {
return 0, errors.New("port is nil on write")
return 0, errors.New("ip port is nil on write")
// }
// TODO
// return 0, nil
Expand Down
70 changes: 70 additions & 0 deletions ip/ip_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
package phocus_ip

import (
"errors"
"testing"
"time"
)

func TestPort_Open(t *testing.T) {
tests := []struct {
name string
port Port
wantErr bool
}{
{
name: "Successful Open",
port: Port{
Host: "localhost",
Port: 8080,
Retries: 3,
},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.port.Open()
if (err != nil) != tt.wantErr {
t.Errorf("Port.Open() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got == nil {
t.Error("Expected non-nil comms.Port, got nil")
}
})
}
}

func TestPort_Close(t *testing.T) {
port := Port{}
if err := port.Close(); err != nil {
t.Errorf("Port.Close() error = %v, want nil", err)
}
}

func TestPort_Read(t *testing.T) {
port := Port{}
timeout := time.Second

result, err := port.Read(timeout)
if err != nil && !errors.Is(err, errors.New("timeout")) {
t.Errorf("Port.Read() error = %v, want nil or timeout error", err)
}
if result != "" {
t.Errorf("Port.Read() got = %v, want empty string", result)
}
}

func TestPort_Write(t *testing.T) {
port := Port{}
input := "test_payload"

n, err := port.Write(input)
if err == nil || err.Error() != "ip port is nil on write" {
t.Errorf("Port.Write() error = %v, want 'ip port is nil on write'", err)
}
if n != 0 {
t.Errorf("Port.Write() bytes written = %v, want 0", n)
}
}
4 changes: 2 additions & 2 deletions messages/QID_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestQID(t *testing.T) {
// invalid write
written, err = SendQID(commonPort1, nil)
assert.Equal(t, -1, written)
assert.Equal(t, errors.New("port is nil on write"), err)
assert.Equal(t, errors.New("serial port is nil on write"), err)
})

t.Run("TestReceiveQID", func(t *testing.T) {
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestQID(t *testing.T) {
// invalid read
response, err = ReceiveQID(commonPort2, 10*time.Millisecond)
assert.Equal(t, "", response)
assert.Equal(t, errors.New("port is nil on read"), err)
assert.Equal(t, errors.New("serial port is nil on read"), err)

// reopen port
commonPort2, err = serialPort2.Open()
Expand Down
6 changes: 3 additions & 3 deletions messages/QPGSn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestQPGSn(t *testing.T) {
// invalid write
written, err = SendQPGSn(commonPort1, nil)
assert.Equal(t, -1, written)
assert.Equal(t, errors.New("port is nil on write"), err)
assert.Equal(t, errors.New("serial port is nil on write"), err)

// invalid write
written, err = SendQPGSn(commonPort1, "1")
Expand All @@ -59,7 +59,7 @@ func TestQPGSn(t *testing.T) {
// invalid write
written, err = SendQPGSn(commonPort1, 1)
assert.Equal(t, -1, written)
assert.Equal(t, errors.New("port is nil on write"), err)
assert.Equal(t, errors.New("serial port is nil on write"), err)
})

t.Run("TestReceiveQPGSn", func(t *testing.T) {
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestQPGSn(t *testing.T) {
// invalid read
response, err = ReceiveQPGSn(commonPort2, 10*time.Millisecond, 1)
assert.Equal(t, "", response)
assert.Equal(t, errors.New("port is nil on read"), err)
assert.Equal(t, errors.New("serial port is nil on read"), err)

commonPort2, err = serialPort2.Open()
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions messages/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func TestGeneric(t *testing.T) {
// invalid write
written, err = SendGeneric(commonPort1, "GENERIC", nil)
assert.Equal(t, -1, written)
assert.Equal(t, errors.New("port is nil on write"), err)
assert.Equal(t, errors.New("serial port is nil on write"), err)
written, err = SendGeneric(commonPort1, "GENERIC", 1)
assert.Equal(t, -1, written)
assert.Equal(t, errors.New("port is nil on write"), err)
assert.Equal(t, errors.New("serial port is nil on write"), err)
written, err = SendGeneric(commonPort1, "GENERIC", "1")
assert.Equal(t, -1, written)
assert.Equal(t, errors.New("port is nil on write"), err)
assert.Equal(t, errors.New("serial port is nil on write"), err)
})

t.Run("TestReceiveGeneric", func(t *testing.T) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestGeneric(t *testing.T) {
// invalid read
response, err = ReceiveGeneric(commonPort2, "GENERIC", 10*time.Millisecond)
assert.Equal(t, "", response)
assert.Equal(t, errors.New("port is nil on read"), err)
assert.Equal(t, errors.New("serial port is nil on read"), err)

commonPort2, err = serialPort2.Open()
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions messages/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ func TestMesages(t *testing.T) {
assert.NoError(t, err)

qpgsnresponse, err := Interpret(client, commonPort2, Message{uuid.New(), "QPGS1", ""}, 0*time.Second)
assert.EqualError(t, err, "port is nil on write")
assert.EqualError(t, err, "serial port is nil on write")
assert.Nil(t, qpgsnresponse)

qpgsnresponse, err = Interpret(client, commonPort2, Message{uuid.New(), "QPGS2", ""}, 0*time.Second)
assert.EqualError(t, err, "port is nil on write")
assert.EqualError(t, err, "serial port is nil on write")
assert.Nil(t, qpgsnresponse)

qpgsnresponse, err = Interpret(client, commonPort2, Message{uuid.New(), "QID", ""}, 0*time.Second)
assert.EqualError(t, err, "port is nil on write")
assert.EqualError(t, err, "serial port is nil on write")
assert.Nil(t, qpgsnresponse)

qpgsnresponse, err = Interpret(client, commonPort2, Message{uuid.New(), "SOMETHING_ELSE", ""}, 0*time.Second)
assert.EqualError(t, err, "port is nil on write")
assert.EqualError(t, err, "serial port is nil on write")
assert.Nil(t, qpgsnresponse)
})

Expand Down
6 changes: 3 additions & 3 deletions serial/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (sp *Port) Open() (comms.Port, error) {
func (sp *Port) Close() error {
fmt.Printf("Closing serial port: %s\n", sp.Path)
if sp.Port == nil || sp == nil {
return errors.New("serial port was nil on close call")
return errors.New("serial port is nil on close")
}
err := (*sp.Port).Close()
if err == nil {
Expand All @@ -73,7 +73,7 @@ func (sp *Port) Read(timeout time.Duration) (string, error) {
log.Printf("Starting read\n")
if sp.Port == nil || sp == nil {
log.Printf("Port nil on read\n")
return "", errors.New("port is nil on read")
return "", errors.New("serial port is nil on read")
}
buff := make([]byte, 140)
err := (*sp.Port).SetReadTimeout(timeout)
Expand Down Expand Up @@ -110,7 +110,7 @@ func (sp *Port) Read(timeout time.Duration) (string, error) {
func (sp *Port) Write(input string) (int, error) {
log.Printf("Starting write\n")
if sp.Port == nil || sp == nil {
return -1, errors.New("port is nil on write")
return -1, errors.New("serial port is nil on write")
}
message := crc.Encode(input)
n, err := (*sp.Port).Write([]byte(message))
Expand Down
21 changes: 19 additions & 2 deletions serial/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestSerial(t *testing.T) {
commonPort1.Close()
written, err = commonPort1.Write("test")
assert.Equal(t, -1, written)
assert.Equal(t, errors.New("port is nil on write"), err)
assert.Equal(t, errors.New("serial port is nil on write"), err)
})

t.Run("TestRead", func(t *testing.T) {
Expand All @@ -158,7 +158,7 @@ func TestSerial(t *testing.T) {
assert.NoError(t, err)
read, err = commonPort1.Read(1 * time.Millisecond)
assert.Equal(t, "", read)
assert.Equal(t, errors.New("port is nil on read"), err)
assert.Equal(t, errors.New("serial port is nil on read"), err)
})

t.Run("TestReadWrite", func(t *testing.T) {
Expand Down Expand Up @@ -195,4 +195,21 @@ func TestSerial(t *testing.T) {
assert.Equal(t, "test\x9b\x06\r", read)
assert.NoError(t, err)
})

t.Run("TestMultiClose", func(t *testing.T) {
serialPort1 := Port{
Path: "./serial1",
Baud: 2400,
Retries: 5,
}
commonPort1, err := serialPort1.Open()
assert.NoError(t, err)
assert.Equal(t, "./serial1", serialPort1.Path)

err = commonPort1.Close()
assert.NoError(t, err)

err = commonPort1.Close()
assert.Equal(t, errors.New("serial port is nil on close"), err)
})
}

0 comments on commit 58f7499

Please sign in to comment.