forked from adrianmo/go-nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zda_test.go
131 lines (129 loc) · 3.45 KB
/
zda_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
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("ZDA", func() {
var (
sentence Sentence
parsed ZDA
err error
raw string
)
Describe("Parsing", func() {
JustBeforeEach(func() {
sentence, err = Parse(raw)
if sentence != nil {
parsed = sentence.(ZDA)
} else {
parsed = ZDA{}
}
})
Context("a valid sentence", func() {
BeforeEach(func() {
raw = "$GPZDA,172809.456,12,07,1996,00,00*57"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid ZDA struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Time": Equal(NewTime(17, 28, 9, 456)),
"Day": Equal(NewInt64(12)),
"Month": Equal(NewInt64(7)),
"Year": Equal(NewInt64(1996)),
"OffsetHours": Equal(NewInt64(0)),
"OffsetMinutes": Equal(NewInt64(0)),
}))
})
})
Context("a sentence with an invalid date", func() {
BeforeEach(func() {
raw = "$GPZDA,172809.456,D,M,Y,00,00*04"
})
It("returns no errors", func() {
Expect(err).NotTo(HaveOccurred())
})
It("equals a valid ZDA struct", func() {
Expect(parsed).To(MatchFields(IgnoreExtras, Fields{
"Time": Equal(NewTime(17, 28, 9, 456)),
"Day": Equal(NewInvalidInt64("strconv.ParseInt: parsing \"D\": invalid syntax")),
"Month": Equal(NewInvalidInt64("strconv.ParseInt: parsing \"M\": invalid syntax")),
"Year": Equal(NewInvalidInt64("strconv.ParseInt: parsing \"Y\": invalid syntax")),
"OffsetHours": Equal(NewInt64(0)),
"OffsetMinutes": Equal(NewInt64(0)),
}))
})
})
Context("a sentence with a bad checksum", func() {
BeforeEach(func() {
raw = "$GPZDA,172809.456,12,07,1996,00,00*BA"
})
It("returns an error", func() {
Expect(err).To(MatchError("nmea: sentence checksum mismatch [57 != BA]"))
})
It("returns nil", func() {
Expect(sentence).To(BeNil())
})
})
})
Describe("Getting data from a $__ZDA sentence", func() {
BeforeEach(func() {
parsed = ZDA{
Time: Time{
Valid: true,
Hour: 20,
Minute: 05,
Second: 45,
Millisecond: 315,
},
Day: NewInt64(16),
Month: NewInt64(4),
Year: NewInt64(2021),
}
})
Context("when having a complete struct", func() {
It("returns a valid date and time", func() {
Expect(parsed.GetDateTime()).To(Equal("2021-04-16T20:05:45.315Z"))
})
})
Context("when missing time", func() {
JustBeforeEach(func() {
parsed.Time = NewInvalidTime("")
})
It("returns an error", func() {
_, err := parsed.GetDateTime()
Expect(err).To(HaveOccurred())
})
})
Context("when missing year", func() {
JustBeforeEach(func() {
parsed.Year = NewInvalidInt64("")
})
It("returns an error", func() {
_, err := parsed.GetDateTime()
Expect(err).To(HaveOccurred())
})
})
Context("when missing month", func() {
JustBeforeEach(func() {
parsed.Month = NewInvalidInt64("")
})
It("returns an error", func() {
_, err := parsed.GetDateTime()
Expect(err).To(HaveOccurred())
})
})
Context("when missing day", func() {
JustBeforeEach(func() {
parsed.Day = NewInvalidInt64("")
})
It("returns an error", func() {
_, err := parsed.GetDateTime()
Expect(err).To(HaveOccurred())
})
})
})
})