Files
openfsd/protocol/plane_info_response_test.go
Reese Norris 05ed593a4b initial commit
2024-04-04 19:40:43 -07:00

170 lines
3.5 KiB
Go

package protocol
import (
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/assert"
"testing"
)
func TestParsePlaneInfoResponsePDU(t *testing.T) {
V = validator.New(validator.WithRequiredStructEnabled())
tests := []struct {
name string
packet string
want *PlaneInfoResponsePDU
wantErr error
}{
{
"Valid Info With All Fields",
"#SBATC:PILOT:PI:GEN:EQUIPMENT=A320:AIRLINE=Delta:LIVERY=Standard:CSL=ModelABC\r\n",
&PlaneInfoResponsePDU{
From: "ATC",
To: "PILOT",
Equipment: "A320",
Airline: "Delta",
Livery: "Standard",
CSL: "ModelABC",
},
nil,
},
{
"Valid Info With Minimum Required Fields",
"#SBATC:PILOT:PI:GEN:EQUIPMENT=A320\r\n",
&PlaneInfoResponsePDU{
From: "ATC",
To: "PILOT",
Equipment: "A320",
Airline: "",
Livery: "",
CSL: "",
},
nil,
},
{
"Invalid - Missing EQUIPMENT Prefix",
"#SBATC:PILOT:PI:GEN:A320:LIVERY=Standard\r\n",
nil,
NewGenericFSDError(SyntaxError),
},
{
"Invalid - Wrong HEADER Prefix",
"$SBATC:PILOT:PI:GEN:EQUIPMENT=A320\r\n",
nil,
NewGenericFSDError(SyntaxError),
},
{
"Invalid - Field Count Less",
"#SBATC:PILOT:PI:GEN\r\n",
nil,
NewGenericFSDError(SyntaxError),
},
{
"Invalid - Field Count More",
"#SBATC:PILOT:PI:GEN:EQUIPMENT=A320:AIRLINE=Delta:LIVERY=Standard:CSL=ModelABC:ExtraField\r\n",
nil,
NewGenericFSDError(SyntaxError),
},
{
"Invalid - No From Field",
"#SB:PILOT:PI:GEN:EQUIPMENT=A320\r\n",
nil,
NewGenericFSDError(SyntaxError),
},
{
"Invalid - No To Field",
"#SBATC::PI:GEN:EQUIPMENT=A320\r\n",
nil,
NewGenericFSDError(SyntaxError),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Perform the parsing
result, err := ParsePlaneInfoResponsePDU(tc.packet)
// Check the error
if tc.wantErr != nil {
assert.Error(t, err)
assert.EqualError(t, err, tc.wantErr.Error())
} else {
assert.NoError(t, err)
}
// Verify the result
assert.Equal(t, tc.want, result)
})
}
}
func TestPlaneInfoResponsePDU_Serialize(t *testing.T) {
tests := []struct {
name string
pdu *PlaneInfoResponsePDU
wantStr string
}{
{
"All Fields Present",
&PlaneInfoResponsePDU{
From: "ATC",
To: "PILOT",
Equipment: "A320",
Airline: "Delta",
Livery: "Standard",
CSL: "ModelABC",
},
"#SBATC:PILOT:PI:GEN:EQUIPMENT=A320:AIRLINE=Delta:LIVERY=Standard:CSL=ModelABC\r\n",
},
{
"Only Required Fields",
&PlaneInfoResponsePDU{
From: "ATC",
To: "PILOT",
Equipment: "B737",
},
"#SBATC:PILOT:PI:GEN:EQUIPMENT=B737\r\n",
},
{
"With Airline",
&PlaneInfoResponsePDU{
From: "CTRL",
To: "PLANE",
Equipment: "E170",
Airline: "United",
},
"#SBCTRL:PLANE:PI:GEN:EQUIPMENT=E170:AIRLINE=United\r\n",
},
{
"With Livery",
&PlaneInfoResponsePDU{
From: "GROUND",
To: "ACFT",
Equipment: "CRJ2",
Livery: "BlueSky",
},
"#SBGROUND:ACFT:PI:GEN:EQUIPMENT=CRJ2:LIVERY=BlueSky\r\n",
},
{
"With CSL",
&PlaneInfoResponsePDU{
From: "TWR",
To: "JET",
Equipment: "CONC",
CSL: "Vintage",
},
"#SBTWR:JET:PI:GEN:EQUIPMENT=CONC:CSL=Vintage\r\n",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Perform the serialization
gotStr := tc.pdu.Serialize()
// Verify the serialized output
assert.Equal(t, tc.wantStr, gotStr)
})
}
}