| 1 | package testutils |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/go-quicktest/qt" |
| 7 | ) |
| 8 | |
| 9 | func TestIsDeepCopy(t *testing.T) { |
| 10 | type s struct { |
| 11 | basic int |
| 12 | array [1]*int |
| 13 | array0 [0]int |
| 14 | ptr *int |
| 15 | slice []*int |
| 16 | ifc any |
| 17 | m map[*int]*int |
| 18 | rec *s |
| 19 | } |
| 20 | |
| 21 | key := 1 |
| 22 | copy := func() *s { |
| 23 | v := &s{ |
| 24 | 0, |
| 25 | [...]*int{new(int)}, |
| 26 | [...]int{}, |
| 27 | new(int), |
| 28 | []*int{new(int)}, |
| 29 | new(int), |
| 30 | map[*int]*int{&key: new(int)}, |
| 31 | nil, |
| 32 | } |
| 33 | v.rec = v |
| 34 | return v |
| 35 | } |
| 36 | |
| 37 | a, b := copy(), copy() |
| 38 | qt.Check(t, qt.IsNil(IsDeepCopy(a, b).Check(nil))) |
| 39 | |
| 40 | a.basic++ |
| 41 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"basic": .*`)) |
| 42 | |
| 43 | a = copy() |
| 44 | (*a.array[0])++ |
| 45 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"array": index 0: .*`)) |
| 46 | |
| 47 | a = copy() |
| 48 | a.array[0] = nil |
| 49 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"array": index 0: .*`)) |
| 50 | |
| 51 | a = copy() |
| 52 | a.array = b.array |
| 53 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"array": index 0: .*`)) |
| 54 | |
| 55 | a = copy() |
| 56 | (*a.ptr)++ |
| 57 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"ptr": .*`)) |
| 58 | |
| 59 | a = copy() |
| 60 | a.ptr = b.ptr |
| 61 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"ptr": .*`)) |
| 62 | |
| 63 | a = copy() |
| 64 | (*a.slice[0])++ |
| 65 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"slice": .*`)) |
| 66 | |
| 67 | a = copy() |
| 68 | a.slice[0] = nil |
| 69 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"slice": .*`)) |
| 70 | |
| 71 | a = copy() |
| 72 | a.slice = nil |
| 73 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"slice": .*`)) |
| 74 | |
| 75 | a = copy() |
| 76 | a.slice = b.slice |
| 77 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"slice": .*`)) |
| 78 | |
| 79 | a = copy() |
| 80 | *(a.ifc.(*int))++ |
| 81 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"ifc": .*`)) |
| 82 | |
| 83 | a = copy() |
| 84 | a.ifc = b.ifc |
| 85 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"ifc": .*`)) |
| 86 | |
| 87 | a = copy() |
| 88 | a.rec = b.rec |
| 89 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"rec": .*`)) |
| 90 | |
| 91 | a = copy() |
| 92 | a.m = b.m |
| 93 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"m": .*`)) |
| 94 | |
| 95 | a = copy() |
| 96 | (*a.m[&key])++ |
| 97 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"m": .*`)) |
| 98 | |
| 99 | a = copy() |
| 100 | a.m[new(int)] = new(int) |
| 101 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"m": .*`)) |
| 102 | |
| 103 | a = copy() |
| 104 | delete(a.m, &key) |
| 105 | qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"m": .*`)) |
| 106 | } |