| 1 |
// Copyright (c) 2018-2022, 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 dark_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/dark" |
| 14 |
"github.com/maxatome/go-testdeep/internal/test" |
| 15 |
) |
| 16 |
|
| 17 |
func checkFieldValueOK(t *testing.T, |
| 18 |
s reflect.Value, fieldName string, value any, |
| 19 |
) { |
| 20 |
t.Helper() |
| 21 |
|
| 22 |
testName := "field " + fieldName |
| 23 |
|
| 24 |
fieldOrig := s.FieldByName(fieldName) |
| 25 |
test.IsFalse(t, fieldOrig.CanInterface(), testName+" + fieldOrig.CanInterface()") |
| 26 |
|
| 27 |
fieldCopy, ok := dark.CopyValue(fieldOrig) |
| 28 |
if test.IsTrue(t, ok, "Can copy "+testName) { |
| 29 |
if test.IsTrue(t, fieldCopy.CanInterface(), testName+" + fieldCopy.CanInterface()") { |
| 30 |
test.IsTrue(t, reflect.DeepEqual(fieldCopy.Interface(), value), |
| 31 |
testName+" + fieldCopy contents") |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func checkFieldValueNOK(t *testing.T, s reflect.Value, fieldName string) { |
| 37 |
t.Helper() |
| 38 |
|
| 39 |
testName := "field " + fieldName |
| 40 |
|
| 41 |
fieldOrig := s.FieldByName(fieldName) |
| 42 |
test.IsFalse(t, fieldOrig.CanInterface(), testName+" + fieldOrig.CanInterface()") |
| 43 |
|
| 44 |
_, ok := dark.CopyValue(fieldOrig) |
| 45 |
test.IsFalse(t, ok, "Could not copy "+testName) |
| 46 |
} |
| 47 |
|
| 48 |
func TestCopyValue(t *testing.T) { |
| 49 |
// Note that even if all the fields are public, a Struct cannot be copied |
| 50 |
type SubPublic struct { |
| 51 |
Public int |
| 52 |
} |
| 53 |
|
| 54 |
type SubPrivate struct { |
| 55 |
private int //nolint: unused,staticcheck |
| 56 |
} |
| 57 |
|
| 58 |
type Private struct { |
| 59 |
boolean bool |
| 60 |
integer int |
| 61 |
uinteger uint |
| 62 |
cplx complex128 |
| 63 |
flt float64 |
| 64 |
str string |
| 65 |
array [3]any |
| 66 |
slice []any |
| 67 |
hash map[any]any |
| 68 |
pint *int |
| 69 |
iface any |
| 70 |
fn func() |
| 71 |
} |
| 72 |
|
| 73 |
// |
| 74 |
// Copy OK |
| 75 |
num := 123 |
| 76 |
private := Private{ |
| 77 |
boolean: true, |
| 78 |
integer: 42, |
| 79 |
cplx: complex(2, -2), |
| 80 |
flt: 1.234, |
| 81 |
str: "foobar", |
| 82 |
array: [3]any{1, 2, SubPublic{Public: 3}}, |
| 83 |
slice: append(make([]any, 0, 10), 4, 5, SubPublic{Public: 6}), |
| 84 |
hash: map[any]any{ |
| 85 |
"foo": &SubPublic{Public: 34}, |
| 86 |
SubPublic{Public: 78}: 42, |
| 87 |
}, |
| 88 |
pint: &num, |
| 89 |
iface: &num, |
| 90 |
} |
| 91 |
privateStruct := reflect.ValueOf(private) |
| 92 |
|
| 93 |
checkFieldValueOK(t, privateStruct, "boolean", private.boolean) |
| 94 |
checkFieldValueOK(t, privateStruct, "integer", private.integer) |
| 95 |
checkFieldValueOK(t, privateStruct, "uinteger", private.uinteger) |
| 96 |
checkFieldValueOK(t, privateStruct, "cplx", private.cplx) |
| 97 |
checkFieldValueOK(t, privateStruct, "flt", private.flt) |
| 98 |
checkFieldValueOK(t, privateStruct, "str", private.str) |
| 99 |
checkFieldValueOK(t, privateStruct, "array", private.array) |
| 100 |
checkFieldValueOK(t, privateStruct, "slice", private.slice) |
| 101 |
checkFieldValueOK(t, privateStruct, "hash", private.hash) |
| 102 |
checkFieldValueOK(t, privateStruct, "pint", private.pint) |
| 103 |
checkFieldValueOK(t, privateStruct, "iface", private.iface) |
| 104 |
|
| 105 |
// |
| 106 |
// Not able to copy... |
| 107 |
private = Private{ |
| 108 |
array: [3]any{1, 2, SubPrivate{}}, |
| 109 |
slice: append(make([]any, 0, 10), &SubPrivate{}, &SubPrivate{}), |
| 110 |
hash: map[any]any{"foo": &SubPrivate{}}, |
| 111 |
iface: &SubPrivate{}, |
| 112 |
fn: func() {}, |
| 113 |
} |
| 114 |
privateStruct = reflect.ValueOf(private) |
| 115 |
|
| 116 |
checkFieldValueNOK(t, privateStruct, "array") |
| 117 |
checkFieldValueNOK(t, privateStruct, "slice") |
| 118 |
checkFieldValueNOK(t, privateStruct, "hash") |
| 119 |
checkFieldValueNOK(t, privateStruct, "iface") |
| 120 |
checkFieldValueNOK(t, privateStruct, "fn") |
| 121 |
|
| 122 |
private.hash = map[any]any{SubPrivate{}: 123} |
| 123 |
privateStruct = reflect.ValueOf(private) |
| 124 |
checkFieldValueNOK(t, privateStruct, "hash") |
| 125 |
|
| 126 |
// |
| 127 |
// nil cases |
| 128 |
private = Private{} |
| 129 |
privateStruct = reflect.ValueOf(private) |
| 130 |
checkFieldValueOK(t, privateStruct, "slice", private.slice) |
| 131 |
checkFieldValueOK(t, privateStruct, "hash", private.hash) |
| 132 |
checkFieldValueOK(t, privateStruct, "pint", private.pint) |
| 133 |
checkFieldValueOK(t, privateStruct, "iface", private.iface) |
| 134 |
} |