Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rpc2: cbor codec #492

Merged
merged 13 commits into from
Feb 20, 2024
Prev Previous commit
Next Next commit
restore assert value
lucix-aws committed Feb 20, 2024
commit 6b6be3987cdad4844c615714e96321ae17087a40
105 changes: 102 additions & 3 deletions encoding/cbor/decode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cbor

import (
"math"
"reflect"
"strings"
"testing"
@@ -442,9 +443,107 @@ func TestDecode_Atomic(t *testing.T) {
if n != len(c.In) {
t.Errorf("didn't decode whole buffer")
}
if !reflect.DeepEqual(c.Expect, actual) {
t.Errorf("%v != %v", c.Expect, actual)
}
assertValue(t, c.Expect, actual)
})
}
}

func assertValue(t *testing.T, e, a Value) {
switch v := e.(type) {
case Uint, NegInt, Slice, String, Major7Bool, *Major7Nil, *Major7Undefined:
if !reflect.DeepEqual(e, a) {
t.Errorf("%v != %v", e, a)
}
case List:
assertList(t, v, a)
case Map:
assertMap(t, v, a)
case *Tag:
assertTag(t, v, a)
case Major7Float32:
assertMajor7Float32(t, v, a)
case Major7Float64:
assertMajor7Float64(t, v, a)
default:
t.Errorf("unrecognized variant %T", e)
}
}

func assertList(t *testing.T, e List, a Value) {
av, ok := a.(List)
if !ok {
t.Errorf("%T != %T", e, a)
return
}

if len(e) != len(av) {
t.Errorf("length %d != %d", len(e), len(av))
return
}

for i := 0; i < len(e); i++ {
assertValue(t, e[i], av[i])
}
}

func assertMap(t *testing.T, e Map, a Value) {
av, ok := a.(Map)
if !ok {
t.Errorf("%T != %T", e, a)
return
}

if len(e) != len(av) {
t.Errorf("length %d != %d", len(e), len(av))
return
}

for k, ev := range e {
avv, ok := av[k]
if !ok {
t.Errorf("missing key %s", k)
return
}

assertValue(t, ev, avv)
}
}

func assertTag(t *testing.T, e *Tag, a Value) {
av, ok := a.(*Tag)
if !ok {
t.Errorf("%T != %T", e, a)
return
}

if e.ID != av.ID {
t.Errorf("tag ID %d != %d", e.ID, av.ID)
return
}

assertValue(t, e.Value, av.Value)
}

func assertMajor7Float32(t *testing.T, e Major7Float32, a Value) {
av, ok := a.(Major7Float32)
if !ok {
t.Errorf("%T != %T", e, a)
return
}

if math.Float32bits(float32(e)) != math.Float32bits(float32(av)) {
t.Errorf("float32(%x) != float32(%x)", e, av)
}
}

func assertMajor7Float64(t *testing.T, e Major7Float64, a Value) {
av, ok := a.(Major7Float64)
if !ok {
t.Errorf("%T != %T", e, a)
return
}

if math.Float64bits(float64(e)) != math.Float64bits(float64(av)) {
t.Errorf("float64(%x) != float64(%x)", e, av)
}
}