From 983d3c9e3dfdb35c7c9bc8cb6a119a3b7eb3c96a Mon Sep 17 00:00:00 2001 From: seiya <20365512+seiyab@users.noreply.github.com> Date: Tue, 7 May 2024 22:34:28 +0900 Subject: [PATCH] enable root-level format for primitive-like types --- format.go | 4 +++- teq_customized_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/format.go b/format.go index 1eef9fb..925e8fa 100644 --- a/format.go +++ b/format.go @@ -21,7 +21,9 @@ func (teq Teq) report(expected, actual any) string { return simple } k := ve.Kind() - if k != reflect.Struct && + _, ok := teq.formats[ve.Type()] + if !ok && + k != reflect.Struct && k != reflect.Map && k != reflect.Slice && k != reflect.Array && diff --git a/teq_customized_test.go b/teq_customized_test.go index 32ee024..3de143b 100644 --- a/teq_customized_test.go +++ b/teq_customized_test.go @@ -178,6 +178,31 @@ differences: } assert.Equal(t, []string{expected}, mt.errors) }) + + t.Run("reflect.Kind", func(t *testing.T) { + tq := teq.New() + tq.AddFormat(func(kind reflect.Kind) string { + return kind.String() + }) + + mt := &mockT{} + tq.Equal(mt, reflect.Int, reflect.String) + if len(mt.errors) != 1 { + t.Fatalf("expected 1 error, got %d", len(mt.errors)) + } + expected := `not equal +differences: +--- expected ++++ actual +@@ -1 +1 @@ +-int ++string +` + if mt.errors[0] != expected { + t.Errorf("expected %q, got %q", expected, mt.errors[0]) + } + assert.Equal(t, expected, mt.errors[0]) + }) } func utc(d time.Time) time.Time {