github.com/gogf/gf/v2@v2.7.4/frame/g/g_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package g_test
     8  
     9  import (
    10  	"context"
    11  	"os"
    12  	"sync"
    13  	"testing"
    14  
    15  	"github.com/gogf/gf/v2/container/garray"
    16  	"github.com/gogf/gf/v2/frame/g"
    17  	"github.com/gogf/gf/v2/test/gtest"
    18  	"github.com/gogf/gf/v2/util/gutil"
    19  )
    20  
    21  var (
    22  	ctx = context.TODO()
    23  )
    24  
    25  func Test_NewVar(t *testing.T) {
    26  	gtest.C(t, func(t *gtest.T) {
    27  		t.Assert(g.NewVar(1).Int(), 1)
    28  		t.Assert(g.NewVar(1, true).Int(), 1)
    29  	})
    30  }
    31  
    32  func Test_Dump(t *testing.T) {
    33  	gtest.C(t, func(t *gtest.T) {
    34  		g.Dump("GoFrame")
    35  	})
    36  }
    37  
    38  func Test_DumpTo(t *testing.T) {
    39  	gtest.C(t, func(t *gtest.T) {
    40  		g.DumpTo(os.Stdout, "GoFrame", gutil.DumpOption{})
    41  	})
    42  }
    43  
    44  func Test_DumpWithType(t *testing.T) {
    45  	gtest.C(t, func(t *gtest.T) {
    46  		g.DumpWithType("GoFrame", 123)
    47  	})
    48  }
    49  
    50  func Test_DumpWithOption(t *testing.T) {
    51  	gtest.C(t, func(t *gtest.T) {
    52  		g.DumpWithOption("GoFrame", gutil.DumpOption{})
    53  	})
    54  }
    55  
    56  func Test_Try(t *testing.T) {
    57  	gtest.C(t, func(t *gtest.T) {
    58  		g.Try(ctx, func(ctx context.Context) {
    59  			g.Dump("GoFrame")
    60  		})
    61  	})
    62  }
    63  
    64  func Test_TryCatch(t *testing.T) {
    65  	gtest.C(t, func(t *gtest.T) {
    66  		g.TryCatch(ctx, func(ctx context.Context) {
    67  			g.Dump("GoFrame")
    68  		}, func(ctx context.Context, exception error) {
    69  			g.Dump(exception)
    70  		})
    71  	})
    72  	gtest.C(t, func(t *gtest.T) {
    73  		g.TryCatch(ctx, func(ctx context.Context) {
    74  			g.Throw("GoFrame")
    75  		}, func(ctx context.Context, exception error) {
    76  			t.Assert(exception.Error(), "GoFrame")
    77  		})
    78  	})
    79  }
    80  
    81  func Test_IsNil(t *testing.T) {
    82  	gtest.C(t, func(t *gtest.T) {
    83  		t.Assert(g.IsNil(nil), true)
    84  		t.Assert(g.IsNil(0), false)
    85  		t.Assert(g.IsNil("GoFrame"), false)
    86  	})
    87  }
    88  
    89  func Test_IsEmpty(t *testing.T) {
    90  	gtest.C(t, func(t *gtest.T) {
    91  		t.Assert(g.IsEmpty(nil), true)
    92  		t.Assert(g.IsEmpty(0), true)
    93  		t.Assert(g.IsEmpty("GoFrame"), false)
    94  	})
    95  }
    96  
    97  func Test_SetDebug(t *testing.T) {
    98  	gtest.C(t, func(t *gtest.T) {
    99  		g.SetDebug(true)
   100  	})
   101  }
   102  
   103  func Test_Object(t *testing.T) {
   104  	gtest.C(t, func(t *gtest.T) {
   105  		t.AssertNE(g.Client(), nil)
   106  		t.AssertNE(g.Server(), nil)
   107  		t.AssertNE(g.TCPServer(), nil)
   108  		t.AssertNE(g.UDPServer(), nil)
   109  		t.AssertNE(g.View(), nil)
   110  		t.AssertNE(g.Config(), nil)
   111  		t.AssertNE(g.Cfg(), nil)
   112  		t.AssertNE(g.Resource(), nil)
   113  		t.AssertNE(g.I18n(), nil)
   114  		t.AssertNE(g.Res(), nil)
   115  		t.AssertNE(g.Log(), nil)
   116  		t.AssertNE(g.Validator(), nil)
   117  	})
   118  }
   119  
   120  func Test_Go(t *testing.T) {
   121  	gtest.C(t, func(t *gtest.T) {
   122  		var (
   123  			wg    = sync.WaitGroup{}
   124  			array = garray.NewArray(true)
   125  		)
   126  		wg.Add(1)
   127  		g.Go(context.Background(), func(ctx context.Context) {
   128  			defer wg.Done()
   129  			array.Append(1)
   130  		}, nil)
   131  		wg.Wait()
   132  		t.Assert(array.Len(), 1)
   133  	})
   134  }