From e9ca0b2450cc1e8aec8ffba771c9c788223f0a1e Mon Sep 17 00:00:00 2001 From: Eliott Bouhana Date: Mon, 26 Feb 2024 18:13:22 +0100 Subject: [PATCH 1/2] add back and forth encode decode testsuite Signed-off-by: Eliott Bouhana --- msgp/write_bytes_test.go | 133 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/msgp/write_bytes_test.go b/msgp/write_bytes_test.go index 69866fe1..edc2437e 100644 --- a/msgp/write_bytes_test.go +++ b/msgp/write_bytes_test.go @@ -3,6 +3,8 @@ package msgp import ( "bytes" "math" + "reflect" + "strings" "testing" "time" ) @@ -348,3 +350,134 @@ func BenchmarkAppendTime(b *testing.B) { AppendTime(buf[0:0], t) } } + +// TestEncodeDecode does a back-and-forth test of encoding and decoding and compare the value with a given output. +func TestEncodeDecode(t *testing.T) { + for _, tc := range []struct { + name string + input interface{} + output interface{} + encodeError string + }{ + { + name: "nil", + input: nil, + }, + { + name: "bool", + input: true, + }, + { + name: "int", + input: int64(42), + }, + { + name: "float", + input: 3.14159, + }, + { + name: "string", + input: "hello", + }, + { + name: "bytes", + input: []byte("hello"), + }, + { + name: "array-empty", + input: []interface{}{}, + }, + { + name: "array", + input: []interface{}{int64(1), int64(2), int64(3)}, + }, + { + name: "map-empty", + input: map[string]interface{}{}, + }, + { + name: "map", + input: map[string]interface{}{"a": int64(1), "b": int64(2)}, + }, + { + name: "map-interface", + input: map[string]interface{}{"a": int64(1), "b": "2"}, + }, + { + name: "map-string", + input: map[string]string{"a": "1", "b": "2"}, + output: map[string]interface{}{"a": "1", "b": "2"}, + }, + { + name: "map-array", + input: map[string][]int64{"a": {1, 2}, "b": {3}}, + output: map[string]interface{}{"a": []interface{}{int64(1), int64(2)}, "b": []interface{}{int64(3)}}, + }, + { + name: "map-map", + input: map[string]map[string]int64{"a": {"a": 1, "b": 2}, "b": {"c": 3}}, + output: map[string]interface{}{"a": map[string]interface{}{"a": int64(1), "b": int64(2)}, "b": map[string]interface{}{"c": int64(3)}}, + }, + { + name: "array-map", + input: []interface{}{map[string]interface{}{"a": int64(1), "b": "2"}, map[string]int64{"c": 3}}, + output: []interface{}{map[string]interface{}{"a": int64(1), "b": "2"}, map[string]interface{}{"c": int64(3)}}, + }, + { + name: "array-array", + input: []interface{}{[]int64{1, 2}, []interface{}{int64(3)}}, + output: []interface{}{[]interface{}{int64(1), int64(2)}, []interface{}{int64(3)}}, + }, + { + name: "array-array-map", + input: []interface{}{[]interface{}{int64(1), int64(2)}, map[string]interface{}{"c": int64(3)}}, + }, + { + name: "map-array-map", + input: map[string]interface{}{"a": []interface{}{int64(1), int64(2)}, "b": map[string]interface{}{"c": int64(3)}}, + }, + { + name: "map-invalid-keys", + input: map[interface{}]interface{}{int64(1): int64(2)}, + encodeError: "msgp: map keys must be strings", + }, + { + name: "map-nested-invalid-keys", + input: map[string]interface{}{"a": map[int64]string{1: "2"}}, + encodeError: "msgp: map keys must be strings", + }, + { + name: "invalid-type", + input: struct{}{}, + encodeError: "msgp: type \"struct {}\" not supported", + }, + } { + t.Run(tc.name, func(t *testing.T) { + // If no output is given, use the input as output + if tc.output == nil { + tc.output = tc.input + } + + buf, err := AppendIntf(nil, tc.input) + if tc.encodeError != "" { + if err == nil || !strings.Contains(err.Error(), tc.encodeError) { + t.Fatalf("expected encode error '%s' but got '%s'", tc.encodeError, err) + } + return + } + + if tc.encodeError == "" && err != nil { + t.Fatalf("expected no encode error but got '%s'", err.Error()) + } + + out, buf, err := ReadIntfBytes(buf) + if err != nil { + t.Fatalf("expected no decode error but got '%s'", err.Error()) + } + + if !reflect.DeepEqual(tc.output, out) { + t.Fatalf("expected '%v' but got '%v'", tc.input, out) + } + }) + } +} From fe78a2b98288901b5f3666bcb6010b5343374e18 Mon Sep 17 00:00:00 2001 From: Eliott Bouhana Date: Mon, 26 Feb 2024 18:18:58 +0100 Subject: [PATCH 2/2] run golangci-lint --fix Signed-off-by: Eliott Bouhana --- msgp/write_bytes_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msgp/write_bytes_test.go b/msgp/write_bytes_test.go index edc2437e..93e13ff6 100644 --- a/msgp/write_bytes_test.go +++ b/msgp/write_bytes_test.go @@ -470,7 +470,7 @@ func TestEncodeDecode(t *testing.T) { t.Fatalf("expected no encode error but got '%s'", err.Error()) } - out, buf, err := ReadIntfBytes(buf) + out, _, _ := ReadIntfBytes(buf) if err != nil { t.Fatalf("expected no decode error but got '%s'", err.Error()) }