github.com/goki/ki@v1.1.11/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 interface{}) bool { 13 return IfaceIsNil(aa) 14 } 15 16 func TestIfaceIsNil(t *testing.T) { 17 ai := interface{}(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 := interface{}(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 }