github.com/alexflint/go-memdump@v1.1.0/descriptor_test.go (about) 1 package memdump 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func assertCompareDescriptors(t *testing.T, a interface{}, b interface{}, expected bool) { 11 da := describe(reflect.TypeOf(a)) 12 db := describe(reflect.TypeOf(b)) 13 assert.Equal(t, expected, descriptorsEqual(da, db)) 14 } 15 16 func TestDescribeScalar(t *testing.T) { 17 assertCompareDescriptors(t, "abc", "d", true) 18 assertCompareDescriptors(t, 123, 456, true) 19 assertCompareDescriptors(t, 1.2, 3.4, true) 20 assertCompareDescriptors(t, uint64(12), uint64(34), true) 21 22 assertCompareDescriptors(t, int16(12), int64(34), false) 23 assertCompareDescriptors(t, uint64(12), int64(34), false) 24 assertCompareDescriptors(t, uint64(12), "34", false) 25 assertCompareDescriptors(t, uint64(12), 4.5, false) 26 } 27 28 func TestDescribePointer(t *testing.T) { 29 var x int 30 var y uint64 31 var z string 32 33 assertCompareDescriptors(t, &x, &x, true) 34 assertCompareDescriptors(t, &y, &y, true) 35 assertCompareDescriptors(t, &z, &z, true) 36 37 assertCompareDescriptors(t, &x, &y, false) 38 assertCompareDescriptors(t, &x, &z, false) 39 assertCompareDescriptors(t, &x, x, false) 40 41 xptr := &x 42 xptrptr := &xptr 43 44 assertCompareDescriptors(t, xptrptr, xptrptr, true) 45 assertCompareDescriptors(t, xptr, xptrptr, false) 46 assertCompareDescriptors(t, x, xptrptr, false) 47 } 48 49 func TestDescribeSlice(t *testing.T) { 50 var x []int 51 var y []uint64 52 var z [][]int16 53 54 assertCompareDescriptors(t, x, x, true) 55 assertCompareDescriptors(t, y, y, true) 56 assertCompareDescriptors(t, z, z, true) 57 assertCompareDescriptors(t, &x, &x, true) 58 assertCompareDescriptors(t, &y, &y, true) 59 assertCompareDescriptors(t, &z, &z, true) 60 61 assertCompareDescriptors(t, x, y, false) 62 assertCompareDescriptors(t, &x, &y, false) 63 assertCompareDescriptors(t, x, z, false) 64 assertCompareDescriptors(t, &x, x, false) 65 } 66 67 func TestDescribeArray(t *testing.T) { 68 var x [4]int 69 var y [10]int 70 var z [2][3]int16 71 72 assertCompareDescriptors(t, x, x, true) 73 assertCompareDescriptors(t, y, y, true) 74 assertCompareDescriptors(t, z, z, true) 75 assertCompareDescriptors(t, &x, &x, true) 76 assertCompareDescriptors(t, &y, &y, true) 77 assertCompareDescriptors(t, &z, &z, true) 78 79 assertCompareDescriptors(t, x, y, false) 80 assertCompareDescriptors(t, &x, &y, false) 81 assertCompareDescriptors(t, x, z, false) 82 assertCompareDescriptors(t, &x, x, false) 83 } 84 85 func TestDescribeStruct(t *testing.T) { 86 type u struct { 87 A string 88 B int 89 } 90 type v struct { 91 A string 92 B int32 93 } 94 type w struct { 95 u 96 A string 97 B int 98 c v 99 } 100 101 assertCompareDescriptors(t, u{}, u{}, true) 102 assertCompareDescriptors(t, v{}, v{}, true) 103 assertCompareDescriptors(t, w{}, w{}, true) 104 assertCompareDescriptors(t, &u{}, &u{}, true) 105 assertCompareDescriptors(t, &v{}, &v{}, true) 106 assertCompareDescriptors(t, &w{}, &w{}, true) 107 108 assertCompareDescriptors(t, u{}, v{}, false) 109 assertCompareDescriptors(t, u{}, w{}, false) 110 assertCompareDescriptors(t, v{}, w{}, false) 111 assertCompareDescriptors(t, u{}, &u{}, false) 112 } 113 114 func TestDescribeStructWithTags(t *testing.T) { 115 type u struct { 116 A string 117 B int 118 } 119 type v struct { 120 A string 121 xyz int 122 } 123 type w struct { 124 A string 125 B int `memdump:"xyz"` 126 } 127 128 assertCompareDescriptors(t, u{}, v{}, false) 129 assertCompareDescriptors(t, u{}, w{}, false) 130 assertCompareDescriptors(t, v{}, w{}, true) 131 } 132 133 func TestDescriptorsEqual_DifferentElems(t *testing.T) { 134 type u struct { 135 A []string 136 } 137 type v struct { 138 A []int 139 } 140 type w struct { 141 A []int 142 } 143 144 assertCompareDescriptors(t, u{}, v{}, false) 145 assertCompareDescriptors(t, u{}, w{}, false) 146 assertCompareDescriptors(t, v{}, w{}, true) 147 } 148 149 func TestDescriptorsEqual_DifferentNumFields(t *testing.T) { 150 type u struct { 151 A string 152 B string 153 } 154 type v struct { 155 A string 156 } 157 type w struct { 158 A string 159 } 160 161 assertCompareDescriptors(t, u{}, v{}, false) 162 assertCompareDescriptors(t, u{}, w{}, false) 163 assertCompareDescriptors(t, v{}, w{}, true) 164 } 165 166 func TestDescribe_PanicsOnMap(t *testing.T) { 167 type T struct { 168 A map[string]int 169 } 170 assert.Panics(t, func() { 171 describe(reflect.TypeOf(T{})) 172 }) 173 }