parse messages
This commit is contained in:
parent
65ad286add
commit
0897210754
5 changed files with 193 additions and 2 deletions
116
event_proxy/main_test.go
Normal file
116
event_proxy/main_test.go
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseMsg(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
wantID int
|
||||
wantData []string
|
||||
wantError bool
|
||||
}{
|
||||
{
|
||||
name: "valid message with single data element",
|
||||
input: "M01,25.5",
|
||||
wantID: 1,
|
||||
wantData: []string{"25.5"},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "valid message with multiple data elements",
|
||||
input: "M02,25.5,60,1013",
|
||||
wantID: 2,
|
||||
wantData: []string{"25.5", "60", "1013"},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "valid message with ID only",
|
||||
input: "M0",
|
||||
wantID: 0,
|
||||
wantData: []string{},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "invalid message with non-numeric ID",
|
||||
input: "Mabc,data",
|
||||
wantID: 0,
|
||||
wantData: nil,
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "invalid message with missing M prefix",
|
||||
input: "1,data",
|
||||
wantID: 0,
|
||||
wantData: nil,
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "invalid message with empty string",
|
||||
input: "",
|
||||
wantID: 0,
|
||||
wantData: nil,
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "valid message with empty data elements",
|
||||
input: "M5,,data,,",
|
||||
wantID: 5,
|
||||
wantData: []string{"", "data", "", ""},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "valid message with special characters in data",
|
||||
input: "M10,key=value!@#$",
|
||||
wantID: 10,
|
||||
wantData: []string{"key=value!@#$"},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "newlines and whitespace are stripped from data",
|
||||
input: "M1,1.0,1.1,1.2\n",
|
||||
wantID: 1,
|
||||
wantData: []string{"1.0", "1.1", "1.2"},
|
||||
wantError: false,
|
||||
},
|
||||
{
|
||||
name: "leading and trailing whitespace stripped",
|
||||
input: "M5, data1 , data2 \t, data3\r\n",
|
||||
wantID: 5,
|
||||
wantData: []string{"data1", "data2", "data3"},
|
||||
wantError: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ParseMsg(tt.input)
|
||||
|
||||
if (err != nil) != tt.wantError {
|
||||
t.Errorf("ParseMsg() error = %v, wantError %v", err, tt.wantError)
|
||||
return
|
||||
}
|
||||
|
||||
if tt.wantError {
|
||||
return
|
||||
}
|
||||
|
||||
if got.ID != tt.wantID {
|
||||
t.Errorf("ParseMsg() ID = %d, want %d", got.ID, tt.wantID)
|
||||
}
|
||||
|
||||
if len(got.Data) != len(tt.wantData) {
|
||||
t.Errorf("ParseMsg() data length = %d, want %d", len(got.Data), len(tt.wantData))
|
||||
return
|
||||
}
|
||||
|
||||
for i, val := range got.Data {
|
||||
if val != tt.wantData[i] {
|
||||
t.Errorf("ParseMsg() data[%d] = %q, want %q", i, val, tt.wantData[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue