github.com/goki/ki@v1.1.17/kit/convert_test.go (about) 1 // Copyright (c) 2018, The GoKi Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package kit 6 7 import ( 8 "fmt" 9 "testing" 10 ) 11 12 func AFun(aa any) bool { 13 return IfaceIsNil(aa) 14 } 15 16 func TestIfaceIsNil(t *testing.T) { 17 ai := any(a) 18 19 if IfaceIsNil(ai) != false { 20 t.Errorf("should be non-nil: %v\n", ai) 21 } 22 23 var ap *A 24 api := any(ap) 25 26 if IfaceIsNil(api) != true { 27 t.Errorf("should be nil: %v\n", api) 28 } 29 30 if AFun(ap) != true { 31 t.Errorf("should be nil: %v\n", ap) 32 } 33 34 if AFun(&a) != false { 35 t.Errorf("should be non-nil: %v\n", &a) 36 } 37 38 } 39 40 func TestConverts(t *testing.T) { 41 fv := 3.14 42 iv := 10 43 sv := "25" 44 // bv := true 45 46 // note: this does not work 47 // reflect.ValueOf(&fv).Elem().Set(reflect.ValueOf("1.58").Convert(reflect.TypeOf(fv))) 48 ok := false 49 50 ft := "1.58" 51 ok = SetRobust(&fv, ft) 52 fs := fmt.Sprintf("%v", fv) 53 if !ok || fs != ft { 54 t.Errorf("Float convert error: %v != %v, ok: %v\n", fs, ft, ok) 55 } 56 57 it := "1" 58 ok = SetRobust(&iv, true) 59 is := fmt.Sprintf("%v", iv) 60 if !ok || is != it { 61 t.Errorf("Int convert error: %v != %v, ok: %v\n", is, it, ok) 62 } 63 64 st := "22" 65 ok = SetRobust(&sv, 22) 66 ss := fmt.Sprintf("%v", sv) 67 if !ok || ss != st { 68 t.Errorf("String convert error: %v != %v, ok: %v\n", ss, st, ok) 69 } 70 tc := C{} 71 InitC() 72 ok = SetRobust(&tc, c) 73 // fmt.Printf("tc %+v\n", tc) 74 if !ok || tc != c { 75 t.Errorf("Struct convert error: %+v != %+v, ok: %v\n", c, tc, ok) 76 } 77 } 78 79 func TestCopySlice(t *testing.T) { 80 var tof []float32 81 var tos []string 82 fmf := []float32{1, 2, 3} 83 fms := []string{"3", "4", "5"} 84 err := CopySliceRobust(&tof, fmf) 85 if err != nil { 86 t.Errorf("copy float: %s\n", err.Error()) 87 } 88 for i := range fmf { 89 if tof[i] != fmf[i] { 90 t.Errorf("tof %d: %g != %g\n", i, tof[i], fmf[i]) 91 } 92 } 93 err = CopySliceRobust(&tos, fms) 94 if err != nil { 95 t.Errorf("copy string: %s\n", err.Error()) 96 } 97 for i := range fms { 98 if tos[i] != fms[i] { 99 t.Errorf("tos %d: %s != %s\n", i, tos[i], fms[i]) 100 } 101 } 102 err = CopySliceRobust(&tof, fms) 103 if err != nil { 104 t.Errorf("copy float = string: %s\n", err.Error()) 105 } 106 for i := range fms { 107 if ToString(tof[i]) != fms[i] { 108 t.Errorf("tof %d: %g != %s\n", i, tof[i], fms[i]) 109 } 110 } 111 fms = fms[:2] 112 err = CopySliceRobust(&tof, fms) 113 if err != nil { 114 t.Errorf("copy float = string: %s\n", err.Error()) 115 } 116 if len(tof) != len(fms) { 117 t.Errorf("copy float = string: size not 2: %d\n", len(tof)) 118 } 119 for i := range fms { 120 if ToString(tof[i]) != fms[i] { 121 t.Errorf("tof %d: %g != %s\n", i, tof[i], fms[i]) 122 } 123 } 124 fms = append(fms, "7") 125 err = CopySliceRobust(&tof, fms) 126 if err != nil { 127 t.Errorf("copy float = string: %s\n", err.Error()) 128 } 129 if len(tof) != len(fms) { 130 t.Errorf("copy float = string: size not 3: %d\n", len(tof)) 131 } 132 for i := range fms { 133 if ToString(tof[i]) != fms[i] { 134 t.Errorf("tof %d: %g != %s\n", i, tof[i], fms[i]) 135 } 136 } 137 138 var toc [][]float32 139 fmc := [][]string{[]string{"1", "2"}, []string{"3", "4"}} 140 err = CopySliceRobust(&toc, fmc) 141 if err != nil { 142 t.Errorf("copy [][]float = [][]string: %s\n", err.Error()) 143 } 144 for i := range fmc { 145 fmci := fmc[i] 146 toci := toc[i] 147 for j := range fmci { 148 if ToString(toci[j]) != fmci[j] { 149 t.Errorf("toci,j %d,%d: %g != %s\n", i, j, toci[j], fmci[j]) 150 } 151 } 152 } 153 } 154 155 func TestCopyMap(t *testing.T) { 156 var tof map[string]float32 157 var tos map[string]string 158 fmf := map[string]float32{"a": 1, "b": 2, "c": 3} 159 fms := map[string]string{"a": "3", "b": "4", "c": "5"} 160 err := CopyMapRobust(&tof, fmf) 161 if err != nil { 162 t.Errorf("copy float: %s\n", err.Error()) 163 } 164 for i := range fmf { 165 if tof[i] != fmf[i] { 166 t.Errorf("tof %s: %g != %g\n", i, tof[i], fmf[i]) 167 } 168 } 169 err = CopyMapRobust(&tos, fms) 170 if err != nil { 171 t.Errorf("copy string: %s\n", err.Error()) 172 } 173 for i := range fms { 174 if tos[i] != fms[i] { 175 t.Errorf("tos %s: %s != %s\n", i, tos[i], fms[i]) 176 } 177 } 178 err = CopyMapRobust(&tof, fms) 179 if err != nil { 180 t.Errorf("copy float = string: %s\n", err.Error()) 181 } 182 for i := range fms { 183 if ToString(tof[i]) != fms[i] { 184 t.Errorf("tof %s: %g != %s\n", i, tof[i], fms[i]) 185 } 186 } 187 delete(fms, "b") 188 err = CopyMapRobust(&tof, fms) 189 if err != nil { 190 t.Errorf("copy float = string: %s\n", err.Error()) 191 } 192 if len(tof) != len(fms) { 193 t.Errorf("copy float = string: size not 2: %d\n", len(tof)) 194 } 195 for i := range fms { 196 if ToString(tof[i]) != fms[i] { 197 t.Errorf("tof %s: %g != %s\n", i, tof[i], fms[i]) 198 } 199 } 200 fms["e"] = "7" 201 err = CopyMapRobust(&tof, fms) 202 if err != nil { 203 t.Errorf("copy float = string: %s\n", err.Error()) 204 } 205 if len(tof) != len(fms) { 206 t.Errorf("copy float = string: size not 3: %d\n", len(tof)) 207 } 208 for i := range fms { 209 if ToString(tof[i]) != fms[i] { 210 t.Errorf("tof %s: %g != %s\n", i, tof[i], fms[i]) 211 } 212 } 213 214 var toc map[string]map[string]float32 215 fmc := map[string]map[string]string{"q": {"a": "1", "b": "2"}, "z": {"c": "3", "d": "4"}} 216 err = CopyMapRobust(&toc, fmc) 217 if err != nil { 218 t.Errorf("copy map[string]map[string]float = map[string]map[string]string: %s\n", err.Error()) 219 } 220 for i := range fmc { 221 fmci := fmc[i] 222 toci := toc[i] 223 for j := range fmci { 224 if ToString(toci[j]) != fmci[j] { 225 t.Errorf("toci,j %s,%s: %g != %s\n", i, j, toci[j], fmci[j]) 226 } 227 } 228 } 229 } 230 231 func TestSetRobustFomString(t *testing.T) { 232 ta := A{} 233 ta.Mbr1 = "av" 234 ta.Mbr2 = 22 235 SetRobust(&ta, `{"Mbr1":"aa", "Mbr2":14}`) // note: only uses " 236 237 flts := []float32{1, 2, 3} 238 SetRobust(&flts, `[3, 4, 5]`) 239 240 mp := map[string]float32{"a": 1, "b": 2, "c": 3} 241 SetRobust(&mp, `{"d":3,"e":4,"f":5}`) 242 }