Compare commits

...

3 Commits

2 changed files with 71 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package testutil
import "fmt" import "fmt"
import "slices" import "slices"
import "strings" import "strings"
import "reflect"
// Snake lets you compare blocks of data where the ordering of certain parts may // Snake lets you compare blocks of data where the ordering of certain parts may
// be swapped every which way. It is designed for comparing the encoding of // be swapped every which way. It is designed for comparing the encoding of
@ -92,3 +93,69 @@ func HexBytes(data []byte) string {
} }
return out.String() return out.String()
} }
// Describe returns a string representing the type and data of the given value.
func Describe(value any) string {
desc := describer { }
desc.describe(reflect.ValueOf(value))
return desc.String()
}
type describer struct {
strings.Builder
indent int
}
func (this *describer) describe(value reflect.Value) {
value = reflect.ValueOf(value.Interface())
switch value.Kind() {
case reflect.Array, reflect.Slice:
this.printf("[\n")
this.indent += 1
for index := 0; index < value.Len(); index ++ {
this.iprintf("")
this.describe(value.Index(index))
this.iprintf("\n")
}
this.indent -= 1
this.iprintf("]")
case reflect.Struct:
this.printf("struct {\n")
this.indent += 1
typ := value.Type()
for index := range typ.NumField() {
indexBuffer := [1]int { index }
this.iprintf("%s: ", typ.Field(index).Name)
this.describe(value.FieldByIndex(indexBuffer[:]))
this.iprintf("\n")
}
this.indent -= 1
this.iprintf("}\n")
case reflect.Map:
this.printf("map {\n")
this.indent += 1
iter := value.MapRange()
for iter.Next() {
this.iprintf("")
this.describe(iter.Key())
this.printf(": ")
this.describe(iter.Value())
this.iprintf("\n")
}
this.indent -= 1
this.iprintf("}\n")
case reflect.Pointer:
this.printf("& ")
this.describe(value.Elem())
default:
this.printf("<%v %v>", value.Type(), value.Interface())
}
}
func (this *describer) printf(format string, v ...any) {
fmt.Fprintf(this, format, v...)
}
func (this *describer) iprintf(format string, v ...any) {
fmt.Fprintf(this, strings.Repeat("\t", this.indent) + format, v...)
}

View File

@ -58,8 +58,8 @@ func TestEncodeAnyTable(test *testing.T) {
func TestEncodeDecodeAnyTable(test *testing.T) { func TestEncodeDecodeAnyTable(test *testing.T) {
err := testEncodeDecodeAny(test, map[uint16] any { err := testEncodeDecodeAny(test, map[uint16] any {
0xF3B9: 1, 0xF3B9: uint32(1),
0x0102: 2, 0x0102: uint32(2),
0x0000: []byte("hi!"), 0x0000: []byte("hi!"),
0xFFFF: []uint16 { 0xBEE5, 0x7777 }, 0xFFFF: []uint16 { 0xBEE5, 0x7777 },
0x1234: [][]uint16 { []uint16 { 0x5 }, []uint16 { 0x17, 0xAAAA} }, 0x1234: [][]uint16 { []uint16 { 0x5 }, []uint16 { 0x17, 0xAAAA} },
@ -183,8 +183,8 @@ func testEncodeDecodeAny(test *testing.T, value, correctValue any) error {
_, decoded, n, err := decAny(bytes) _, decoded, n, err := decAny(bytes)
if err != nil { return err } if err != nil { return err }
test.Log("got: ", decoded) test.Log("got: ", tu.Describe(decoded))
test.Log("correct:", correctValue) test.Log("correct:", tu.Describe(correctValue))
if !reflect.DeepEqual(decoded, correctValue) { if !reflect.DeepEqual(decoded, correctValue) {
return fmt.Errorf("values not equal") return fmt.Errorf("values not equal")
} }