Files

189 lines
5.5 KiB
Go
Raw Permalink Normal View History

2021-01-04 15:34:44 +08:00
package parser
import (
"github.com/stretchr/testify/assert"
"testing"
)
2021-01-05 12:43:04 +08:00
func TestAirportAndTimeRegex(t *testing.T) {
ast := assert.New(t)
2021-01-05 18:04:17 +08:00
text := "-ZBTJ0554"
2021-01-05 12:43:04 +08:00
fields := getFields(DepRegex, text)
ast.Equal(1, len(fields), "Should get 1 filed")
ast.Equal("ZBTJ0554", fields[KeyDeparture])
fields = getFields(DestinationRegex, text)
ast.Equal(2, len(fields))
ast.Equal("ZBTJ", fields["dest"])
ast.Equal("0554", fields["time"])
}
func TestPlaneRegex(t *testing.T) {
ast := assert.New(t)
2021-01-05 18:04:17 +08:00
text := "-JAE7433/A0132"
2021-01-05 12:43:04 +08:00
fields := getFields(PlaneRegex, text)
ast.Equal(1, len(fields), "Should get plane value")
ast.Equal("JAE7433/A0132", fields[KeyPlane])
}
2021-01-05 18:04:17 +08:00
func TestAircraftRegex(t *testing.T) {
ast := assert.New(t)
text := "-B763/H-SDHIPRWZ/S"
fields := getFields(AircraftRegex, text)
ast.Equal(1, len(fields))
ast.Equal("B763/H", fields[KeyAircraft])
}
2021-01-05 12:43:04 +08:00
func TestDofRegex(t *testing.T) {
ast := assert.New(t)
text := "DOF/120820"
fields := getFields(DofRegex, text)
ast.Equal(1, len(fields), "Should get dof value")
ast.Equal("DOF/120820", fields[KeyDof])
d := parseDof(text)
ast.Equal(2012, d.Year())
ast.Equal(8, int(d.Month()))
ast.Equal(20, d.Day())
}
2021-01-05 18:04:17 +08:00
func TestGetOtherRegex(t *testing.T) {
ast := assert.New(t)
regex := getOtherRegexWithName("p1")
ast.Equal(`-?(?P<p1>[A-Z0-9\/ \n\r]+)`, regex)
}
func TestOthers(t *testing.T) {
ast := assert.New(t)
text := `-N0473F320 NOPIK1B
NOPIK G597 GONAV/N0473F320 G597 AGAVO/K0876S0980 G597 DONVO A326
ANRAT ANRATA`
fields := getFields(OtherRegex, text)
ast.Equal(1, len(fields))
ast.Equal(text[1:], fields["NAME"])
text = "-N0473F320 NOPIK1B"
fields = getFields(OtherRegex, text)
ast.Equal(1, len(fields))
ast.Equal(text[1:], fields["NAME"])
}
2021-01-05 12:43:04 +08:00
func TestArrDep(t *testing.T) {
2021-01-04 15:34:44 +08:00
ast := assert.New(t)
s := `ZCZC TMQ2526 141605
FF ZBTJZPZX
141604 ZBACZQZX
2021-01-05 12:43:04 +08:00
(ARR-JAE7433/A0132-RKSI-ZBTJ1604)
2021-01-04 15:34:44 +08:00
NNNN`
tele, err := Parse(s)
ast.Nil(err, "Should be no error")
ast.Equal(tele.Complete, true, "Should be complete telegram")
ast.Equal(4, len(tele.Fields), "get 4 lines")
ast.Equal("ZCZC TMQ2526 141605", tele.Fields[0])
ast.Equal(2526, tele.Sequence, "TMQ must be 2526")
ast.Equal(false, tele.Type == "PLN", "Not a plan")
ast.Equal("ARR", tele.Type, "Flop type is ARR")
ast.Equal("JAE7433", tele.FlightNumber, "flight number is JAE7433")
ast.Equal("A0132", tele.RegisterNumber, "register number is A0132")
ast.Equal("RKSI", tele.Departure, "departure airport is RKSI")
ast.Equal("ZBTJ", tele.Destination, "destination airport is ZBTJ")
ast.Equal(16, tele.DestinationTime.Hour())
ast.Equal(4, tele.DestinationTime.Minute())
2021-01-05 12:43:04 +08:00
s = `ZCZC TMQ2526 141605
FF ZBTJZPZX
141604 ZBACZQZX
(DEP-JAE7433/A0132-RKSI1604-ZBTJ-DOF/120820)
NNNN`
tele, err = Parse(s)
ast.Nil(err, "Should be no error")
ast.Equal(tele.Complete, true, "Should be complete telegram")
ast.Equal(4, len(tele.Fields), "get 4 lines")
ast.Equal("ZCZC TMQ2526 141605", tele.Fields[0])
ast.Equal(2526, tele.Sequence, "TMQ must be 2526")
ast.Equal(false, tele.Type == "PLN", "Not a plan")
ast.Equal("DEP", tele.Type, "Flop type is DEP")
ast.Equal("JAE7433", tele.FlightNumber, "flight number is JAE7433")
ast.Equal("A0132", tele.RegisterNumber, "register number is A0132")
ast.Equal("RKSI", tele.Departure, "departure airport is RKSI")
ast.Equal("ZBTJ", tele.Destination, "destination airport is ZBTJ")
ast.Equal(16, tele.DepartureTime.Hour())
ast.Equal(4, tele.DepartureTime.Minute())
2021-01-04 15:34:44 +08:00
ast.Equal(2012, tele.DateOfFlight.Year())
ast.Equal(8, int(tele.DateOfFlight.Month()))
ast.Equal(20, tele.DateOfFlight.Day())
s = `ZCZC TMQ2526 141605
FF ZBTJZPZX
141604 ZBACZQZX
(ARR-JAE7433/A0132-RKSI)
NNNN`
2021-01-04 15:35:32 +08:00
if tele, _ = Parse(s); tele != nil {
ast.Equal(1, len(tele.ParseErrors))
}
2021-01-04 15:34:44 +08:00
}
2021-01-04 16:02:13 +08:00
2021-01-04 18:07:48 +08:00
func TestFpl(t *testing.T) {
ast := assert.New(t)
text := `ZCZC TMQ2628 151601
FF ZBTJZPZX
151600 RKSSAARO
(FPL-AAR997-IS
-B763/H-SDHIPRWZ/S
-RKSI0120
-N0473F320 NOPIK1B NOPIK G597 GONAV/N0473F320 G597 AGAVO/K0876S0980
G597 DONVO A326 ANRAT ANRAT2A
-ZBTJ0126 ZBAA
-EET/ZSHA0025 ZYSH0039 ZBPE0059 REG/HL7507 SEL/KRHJ OPR/AAR
DOF/090516 RMK/TCAS II EQUIPPED NAV/RNAV1 RNAV5 STS/CARGO)
NNNN
`
tele, err := Parse(text)
ast.Nil(err, "Should be no error")
ast.Equal(12, len(tele.Fields), "get 12 lines")
ast.Equal(2628, tele.Sequence, "TMQ must be 2628")
ast.Equal(false, tele.Type == "PLN", "Not a plan")
ast.Equal("FPL", tele.Type, "Flop type is FPL")
ast.Equal("AAR997", tele.FlightNumber, "flight number is AAR997")
2021-01-05 18:04:17 +08:00
ast.Equal("B763/H", tele.Aircraft, "aircraft is B763/H")
2021-01-04 18:07:48 +08:00
ast.Equal("RKSI", tele.Departure, "departure airport is RKSI")
ast.Equal(1, tele.DepartureTime.Hour())
ast.Equal(20, tele.DepartureTime.Minute())
ast.Equal("ZBTJ", tele.Destination)
ast.Equal(1, tele.DestinationTime.Hour())
ast.Equal(26, tele.DestinationTime.Minute())
ast.Equal(2009, tele.DateOfFlight.Year())
ast.Equal(5, int(tele.DateOfFlight.Month()))
ast.Equal(16, tele.DateOfFlight.Day())
}
func TestDelay(t *testing.T) {
ast := assert.New(t)
text := `ZCZC TMQ2626 142159
FF ZBTJZXZX
142158 ZBTJZPZX
(DLA-JAE7433-NZAA0145-VTBS-DOF/091120)
NNNN`
tele, err := Parse(text)
ast.Nil(err, "Should be no error")
ast.Equal(4, len(tele.Fields), "get 4 lines")
ast.Equal("ZCZC TMQ2626 142159", tele.Fields[0])
ast.Equal(2626, tele.Sequence, "TMQ must be 2626")
ast.Equal("DLA", tele.Type, "Flop type is DLA")
ast.Equal("JAE7433", tele.FlightNumber)
2021-01-05 13:28:01 +08:00
ast.Equal("NZAA", tele.Departure)
ast.Equal(1, tele.DepartureTime.Hour())
ast.Equal(45, tele.DepartureTime.Minute())
ast.Equal(2009, tele.DateOfFlight.Year())
ast.Equal(11, int(tele.DateOfFlight.Month()))
ast.Equal(20, tele.DateOfFlight.Day())
2021-01-04 18:07:48 +08:00
}