github.com/hedzr/evendeep@v0.4.8/01_test.go (about)

     1  package evendeep
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"reflect"
     7  	"runtime"
     8  	"strconv"
     9  	"testing"
    10  	"time"
    11  	"unsafe"
    12  
    13  	"github.com/hedzr/log"
    14  	"gopkg.in/hedzr/errors.v3"
    15  
    16  	"github.com/hedzr/evendeep/dbglog"
    17  	"github.com/hedzr/evendeep/internal/cl"
    18  	"github.com/hedzr/evendeep/internal/tool"
    19  )
    20  
    21  // TestLogNormal _
    22  func TestLogNormal(t *testing.T) {
    23  	// config := log.NewLoggerConfigWith(true, "logrus", "trace")
    24  	// logger := logrus.NewWithConfig(config)
    25  	log.Printf("hello")
    26  	log.Infof("hello info")
    27  	log.Warnf("hello warn")
    28  	log.Errorf("hello error")
    29  	log.Debugf("hello debug")
    30  	log.Tracef("hello trace")
    31  
    32  	dbglog.Log("but again")
    33  }
    34  
    35  // TestErrorsTmpl _
    36  func TestErrorsTmpl(t *testing.T) {
    37  	var errTmpl = errors.New("expecting %v but got %v")
    38  
    39  	var err error
    40  	err = errTmpl.FormatWith("789", "123")
    41  	t.Logf("The error is expected: %v", err)
    42  	err = errTmpl.FormatWith(true, false)
    43  	t.Logf("The error is expected: %v", err)
    44  }
    45  
    46  // TestErrorsIs _
    47  func TestErrorsIs(t *testing.T) {
    48  	_, err := strconv.ParseFloat("hello", 64)
    49  	t.Logf("err = %+v", err)
    50  
    51  	// e1:=errors2.Unwrap(err)
    52  	// t.Logf("e1 = %+v", e1)
    53  
    54  	t.Logf("errors.Is(err, strconv.ErrSyntax): %v", errors.Is(err, strconv.ErrSyntax))
    55  	t.Logf("errors.Is(err, &strconv.NumError{}): %v", errors.Is(err, &strconv.NumError{}))
    56  
    57  	var e2 *strconv.NumError
    58  	if errors.As(err, &e2) {
    59  		t.Logf("As() ok, e2 = %v", e2)
    60  	} else {
    61  		t.Logf("As() not ok")
    62  	}
    63  }
    64  
    65  func TestSliceLen(t *testing.T) {
    66  	var str []string
    67  	var v = reflect.ValueOf(&str)
    68  
    69  	// make value to adopt element's type - in this case string type
    70  
    71  	v = v.Elem()
    72  
    73  	v = reflect.Append(v, reflect.ValueOf("abc"))
    74  	v = reflect.Append(v, reflect.ValueOf("def"))
    75  	v = reflect.Append(v, reflect.ValueOf("ghi"), reflect.ValueOf("jkl"))
    76  
    77  	fmt.Println("Our value is a type of :", v.Kind())
    78  	fmt.Printf("len : %v, %v\n", v.Len(), tool.Typfmtv(&v))
    79  
    80  	vSlice := v.Slice(0, v.Len())
    81  	vSliceElems := vSlice.Interface()
    82  
    83  	fmt.Println("With the elements of : ", vSliceElems)
    84  
    85  	v = reflect.AppendSlice(v, reflect.ValueOf([]string{"mno", "pqr", "stu"}))
    86  
    87  	vSlice = v.Slice(0, v.Len())
    88  	vSliceElems = vSlice.Interface()
    89  
    90  	fmt.Println("After AppendSlice : ", vSliceElems)
    91  }
    92  
    93  func TestUnexported(t *testing.T) {
    94  	var s = struct{ foo int }{42}
    95  	var i int
    96  
    97  	rs := reflect.ValueOf(&s).Elem() // s, but writable
    98  	rf := rs.Field(0)                // s.foo
    99  	ri := reflect.ValueOf(&i).Elem() // i, but writeable
   100  
   101  	// These both fail with "reflect.Value.Set using value obtained using unexported field":
   102  	// ri.Set(rf)
   103  	// rf.Set(ri)
   104  
   105  	// Cheat:
   106  	rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
   107  
   108  	// Now these both work:
   109  	ri.Set(rf)
   110  	i = 9
   111  	rf.Set(ri)
   112  
   113  	fmt.Println(s, i, runtime.Version())
   114  
   115  	rf = rs.Field(0)
   116  	cl.SetUnexportedField(rf, reflect.ValueOf(123))
   117  	fmt.Println(s, i, runtime.Version())
   118  }
   119  
   120  func TestTm00(t *testing.T) {
   121  
   122  	timeFloat := 13572223.479231686
   123  	sec, dec := math.Modf(timeFloat)
   124  	tm := time.Unix(int64(sec), int64(dec*(1e9)))
   125  	t.Logf("tm: %v", tm)
   126  
   127  	t.Logf("sec, %v, nano, %v", tm.Unix(), tm.UnixNano())
   128  	t.Logf("f: %v", float64(tm.UnixNano())/1e9)
   129  }
   130  
   131  func TestValueValid(t *testing.T) {
   132  
   133  	var ival int
   134  	var pival *int
   135  	type A struct {
   136  		ival int //nolint:unused,structcheck //test
   137  	}
   138  	var aval A
   139  	var paval *A
   140  
   141  	var v reflect.Value
   142  
   143  	t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
   144  
   145  	v = reflect.ValueOf(ival)
   146  	t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
   147  
   148  	v = reflect.ValueOf(pival)
   149  	t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
   150  
   151  	v = reflect.ValueOf(aval)
   152  	t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
   153  
   154  	v = reflect.ValueOf(paval)
   155  	t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
   156  
   157  	var b bool
   158  	v = reflect.ValueOf(b)
   159  	t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
   160  
   161  	b = true
   162  	v = reflect.ValueOf(b)
   163  	t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
   164  }