github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztype/type_test.go (about)

     1  package ztype_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/sohaha/zlsgo"
     8  	"github.com/sohaha/zlsgo/zjson"
     9  	"github.com/sohaha/zlsgo/ztime"
    10  	"github.com/sohaha/zlsgo/ztype"
    11  )
    12  
    13  func TestNew(t *testing.T) {
    14  	tt := zlsgo.NewTest(t)
    15  
    16  	v := ztype.New(`{"age": "100"}`)
    17  	v2 := ztype.New(v)
    18  	tt.Equal(v.Get("age").Int(), v2.Get("age").Int())
    19  	tt.Equal(v.Get("age2").Int(12), ztype.New(nil).Int(12))
    20  
    21  	t.Run("Map", func(t *testing.T) {
    22  		t.Log(ztype.New("123").Map())
    23  		t.Log(ztype.New(`{"name": "test"}`).Map())
    24  		t.Log(ztype.New([]string{"1", "2"}).Map())
    25  		t.Log(ztype.New(map[string]interface{}{"abc": 123}).Map())
    26  	})
    27  
    28  	t.Run("Slice", func(t *testing.T) {
    29  		t.Log(ztype.New("123").SliceValue())
    30  		t.Log(ztype.New(`{"name": "test"}`).Maps())
    31  		t.Log(ztype.New([]string{"1", "2"}).SliceInt())
    32  		t.Log(ztype.New(map[string]interface{}{"abc": 123}).Slice())
    33  	})
    34  
    35  	t.Run("Time", func(t *testing.T) {
    36  		t.Log(ztype.New("2022-07-17 17:23:58").Time())
    37  		t.Log(ztype.New(time.Now()).Time())
    38  		t.Log(ztype.New(ztime.Now()).Time())
    39  	})
    40  }
    41  
    42  func TestNewMap(t *testing.T) {
    43  	m := map[string]interface{}{"a": 1, "b": 2.01, "c": []string{"d", "e", "f", "g", "h"}, "r": map[string]int{"G1": 1, "G2": 2}}
    44  	mt := ztype.Map(m)
    45  
    46  	for _, v := range []string{"a", "b", "c", "d", "r", "_"} {
    47  		typ := mt.Get(v)
    48  		d := map[string]interface{}{
    49  			"value":   typ.Value(),
    50  			"bytes":   typ.Bytes([]byte("_")),
    51  			"string":  typ.String("_"),
    52  			"bool":    typ.Bool(false),
    53  			"int":     typ.Int(1),
    54  			"int8":    typ.Int8(1),
    55  			"int16":   typ.Int16(1),
    56  			"int32":   typ.Int32(1),
    57  			"int64":   typ.Int64(1),
    58  			"uint":    typ.Uint(1),
    59  			"uint8":   typ.Uint8(1),
    60  			"uint16":  typ.Uint16(1),
    61  			"uint32":  typ.Uint32(1),
    62  			"uint64":  typ.Uint64(1),
    63  			"float32": typ.Float32(1),
    64  			"float64": typ.Float64(1),
    65  			"map":     typ.Map(),
    66  			"slice_0": typ.Slice().Index(0).String("_s_"),
    67  		}
    68  		t.Logf("%s %+v", v, d)
    69  	}
    70  
    71  }
    72  
    73  func TestNewMapKeys(t *testing.T) {
    74  	tt := zlsgo.NewTest(t)
    75  
    76  	json := `{"a":1,"b.c":2,"d":{"e":3,"f":4},"g":[5,6],"h":{"i":{"j":"100","k":"101"},"o":["p","q",1,16.8]},"0":"00001"}`
    77  	m := zjson.Parse(json).Map()
    78  
    79  	var arr ztype.Maps
    80  	_ = zjson.Unmarshal(`[`+json+`]`, &arr)
    81  
    82  	tt.EqualTrue(!arr.IsEmpty())
    83  	tt.Equal(1, arr.Len())
    84  	t.Log(arr.Index(0).Get("no").Exists())
    85  
    86  	maps := []ztype.Map{ztype.Map(m), arr.Index(0), map[string]interface{}{"a": 1, "b.c": 2, "d": map[string]interface{}{"e": 3, "f": 4}, "g": []interface{}{5, 6}, "h": map[string]interface{}{"i": map[string]interface{}{"j": "100", "k": "101"}, "o": []interface{}{"p", "q", 1, 16.8}}, "0": "00001"}}
    87  	for _, mt := range maps {
    88  		t.Log(mt.Get("0").Value())
    89  		tt.Equal("00001", mt.Get("0").String())
    90  
    91  		t.Log(mt.Get("a").Value())
    92  		tt.Equal(1, mt.Get("a").Int())
    93  
    94  		t.Log(mt.Get("b.c").Value())
    95  		tt.EqualTrue(!mt.Get("b.c").Exists())
    96  		tt.Equal(0, mt.Get("b.c").Int())
    97  
    98  		t.Log(mt.Get("b\\.c").Value())
    99  		tt.EqualTrue(mt.Get("b\\.c").Exists())
   100  		tt.Equal(2, mt.Get("b\\.c").Int())
   101  
   102  		d := mt.Get("d")
   103  		t.Log(d.Value())
   104  		tt.EqualTrue(d.Exists())
   105  
   106  		t.Log(d.Get("e").Value())
   107  		tt.Equal(3, d.Get("e").Int())
   108  
   109  		t.Log(mt.Get("g").Value())
   110  		tt.Equal("6", mt.Get("g.1").String())
   111  
   112  		t.Log(mt.Get("h.i.k").Value())
   113  		tt.Equal("101", mt.Get("h.i.k").String())
   114  
   115  		t.Log(mt.Get("h.o.3").Value())
   116  		tt.Equal(16.8, mt.Get("h.o.3").Float64())
   117  	}
   118  }
   119  
   120  func TestMapSet(t *testing.T) {
   121  	tt := zlsgo.NewTest(t)
   122  
   123  	m := ztype.Map{}
   124  
   125  	tt.EqualTrue(m.IsEmpty())
   126  	tt.EqualTrue(!m.Get("a").Exists())
   127  	_ = m.Set("a", 1)
   128  	tt.EqualTrue(m.Get("a").Exists())
   129  	tt.Equal(1, m.Get("a").Int())
   130  	tt.EqualTrue(!m.IsEmpty())
   131  
   132  	var m2 = ztype.Map{}
   133  
   134  	tt.EqualTrue(m2.IsEmpty())
   135  	tt.EqualTrue(!m2.Get("a").Exists())
   136  	_ = m2.Set("a", 1)
   137  	tt.EqualTrue(m2.Get("a").Exists())
   138  	tt.Equal(1, m2.Get("a").Int())
   139  	tt.EqualTrue(!m2.IsEmpty())
   140  }