forked from adrianmo/go-nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ths_test.go
167 lines (165 loc) · 4.59 KB
/
ths_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package nmea_test
import (
. "github.com/munnik/go-nmea"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
)
var _ = Describe("THS", func() {
var (
sentence Sentence
parsed THS
err error
raw string
)
Describe("Parsing", func() {
JustBeforeEach(func() {
sentence, err = Parse(raw)
if sentence != nil {
parsed = sentence.(THS)
} else {
parsed = THS{}
}
})
Context("a valid sentence with status set to autonomous", func() {
BeforeEach(func() {
raw = "$INTHS,123.456,A*20"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid THS struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Heading": Equal(NewFloat64(123.456)),
"Status": Equal(NewString(AutonomousTHS)),
}))
})
})
Context("a valid sentence with status set to estimated", func() {
BeforeEach(func() {
raw = "$INTHS,123.456,E*24"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid THS struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Heading": Equal(NewFloat64(123.456)),
"Status": Equal(NewString(EstimatedTHS)),
}))
})
})
Context("a valid sentence with status set to manual", func() {
BeforeEach(func() {
raw = "$INTHS,123.456,M*2C"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid THS struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Heading": Equal(NewFloat64(123.456)),
"Status": Equal(NewString(ManualTHS)),
}))
})
})
Context("a valid sentence with status set to simulator", func() {
BeforeEach(func() {
raw = "$INTHS,123.456,S*32"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid THS struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Heading": Equal(NewFloat64(123.456)),
"Status": Equal(NewString(SimulatorTHS)),
}))
})
})
Context("a valid sentence with status set to invalid", func() {
BeforeEach(func() {
raw = "$INTHS,,V*1E"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid THS struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Heading": Equal(NewInvalidFloat64("strconv.ParseFloat: parsing \"\": invalid syntax")),
"Status": Equal(NewString(InvalidTHS)),
}))
})
})
Context("a sentence with a non existing status", func() {
BeforeEach(func() {
raw = "$INTHS,123.456,B*23"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid THS struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Heading": Equal(NewFloat64(123.456)),
"Status": Equal(NewInvalidString("not a valid option")),
}))
})
})
Context("a sentence with an invalid heading", func() {
BeforeEach(func() {
raw = "$INTHS,XXX,A*51"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid THS struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Heading": Equal(NewInvalidFloat64("strconv.ParseFloat: parsing \"XXX\": invalid syntax")),
"Status": Equal(NewString(AutonomousTHS)),
}))
})
})
Context("a sentence with a bad checksum", func() {
BeforeEach(func() {
raw = "$INTHS,123.456,A*21"
})
It("returns an error", func() {
Expect(err).To(MatchError("nmea: sentence checksum mismatch [20 != 21]"))
})
It("returns nil", func() {
Expect(sentence).To(BeNil())
})
})
})
Describe("Getting data from a THS struct", func() {
BeforeEach(func() {
parsed = THS{
Heading: NewFloat64(TrueDirectionDegrees),
Status: NewString(SimulatorTHS),
}
})
Context("when having a complete struct", func() {
It("returns a valid true heading", func() {
Expect(parsed.GetTrueHeading()).To(BeNumerically("~", TrueDirectionRadians, 0.00001))
})
})
Context("when having a struct with missing heading", func() {
JustBeforeEach(func() {
parsed.Heading = NewInvalidFloat64("")
})
It("returns an error", func() {
_, err := parsed.GetTrueHeading()
Expect(err).To(HaveOccurred())
})
})
Context("when having a struct with status flag set to invalid", func() {
JustBeforeEach(func() {
parsed.Status = NewString(InvalidTHS)
})
It("returns an error", func() {
_, err := parsed.GetTrueHeading()
Expect(err).To(HaveOccurred())
})
})
})
})