forked from adrianmo/go-nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa_test.go
140 lines (138 loc) · 4.42 KB
/
rsa_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
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("RSA", func() {
var (
sentence Sentence
parsed RSA
err error
raw string
)
Describe("Parsing", func() {
JustBeforeEach(func() {
sentence, err = Parse(raw)
if sentence != nil {
parsed = sentence.(RSA)
} else {
parsed = RSA{}
}
})
Context("a valid sentence", func() {
BeforeEach(func() {
raw = "$RIRSA,3.1,A,-3.1,A*76"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid RSA struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"RudderAngleStarboard": Equal(NewFloat64(3.1)),
"StatusStarboard": Equal(NewString(ValidRSA)),
"RudderAnglePortside": Equal(NewFloat64(-3.1)),
"StatusPortside": Equal(NewString(ValidRSA)),
}))
})
})
Context("a sentence with no rsa values", func() {
BeforeEach(func() {
raw = "$RIRSA,,V,,V*5B"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid RSA struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"RudderAngleStarboard": Equal(NewInvalidFloat64("strconv.ParseFloat: parsing \"\": invalid syntax")),
"StatusStarboard": Equal(NewString(InvalidRSA)),
"RudderAnglePortside": Equal(NewInvalidFloat64("strconv.ParseFloat: parsing \"\": invalid syntax")),
"StatusPortside": Equal(NewString(InvalidRSA)),
}))
})
})
Context("a sentence with status set to invalid", func() {
BeforeEach(func() {
raw = "$RIRSA,3.1,V,-3.1,V*76"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid RSA struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"RudderAngleStarboard": Equal(NewFloat64(3.1)),
"StatusStarboard": Equal(NewString(InvalidRSA)),
"RudderAnglePortside": Equal(NewFloat64(-3.1)),
"StatusPortside": Equal(NewString(InvalidRSA)),
}))
})
})
Context("a sentence with non existing status", func() {
BeforeEach(func() {
raw = "$RIRSA,3.1,K,-3.1,K*76"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid RSA struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"RudderAngleStarboard": Equal(NewFloat64(3.1)),
"StatusStarboard": Equal(NewInvalidString("not a valid option")),
"RudderAnglePortside": Equal(NewFloat64(-3.1)),
"StatusPortside": Equal(NewInvalidString("not a valid option")),
}))
})
})
Context("a sentence with a bad checksum", func() {
BeforeEach(func() {
raw = "$RIRSA,3.1,A,-3.1,A*75"
})
It("returns an error", func() {
Expect(err).To(MatchError("nmea: sentence checksum mismatch [76 != 75]"))
})
It("returns nil", func() {
Expect(sentence).To(BeNil())
})
})
})
Describe("Getting data from a RSA struct", func() {
BeforeEach(func() {
parsed = RSA{
RudderAngleStarboard: NewFloat64(TrueDirectionDegrees),
StatusStarboard: NewString(ValidRSA),
RudderAnglePortside: NewFloat64(-TrueDirectionDegrees),
StatusPortside: NewString(ValidRSA),
}
})
Context("when having a complete struct", func() {
It("returns a valid rate of turn", func() {
Expect(parsed.GetRudderAngleStarboard()).To(BeNumerically("~", TrueDirectionRadians, 0.00001))
Expect(parsed.GetRudderAnglePortside()).To(BeNumerically("~", -TrueDirectionRadians, 0.00001))
_, err := parsed.GetRudderAngle()
Expect(err).To(HaveOccurred())
})
})
Context("when having a struct with a missing portside rudder angle", func() {
JustBeforeEach(func() {
parsed.RudderAnglePortside = NewInvalidFloat64("")
})
It("returns an error", func() {
_, err := parsed.GetRudderAnglePortside()
Expect(err).To(HaveOccurred())
Expect(parsed.GetRudderAngleStarboard()).To(BeNumerically("~", TrueDirectionRadians, 0.00001))
Expect(parsed.GetRudderAngle()).To(BeNumerically("~", TrueDirectionRadians, 0.00001))
})
})
Context("when having a struct with status flag set to invalid", func() {
JustBeforeEach(func() {
parsed.StatusStarboard = NewInvalidString("")
})
It("returns an error", func() {
_, err := parsed.GetRudderAngleStarboard()
Expect(err).To(HaveOccurred())
})
})
})
})