MCPcopy
hub / github.com/rs/zerolog / TestDecodeTimestamp

Function TestDecodeTimestamp

internal/cbor/decoder_test.go:161–201  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

159}
160
161func TestDecodeTimestamp(t *testing.T) {
162 decodeTimeZone, _ = time.LoadLocation("UTC")
163 for _, tc := range internal.TimeIntegerTestcases {
164 tm := decodeTagData(getReader(tc.Binary))
165 if string(tm) != "\""+tc.RfcStr+"\"" {
166 t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), tm, tc.RfcStr)
167 }
168 }
169 for _, tc := range internal.TimeFloatTestcases {
170 tm := decodeTagData(getReader(tc.Out))
171 //Since we convert to float and back - it may be slightly off - so
172 //we cannot check for exact equality instead, we'll check it is
173 //very close to each other Less than a Microsecond (lets not yet do nanosec)
174
175 got, _ := time.Parse(string(tm), string(tm))
176 want, _ := time.Parse(tc.RfcStr, tc.RfcStr)
177 if got.Sub(want) > time.Microsecond {
178 t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Out)), tm, tc.RfcStr)
179 }
180 }
181
182 // Test with decodeTimeZone = nil to cover the else branches
183 oldTimeZone := decodeTimeZone
184 decodeTimeZone = nil
185 defer func() { decodeTimeZone = oldTimeZone }()
186
187 for _, tc := range internal.TimeIntegerTestcases {
188 tm := decodeTagData(getReader(tc.Binary))
189 if string(tm) != "\""+tc.RfcStr+"\"" {
190 t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), tm, tc.RfcStr)
191 }
192 }
193 for _, tc := range internal.TimeFloatTestcases {
194 tm := decodeTagData(getReader(tc.Out))
195 got, _ := time.Parse(string(tm), string(tm))
196 want, _ := time.Parse(tc.RfcStr, tc.RfcStr)
197 if got.Sub(want) > time.Microsecond {
198 t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Out)), tm, tc.RfcStr)
199 }
200 }
201}
202
203func TestDecodeNetworkAddr(t *testing.T) {
204 for _, tc := range internal.IpAddrTestCases {

Callers

nothing calls this directly

Calls 2

decodeTagDataFunction · 0.85
getReaderFunction · 0.85

Tested by

no test coverage detected