From 3c155639e714165c05072e17a83ae16c1f9ef349 Mon Sep 17 00:00:00 2001 From: seiya <20365512+seiyab@users.noreply.github.com> Date: Fri, 3 May 2024 23:18:39 +0900 Subject: [PATCH] pointer --- format.go | 12 +++++-- teq_diff_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/format.go b/format.go index 3f5b7a0..7e9ed38 100644 --- a/format.go +++ b/format.go @@ -24,7 +24,8 @@ func (teq Teq) report(expected, actual any) string { k != reflect.Map && k != reflect.Slice && k != reflect.Array && - k != reflect.String { + k != reflect.String && + k != reflect.Pointer { return simple } if k == reflect.String { @@ -102,7 +103,7 @@ var fmts = map[reflect.Kind]func(reflect.Value, func(reflect.Value) lines) lines reflect.Array: arrayFmt, reflect.Slice: sliceFmt, reflect.Interface: todoFmt, - reflect.Pointer: todoFmt, + reflect.Pointer: pointerFmt, reflect.Struct: structFmt, reflect.Map: mapFmt, reflect.Func: todoFmt, @@ -162,6 +163,13 @@ func sliceFmt(v reflect.Value, next func(reflect.Value) lines) lines { return result } +func pointerFmt(v reflect.Value, next func(reflect.Value) lines) lines { + if v.IsNil() { + return linesOf("") + } + return next(v.Elem()).ledBy("*") +} + func structFmt(v reflect.Value, next func(reflect.Value) lines) lines { open := fmt.Sprintf("%s{", v.Type().String()) close := "}" diff --git a/teq_diff_test.go b/teq_diff_test.go index 9567d1d..cd1be42 100644 --- a/teq_diff_test.go +++ b/teq_diff_test.go @@ -1,7 +1,9 @@ package teq_test import ( + "net/http" "testing" + "time" "github.com/seiyab/teq" ) @@ -40,9 +42,35 @@ differences: func TestEqual_Format(t *testing.T) { assert := teq.New() t.Run("array", func(t *testing.T) { - t.Run("empty", func(t *testing.T) { + mt := &mockT{} + assert.Equal(mt, [1]int{0}, [1]int{1}) + if len(mt.errors) != 1 { + t.Fatalf("expected 1 error, got %d", len(mt.errors)) + } + expected := `not equal +differences: +--- expected ++++ actual +@@ -1,3 +1,3 @@ + [1]int{ +- int(0), ++ int(1), + } +` + if mt.errors[0] != expected { + t.Errorf("expected %q, got %q", expected, mt.errors[0]) + } + assert.Equal(t, expected, mt.errors[0]) + }) + + t.Run("pointer", func(t *testing.T) { + t.Run("nil", func(t *testing.T) { mt := &mockT{} - assert.Equal(mt, [1]int{0}, [1]int{1}) + x := 100 + a := &x + b := (*int)(nil) + assert.Equal(mt, a, b) + if len(mt.errors) != 1 { t.Fatalf("expected 1 error, got %d", len(mt.errors)) } @@ -50,11 +78,59 @@ func TestEqual_Format(t *testing.T) { differences: --- expected +++ actual -@@ -1,3 +1,3 @@ - [1]int{ -- int(0), -+ int(1), +@@ -1 +1 @@ +-*int(100) ++ +` + if mt.errors[0] != expected { + t.Errorf("expected %q, got %q", expected, mt.errors[0]) + } + assert.Equal(t, expected, mt.errors[0]) + }) + + t.Run("pointer of struct", func(t *testing.T) { + t.Skip() + mt := &mockT{} + assert.Equal(mt, &http.Client{Timeout: time.Second}, http.DefaultClient) + + if len(mt.errors) != 1 { + t.Fatalf("expected 1 error, got %d", len(mt.errors)) + } + expected := `not equal +differences: +--- expected ++++ actual +@@ -1 +1 @@ +- Timeout: time.Duration(0), ++ Timeout: 1s, } +` + if mt.errors[0] != expected { + t.Errorf("expected %q, got %q", expected, mt.errors[0]) + } + assert.Equal(t, expected, mt.errors[0]) + }) + + t.Run("slice of pointer", func(t *testing.T) { + ref := func(s string) *string { return &s } + mt := &mockT{} + assert.Equal(mt, + []*string{ref("a"), ref("b"), ref("c"), nil}, + []*string{ref("a"), ref("b"), ref("d"), nil}, + ) + + if len(mt.errors) != 1 { + t.Fatalf("expected 1 error, got %d", len(mt.errors)) + } + expected := `not equal +differences: +--- expected ++++ actual +@@ -3,3 +3,3 @@ + *"b", +- *"c", ++ *"d", + , ` if mt.errors[0] != expected { t.Errorf("expected %q, got %q", expected, mt.errors[0])