From 85b40570328ca9b9edef963084200ce8d15adeb0 Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Wed, 24 Apr 2024 14:25:12 +0200 Subject: [PATCH] don't rely on testify to compare structs --- bridges/otellogrus/hook_test.go | 53 +++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/bridges/otellogrus/hook_test.go b/bridges/otellogrus/hook_test.go index 1a2838e4a13..c3e92476573 100644 --- a/bridges/otellogrus/hook_test.go +++ b/bridges/otellogrus/hook_test.go @@ -3,6 +3,7 @@ package otellogrus import ( + "slices" "testing" "time" @@ -112,7 +113,7 @@ func TestHookFire(t *testing.T) { for _, s := range rec.Result() { if k == s.Name { - assert.Equal(t, v, s.Records) + assertRecords(t, v, s.Records) found = true } } @@ -212,7 +213,7 @@ func TestConvertFields(t *testing.T) { }, } { t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, convertFields(tt.fields), tt.wantKeyValue) + assertKeyValues(t, tt.wantKeyValue, convertFields(tt.fields)) }) } } @@ -226,3 +227,51 @@ func buildRecord(body log.Value, timestamp time.Time, severity log.Severity, att return record } + +func assertKeyValues(t *testing.T, want, got []log.KeyValue) { + t.Helper() + if !slices.EqualFunc(want, got, log.KeyValue.Equal) { + t.Errorf("KeyValues are not equal:\nwant: %v\ngot: %v", want, got) + } +} + +func assertBody(t *testing.T, want log.Value, got log.Value) { + t.Helper() + if !got.Equal(want) { + t.Errorf("Body value is not equal:\nwant: %v\ngot: %v", want, got) + } +} + +func assertAttributes(t *testing.T, want, got log.Record) { + t.Helper() + + var wantAttr []log.KeyValue + want.WalkAttributes(func(kv log.KeyValue) bool { + wantAttr = append(wantAttr, kv) + return true + }) + var gotAttr []log.KeyValue + got.WalkAttributes(func(kv log.KeyValue) bool { + gotAttr = append(gotAttr, kv) + return true + }) + + if !slices.EqualFunc(wantAttr, gotAttr, log.KeyValue.Equal) { + t.Errorf("Attributes are not equal:\nwant: %v\ngot: %v", want, got) + } +} + +func assertRecords(t *testing.T, want, got []log.Record) { + t.Helper() + + assert.Equal(t, len(want), len(got)) + + for i, j := range want { + assert.Equal(t, j.Timestamp(), got[i].Timestamp()) + assert.Equal(t, j.ObservedTimestamp(), got[i].ObservedTimestamp()) + assert.Equal(t, j.Severity(), got[i].Severity()) + assert.Equal(t, j.SeverityText(), got[i].SeverityText()) + assertBody(t, j.Body(), got[i].Body()) + assertAttributes(t, j, got[i]) + } +}