| 1 |
// Copyright (c) 2020, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package anchors_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/anchors" |
| 15 |
"github.com/maxatome/go-testdeep/internal/test" |
| 16 |
) |
| 17 |
|
| 18 |
func TestInfo(t *testing.T) { |
| 19 |
i := anchors.NewInfo() |
| 20 |
|
| 21 |
test.IsFalse(t, i.DoAnchorsPersist()) |
| 22 |
|
| 23 |
i.SetAnchorsPersist(true) |
| 24 |
test.IsTrue(t, i.DoAnchorsPersist()) |
| 25 |
|
| 26 |
i.SetAnchorsPersist(false) |
| 27 |
test.IsFalse(t, i.DoAnchorsPersist()) |
| 28 |
} |
| 29 |
|
| 30 |
func TestBuildResolveAnchor(t *testing.T) { |
| 31 |
var i anchors.Info |
| 32 |
|
| 33 |
checkResolveAnchor := func(t *testing.T, val any, opName string) { |
| 34 |
t.Helper() |
| 35 |
v1, err := i.AddAnchor(reflect.TypeOf(val), reflect.ValueOf(opName+" (1)")) |
| 36 |
if !test.NoError(t, err, "first anchor") { |
| 37 |
return |
| 38 |
} |
| 39 |
v2, err := i.AddAnchor(reflect.TypeOf(val), reflect.ValueOf(opName+" (2)")) |
| 40 |
if !test.NoError(t, err, "second anchor") { |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
op, found := i.ResolveAnchor(v1) |
| 45 |
test.IsTrue(t, found, "first anchor found") |
| 46 |
test.EqualStr(t, op.String(), opName+" (1)", "first anchor operator OK") |
| 47 |
|
| 48 |
op, found = i.ResolveAnchor(v2) |
| 49 |
test.IsTrue(t, found, "second anchor found") |
| 50 |
test.EqualStr(t, op.String(), opName+" (2)", "second anchor operator OK") |
| 51 |
} |
| 52 |
|
| 53 |
t.Run("AddAnchor basic types", func(t *testing.T) { |
| 54 |
checkResolveAnchor(t, 0, "int") |
| 55 |
checkResolveAnchor(t, int8(0), "int8") |
| 56 |
checkResolveAnchor(t, int16(0), "int16") |
| 57 |
checkResolveAnchor(t, int32(0), "int32") |
| 58 |
checkResolveAnchor(t, int64(0), "int64") |
| 59 |
|
| 60 |
checkResolveAnchor(t, uint(0), "uint") |
| 61 |
checkResolveAnchor(t, uint8(0), "uint8") |
| 62 |
checkResolveAnchor(t, uint16(0), "uint16") |
| 63 |
checkResolveAnchor(t, uint32(0), "uint32") |
| 64 |
checkResolveAnchor(t, uint64(0), "uint64") |
| 65 |
|
| 66 |
checkResolveAnchor(t, uintptr(0), "uintptr") |
| 67 |
|
| 68 |
checkResolveAnchor(t, float32(0), "float32") |
| 69 |
checkResolveAnchor(t, float64(0), "float64") |
| 70 |
|
| 71 |
checkResolveAnchor(t, complex(float32(0), 0), "complex64") |
| 72 |
checkResolveAnchor(t, complex(float64(0), 0), "complex128") |
| 73 |
|
| 74 |
checkResolveAnchor(t, "", "string") |
| 75 |
checkResolveAnchor(t, (chan int)(nil), "chan") |
| 76 |
checkResolveAnchor(t, (map[string]bool)(nil), "map") |
| 77 |
checkResolveAnchor(t, ([]int)(nil), "slice") |
| 78 |
checkResolveAnchor(t, (*time.Time)(nil), "pointer") |
| 79 |
}) |
| 80 |
|
| 81 |
t.Run("AddAnchor", func(t *testing.T) { |
| 82 |
oldAnchorableTypes := anchors.AnchorableTypes |
| 83 |
defer func() { anchors.AnchorableTypes = oldAnchorableTypes }() |
| 84 |
|
| 85 |
type ok struct{ index int } |
| 86 |
|
| 87 |
// AddAnchor for ok type |
| 88 |
err := anchors.AddAnchorableStructType(func(nextAnchor int) ok { |
| 89 |
return ok{index: 1000 + nextAnchor} |
| 90 |
}) |
| 91 |
if err != nil { |
| 92 |
t.Fatalf("AddAnchorableStructType failed: %s", err) |
| 93 |
} |
| 94 |
checkResolveAnchor(t, ok{}, "ok{}") |
| 95 |
|
| 96 |
// AddAnchor for ok convertible type |
| 97 |
type okConvert ok |
| 98 |
checkResolveAnchor(t, okConvert{}, "okConvert{}") |
| 99 |
|
| 100 |
// Replace ok type |
| 101 |
err = anchors.AddAnchorableStructType(func(nextAnchor int) ok { |
| 102 |
return ok{index: 2000 + nextAnchor} |
| 103 |
}) |
| 104 |
if err != nil { |
| 105 |
t.Fatalf("AddAnchorableStructType failed: %s", err) |
| 106 |
} |
| 107 |
if len(anchors.AnchorableTypes) != 2 { |
| 108 |
t.Fatalf("Bad number of anchored type: got=%d expected=2", |
| 109 |
len(anchors.AnchorableTypes)) |
| 110 |
} |
| 111 |
checkResolveAnchor(t, ok{}, "ok{}") |
| 112 |
|
| 113 |
// AddAnchor for builtin time.Time type |
| 114 |
checkResolveAnchor(t, time.Time{}, "time.Time{}") |
| 115 |
|
| 116 |
// AddAnchor for unknown type |
| 117 |
_, err = i.AddAnchor(reflect.TypeOf(func() {}), reflect.ValueOf(123)) |
| 118 |
if test.Error(t, err) { |
| 119 |
test.EqualStr(t, err.Error(), "func kind is not supported as an anchor") |
| 120 |
} |
| 121 |
|
| 122 |
// AddAnchor for unknown struct type |
| 123 |
_, err = i.AddAnchor(reflect.TypeOf(struct{}{}), reflect.ValueOf(123)) |
| 124 |
if test.Error(t, err) { |
| 125 |
test.EqualStr(t, |
| 126 |
err.Error(), |
| 127 |
"struct {} struct type is not supported as an anchor. Try AddAnchorableStructType") |
| 128 |
} |
| 129 |
|
| 130 |
// Struct not comparable |
| 131 |
type notComparable struct{ s []int } |
| 132 |
v := reflect.ValueOf(notComparable{s: []int{42}}) |
| 133 |
op, found := i.ResolveAnchor(v) |
| 134 |
test.IsFalse(t, found) |
| 135 |
if !reflect.DeepEqual(v.Interface(), op.Interface()) { |
| 136 |
test.EqualErrorMessage(t, op.Interface(), v.Interface()) |
| 137 |
} |
| 138 |
|
| 139 |
// Struct comparable but not anchored |
| 140 |
v = reflect.ValueOf(struct{}{}) |
| 141 |
op, found = i.ResolveAnchor(v) |
| 142 |
test.IsFalse(t, found) |
| 143 |
if !reflect.DeepEqual(v.Interface(), op.Interface()) { |
| 144 |
test.EqualErrorMessage(t, op.Interface(), v.Interface()) |
| 145 |
} |
| 146 |
|
| 147 |
// Struct anchored once, but not for this value |
| 148 |
v = reflect.ValueOf(ok{index: 42424242}) |
| 149 |
op, found = i.ResolveAnchor(v) |
| 150 |
test.IsFalse(t, found) |
| 151 |
if !reflect.DeepEqual(v.Interface(), op.Interface()) { |
| 152 |
test.EqualErrorMessage(t, op.Interface(), v.Interface()) |
| 153 |
} |
| 154 |
|
| 155 |
// Kind not supported |
| 156 |
v = reflect.ValueOf(true) |
| 157 |
op, found = i.ResolveAnchor(v) |
| 158 |
test.IsFalse(t, found) |
| 159 |
if !reflect.DeepEqual(v.Interface(), op.Interface()) { |
| 160 |
test.EqualErrorMessage(t, op.Interface(), v.Interface()) |
| 161 |
} |
| 162 |
}) |
| 163 |
|
| 164 |
t.Run("ResetAnchors", func(t *testing.T) { |
| 165 |
v, err := i.AddAnchor(reflect.TypeOf(12), reflect.ValueOf("zip")) |
| 166 |
if !test.NoError(t, err) { |
| 167 |
return |
| 168 |
} |
| 169 |
|
| 170 |
op, found := i.ResolveAnchor(v) |
| 171 |
test.IsTrue(t, found) |
| 172 |
test.EqualStr(t, op.String(), "zip") |
| 173 |
|
| 174 |
i.SetAnchorsPersist(true) |
| 175 |
i.ResetAnchors(false) |
| 176 |
|
| 177 |
op, found = i.ResolveAnchor(v) |
| 178 |
test.IsTrue(t, found) |
| 179 |
test.EqualStr(t, op.String(), "zip") |
| 180 |
|
| 181 |
i.ResetAnchors(true) |
| 182 |
_, found = i.ResolveAnchor(reflect.ValueOf(42)) |
| 183 |
test.IsFalse(t, found) |
| 184 |
|
| 185 |
i.SetAnchorsPersist(false) |
| 186 |
v, err = i.AddAnchor(reflect.TypeOf(12), reflect.ValueOf("xxx")) |
| 187 |
if !test.NoError(t, err) { |
| 188 |
return |
| 189 |
} |
| 190 |
|
| 191 |
op, found = i.ResolveAnchor(v) |
| 192 |
test.IsTrue(t, found) |
| 193 |
test.EqualStr(t, op.String(), "xxx") |
| 194 |
|
| 195 |
i.ResetAnchors(false) |
| 196 |
_, found = i.ResolveAnchor(reflect.ValueOf(42)) |
| 197 |
test.IsFalse(t, found) |
| 198 |
}) |
| 199 |
|
| 200 |
t.Run("skip", func(t *testing.T) { |
| 201 |
var i *anchors.Info |
| 202 |
|
| 203 |
_, found := i.ResolveAnchor(reflect.ValueOf(42)) |
| 204 |
test.IsFalse(t, found) |
| 205 |
|
| 206 |
i = &anchors.Info{} |
| 207 |
_, found = i.ResolveAnchor(reflect.ValueOf(42)) |
| 208 |
test.IsFalse(t, found) |
| 209 |
}) |
| 210 |
} |