Skip to content

Commit edd6901

Browse files
moar tests
Signed-off-by: Thomas Fossati <[email protected]>
1 parent 3114fa2 commit edd6901

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

cmw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func NewMonad(mediaType any, value []byte, indicators ...Indicator) *CMW {
9090

9191
func (o CMW) GetMonadType() string { return o.monad.getType() }
9292
func (o CMW) GetMonadValue() []byte { return o.monad.getValue() }
93-
func (o CMW) GetMonadIndicator() Indicator { return o.ind }
93+
func (o CMW) GetMonadIndicator() Indicator { return o.monad.getIndicator() }
9494
func (o *CMW) UseCBORTagFormat() { o.monad.format = FormatCBORTag }
9595

9696
// NewCollection instantiate a new Collection CMW with the supplied __cmwc_t

collection_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func Test_Collection_JSON_Deserialize_ok(t *testing.T) {
2424
err := actual.UnmarshalJSON(tv)
2525
assert.NoError(t, err)
2626

27+
assert.Equal(t, actual.GetFormat().String(), "JSON collection")
28+
2729
a, err := actual.GetCollectionItem("a")
2830
assert.NoError(t, err)
2931
assert.Equal(t, FormatJSONRecord, a.GetFormat())
@@ -79,6 +81,8 @@ func Test_Collection_CBOR_Deserialize_ok(t *testing.T) {
7981
err := actual.UnmarshalCBOR(tv)
8082
assert.NoError(t, err)
8183

84+
assert.Equal(t, actual.GetFormat().String(), "CBOR collection")
85+
8286
one, err := actual.GetCollectionItem(uint64(1))
8387
assert.NoError(t, err)
8488
assert.Equal(t, KindMonad, one.GetKind())

monad.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ func (o *monad) UnmarshalCBOR(b []byte) error {
5555
}
5656
o.format = FormatCBORTag
5757
} else {
58-
return fmt.Errorf("want CBOR Tag or CBOR array, got 0x%02x", b[0])
58+
// unreachable
59+
panic(fmt.Sprintf("want CBOR Tag or CBOR array, got 0x%02x", b[0]))
5960
}
6061

6162
return nil

monad_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,35 @@ func Test_UnmarshalCBOR_tag_ko(t *testing.T) {
411411
})
412412
}
413413
}
414+
415+
func Test_UnmarshalCBOR_ko(t *testing.T) {
416+
tests := []struct {
417+
name string
418+
tv []byte
419+
expectedErr string
420+
}{
421+
{
422+
"weird",
423+
[]byte{0x00},
424+
"want CBOR map, CBOR array or CBOR Tag start symbols, got: 0x00",
425+
},
426+
{
427+
"empty",
428+
[]byte{},
429+
"empty buffer",
430+
},
431+
{
432+
"abruptly truncated collection",
433+
[]byte{0xa1, 0x00},
434+
"unmarshaling CBOR collection: unexpected EOF",
435+
},
436+
}
437+
438+
for _, tt := range tests {
439+
t.Run(tt.name, func(t *testing.T) {
440+
var cmw CMW
441+
err := cmw.UnmarshalCBOR(tt.tv)
442+
assert.EqualError(t, err, tt.expectedErr)
443+
})
444+
}
445+
}

utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ func mustHexDecode(s string) []byte {
3838
func startJSONCollection(c byte) bool { return c == 0x7b }
3939
func startJSONRecord(c byte) bool { return c == 0x5b }
4040
func startCBORCollection(c byte) bool { return c >= 0xa0 && c <= 0xbb || c == 0xbf }
41-
func startCBORRecord(c byte) bool { return c == 0x82 || c == 0x83 }
41+
func startCBORRecord(c byte) bool { return c == 0x82 || c == 0x83 || c == 0x9f }
4242
func startCBORTag(c byte) bool { return c >= 0xda }

0 commit comments

Comments
 (0)