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