goki.dev/laser@v0.1.34/basic_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 laser
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"goki.dev/laser/testdata"
    12  )
    13  
    14  func AFun(aa any) bool {
    15  	return AnyIsNil(aa)
    16  }
    17  
    18  func TestAnyIsNil(t *testing.T) {
    19  	ai := any(a)
    20  
    21  	if AnyIsNil(ai) != false {
    22  		t.Errorf("should be non-nil: %v\n", ai)
    23  	}
    24  
    25  	var ap *A
    26  	api := any(ap)
    27  
    28  	if AnyIsNil(api) != true {
    29  		t.Errorf("should be nil: %v\n", api)
    30  	}
    31  
    32  	if AFun(ap) != true {
    33  		t.Errorf("should be nil: %v\n", ap)
    34  	}
    35  
    36  	if AFun(&a) != false {
    37  		t.Errorf("should be non-nil: %v\n", &a)
    38  	}
    39  
    40  }
    41  
    42  func TestConverts(t *testing.T) {
    43  	fv := 3.14
    44  	iv := 10
    45  	sv := "25"
    46  	// bv := true
    47  
    48  	// note: this does not work
    49  	// reflect.ValueOf(&fv).Elem().Set(reflect.ValueOf("1.58").Convert(reflect.TypeOf(fv)))
    50  
    51  	ft := "1.58"
    52  	err := SetRobust(&fv, ft)
    53  	fs := fmt.Sprintf("%v", fv)
    54  	if err != nil || fs != ft {
    55  		t.Errorf("Float convert error: %v != %v, err: %v", fs, ft, err)
    56  	}
    57  
    58  	it := "1"
    59  	err = SetRobust(&iv, true)
    60  	is := fmt.Sprintf("%v", iv)
    61  	if err != nil || is != it {
    62  		t.Errorf("Int convert error: %v != %v, err: %v", is, it, err)
    63  	}
    64  
    65  	st := "22"
    66  	err = SetRobust(&sv, 22)
    67  	ss := fmt.Sprintf("%v", sv)
    68  	if err != nil || ss != st {
    69  		t.Errorf("String convert error: %v != %v, err: %v\n", ss, st, err)
    70  	}
    71  	tc := C{}
    72  	InitC()
    73  	err = SetRobust(&tc, c)
    74  	// fmt.Printf("tc %+v\n", tc)
    75  	if err != nil || tc != c {
    76  		t.Errorf("Struct convert error: %+v != %+v, err: %v\n", c, tc, err)
    77  	}
    78  }
    79  
    80  func TestSetRobustFomString(t *testing.T) {
    81  	ta := A{}
    82  	ta.Mbr1 = "av"
    83  	ta.Mbr2 = 22
    84  	SetRobust(&ta, `{"Mbr1":"aa", "Mbr2":14}`) // note: only uses "
    85  	if ta.Mbr1 != "aa" || ta.Mbr2 != 14 {
    86  		t.Errorf("SetRobust: fields from struct string failed")
    87  	}
    88  
    89  	flts := []float32{1, 2, 3}
    90  	SetRobust(&flts, `[3, 4, 5]`)
    91  	if flts[1] != 4 {
    92  		t.Errorf("SetRobust: slice from string failed")
    93  	}
    94  
    95  	mp := map[string]float32{"a": 1, "b": 2, "c": 3}
    96  	SetRobust(&mp, `{"d":3,"e":4,"f":5}`)
    97  	if mp["e"] != 4 {
    98  		t.Errorf("SetRobust: map from string failed")
    99  	}
   100  	if mp["a"] != 1 {
   101  		t.Errorf("SetRobust: map from string reset prior values")
   102  	}
   103  
   104  	fruit := testdata.Peach
   105  	SetRobust(&fruit, "Strawberry")
   106  	if fruit != testdata.Strawberry {
   107  		t.Errorf("SetRobust: enum from string failed")
   108  	}
   109  
   110  	if ToString(fruit) != "Strawberry" {
   111  		t.Errorf("ToString: failed to use Stringer on enum")
   112  	}
   113  }