protocol/aprs: refactored
This commit is contained in:
153
protocol/aprs/message_test.go
Normal file
153
protocol/aprs/message_test.go
Normal file
@@ -0,0 +1,153 @@
|
||||
package aprs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseMessage(t *testing.T) {
|
||||
tests := []struct {
|
||||
Name string
|
||||
Raw Raw
|
||||
Want *Message
|
||||
}{
|
||||
{
|
||||
"message, no ack expected",
|
||||
":WU2Z :Testing",
|
||||
&Message{
|
||||
Recipient: "WU2Z",
|
||||
Text: "Testing",
|
||||
},
|
||||
},
|
||||
{
|
||||
"message with sequence number, ack expected",
|
||||
":WU2Z :Testing{003",
|
||||
&Message{
|
||||
ID: 3,
|
||||
Recipient: "WU2Z",
|
||||
Text: "Testing",
|
||||
},
|
||||
},
|
||||
{
|
||||
"an e-mail message",
|
||||
":EMAIL :msproul@ap.org Test email",
|
||||
&Message{
|
||||
Recipient: "EMAIL",
|
||||
Text: "msproul@ap.org Test email",
|
||||
},
|
||||
},
|
||||
{
|
||||
"message acknowledgement",
|
||||
":KB2ICI-14:ack003",
|
||||
&Message{
|
||||
ID: 3,
|
||||
IsAcknowledge: true,
|
||||
Recipient: "KB2ICI-14",
|
||||
},
|
||||
},
|
||||
{
|
||||
"message rejection",
|
||||
":KB2ICI-14:rej003",
|
||||
&Message{
|
||||
ID: 3,
|
||||
IsRejection: true,
|
||||
Recipient: "KB2ICI-14",
|
||||
},
|
||||
},
|
||||
{
|
||||
"bulletin",
|
||||
":BLN3 :Snow expected in Tampa RSN",
|
||||
&Message{
|
||||
ID: 3,
|
||||
IsBulletin: true,
|
||||
Recipient: "BLN3",
|
||||
Text: "Snow expected in Tampa RSN",
|
||||
},
|
||||
},
|
||||
{
|
||||
"annoucement",
|
||||
":BLNQ :Mt St Helen digi will be QRT this weekend",
|
||||
&Message{
|
||||
IsBulletin: true,
|
||||
Recipient: "BLNQ",
|
||||
AnnoucementID: 'Q',
|
||||
Text: "Mt St Helen digi will be QRT this weekend",
|
||||
},
|
||||
},
|
||||
{
|
||||
"group bulletin 4 to the WX group",
|
||||
":BLN4WX :Stand by your snowplows",
|
||||
&Message{
|
||||
ID: 4,
|
||||
IsBulletin: true,
|
||||
Recipient: "BLN4WX",
|
||||
Group: "WX",
|
||||
Text: "Stand by your snowplows",
|
||||
},
|
||||
},
|
||||
{
|
||||
"national weather service alert",
|
||||
":NWS-WARN :092010z,THUNDER_STORM,AR_ASHLEY,{S9JbA",
|
||||
&Message{
|
||||
IsBulletin: true,
|
||||
Recipient: "NWS-WARN",
|
||||
Severity: "WARN",
|
||||
Text: "092010z,THUNDER_STORM,AR_ASHLEY,{S9JbA",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var decoder messageDecoder
|
||||
for _, test := range tests {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
frame := &Frame{Raw: test.Raw}
|
||||
if !decoder.CanDecode(frame) {
|
||||
t.Fatalf("%T can't decode %q", decoder, test.Raw)
|
||||
}
|
||||
|
||||
v, err := decoder.Decode(frame)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testCompareMessage(t, test.Want, v)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testCompareMessage(t *testing.T, want *Message, value any) {
|
||||
t.Helper()
|
||||
|
||||
test, ok := value.(*Message)
|
||||
if !ok {
|
||||
t.Fatalf("expected data to be a %T, got %T", want, value)
|
||||
return
|
||||
}
|
||||
|
||||
if test.ID != want.ID {
|
||||
t.Errorf("expected id %d, got %d", want.ID, test.ID)
|
||||
}
|
||||
if test.IsAcknowledge != want.IsAcknowledge {
|
||||
t.Errorf("expected is acknowledge %t, got %t", want.IsAcknowledge, test.IsAcknowledge)
|
||||
}
|
||||
if test.IsRejection != want.IsRejection {
|
||||
t.Errorf("expected is rejection %t, got %t", want.IsRejection, test.IsRejection)
|
||||
}
|
||||
if test.IsBulletin != want.IsBulletin {
|
||||
t.Errorf("expected is bulletin %t, got %t", want.IsBulletin, test.IsBulletin)
|
||||
}
|
||||
if test.Recipient != want.Recipient {
|
||||
t.Errorf("expected recipient %q, got %q", want.Recipient, test.Recipient)
|
||||
}
|
||||
if test.AnnoucementID != want.AnnoucementID {
|
||||
t.Errorf("expected annoucement id %q, got %q", want.AnnoucementID, test.AnnoucementID)
|
||||
}
|
||||
if test.Group != want.Group {
|
||||
t.Errorf("expected group %q, got %q", want.Group, test.Group)
|
||||
}
|
||||
if test.Severity != want.Severity {
|
||||
t.Errorf("expected severity %q, got %q", want.Severity, test.Severity)
|
||||
}
|
||||
if test.Text != want.Text {
|
||||
t.Errorf("expected text %q, got %q", want.Text, test.Text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user