diff --git a/cbor_test.go b/cbor_test.go index 193203e..7c8bdac 100644 --- a/cbor_test.go +++ b/cbor_test.go @@ -41,3 +41,69 @@ func Test_DefaultCBORDecoder(t *testing.T) { require.NoError(t, err) require.Equal(t, "Hello World", ss.ImportantString) } + +func Test_DefaultCBOREncoderWithEmptyString(t *testing.T) { + t.Parallel() + + var ( + ss = &sampleStructure{ + ImportantString: "", + } + importantString = `a170696d706f7274616e745f737472696e6760` + cborEncoder CBORMarshal = cbor.Marshal + ) + + raw, err := cborEncoder(ss) + require.NoError(t, err) + + require.Equal(t, hex.EncodeToString(raw), importantString) +} + +func Test_DefaultCBORDecoderWithEmptyString(t *testing.T) { + t.Parallel() + + var ( + ss sampleStructure + importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e6760") + cborDecoder CBORUnmarshal = cbor.Unmarshal + ) + if err != nil { + t.Error("Failed to decode hex string") + } + + err = cborDecoder(importantString, &ss) + require.NoError(t, err) + require.Equal(t, "", ss.ImportantString) +} + +func Test_DefaultCBOREncoderWithUnitializedStruct(t *testing.T) { + t.Parallel() + + var ( + ss sampleStructure + importantString = `a170696d706f7274616e745f737472696e6760` + cborEncoder CBORMarshal = cbor.Marshal + ) + + raw, err := cborEncoder(ss) + require.NoError(t, err) + + require.Equal(t, hex.EncodeToString(raw), importantString) +} + +func Test_DefaultCBORDecoderWithUnitializedStruct(t *testing.T) { + t.Parallel() + + var ( + ss sampleStructure + importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e6760") + cborDecoder CBORUnmarshal = cbor.Unmarshal + ) + if err != nil { + t.Error("Failed to decode hex string") + } + + err = cborDecoder(importantString, &ss) + require.NoError(t, err) + require.Equal(t, ss, ss) +}