Skip to content

Commit

Permalink
pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyab committed May 3, 2024
1 parent c755e4e commit 3c15563
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
12 changes: 10 additions & 2 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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("<nil>")
}
return next(v.Elem()).ledBy("*")
}

func structFmt(v reflect.Value, next func(reflect.Value) lines) lines {
open := fmt.Sprintf("%s{", v.Type().String())
close := "}"
Expand Down
88 changes: 82 additions & 6 deletions teq_diff_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package teq_test

import (
"net/http"
"testing"
"time"

"github.com/seiyab/teq"
)
Expand Down Expand Up @@ -40,21 +42,95 @@ 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))
}
expected := `not equal
differences:
--- expected
+++ actual
@@ -1,3 +1,3 @@
[1]int{
- int(0),
+ int(1),
@@ -1 +1 @@
-*int(100)
+<nil>
`
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",
<nil>,
`
if mt.errors[0] != expected {
t.Errorf("expected %q, got %q", expected, mt.errors[0])
Expand Down

0 comments on commit 3c15563

Please sign in to comment.