From b06b43f4b5a1e9b8508aa7940572a4c87b700a04 Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Thu, 26 Dec 2024 15:18:13 -0600 Subject: [PATCH] test: add support for non-strict-equality decode tests Future work will enable us to decode structures containing deeply nested pointers, which will fail strict equality checks. --- common_data_for_test.go | 13 +++++++------ decode_test.go | 21 +++++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/common_data_for_test.go b/common_data_for_test.go index 4fedd04..d71d996 100644 --- a/common_data_for_test.go +++ b/common_data_for_test.go @@ -8,12 +8,13 @@ import ( ) type TestData struct { - Name string - Value interface{} - DecodeValue interface{} // used when the document cannot encode parts of Value - Documents map[int][]byte - SkipDecode map[int]bool - SkipEncode map[int]bool + Name string + Value interface{} + DecodeValue interface{} // used when the document cannot encode parts of Value + TestDecodedValue func(interface{}) error + Documents map[int][]byte + SkipDecode map[int]bool + SkipEncode map[int]bool } type SparseBundleHeader struct { diff --git a/decode_test.go b/decode_test.go index adccbcb..32ef355 100644 --- a/decode_test.go +++ b/decode_test.go @@ -126,11 +126,11 @@ func TestDecode(t *testing.T) { expVal = expReflect.Interface() results := make(map[int]interface{}) - for fmt, doc := range test.Documents { - if test.SkipDecode[fmt] { + for format, doc := range test.Documents { + if test.SkipDecode[format] { return } - subtest(t, FormatNames[fmt], func(t *testing.T) { + subtest(t, FormatNames[format], func(t *testing.T) { val := reflect.New(expReflect.Type()).Interface() _, err := Unmarshal(doc, val) if err != nil { @@ -144,9 +144,18 @@ func TestDecode(t *testing.T) { val = valReflect.Interface() } - results[fmt] = val - if !reflect.DeepEqual(expVal, val) { - t.Logf("Expected: %#v\n", expVal) + results[format] = val + var passErr error + if test.TestDecodedValue == nil { + if !reflect.DeepEqual(expVal, val) { + passErr = fmt.Errorf("Expected: %#v", expVal) + } + } else { + passErr = test.TestDecodedValue(val) + } + + if passErr != nil { + t.Logf("%v\n", passErr) t.Logf("Received: %#v\n", val) t.Fail() }