github.com/gogf/gf@v1.16.9/frame/gins/gins_z_unit_basic_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 gins_test
     8  
     9  import (
    10  	"github.com/gogf/gf/frame/gins"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/test/gtest"
    14  )
    15  
    16  func Test_SetGet(t *testing.T) {
    17  	gtest.C(t, func(t *gtest.T) {
    18  		gins.Set("test-user", 1)
    19  		t.Assert(gins.Get("test-user"), 1)
    20  		t.Assert(gins.Get("none-exists"), nil)
    21  	})
    22  	gtest.C(t, func(t *gtest.T) {
    23  		t.Assert(gins.GetOrSet("test-1", 1), 1)
    24  		t.Assert(gins.Get("test-1"), 1)
    25  	})
    26  	gtest.C(t, func(t *gtest.T) {
    27  		t.Assert(gins.GetOrSetFunc("test-2", func() interface{} {
    28  			return 2
    29  		}), 2)
    30  		t.Assert(gins.Get("test-2"), 2)
    31  	})
    32  	gtest.C(t, func(t *gtest.T) {
    33  		t.Assert(gins.GetOrSetFuncLock("test-3", func() interface{} {
    34  			return 3
    35  		}), 3)
    36  		t.Assert(gins.Get("test-3"), 3)
    37  	})
    38  	gtest.C(t, func(t *gtest.T) {
    39  		t.Assert(gins.SetIfNotExist("test-4", 4), true)
    40  		t.Assert(gins.Get("test-4"), 4)
    41  		t.Assert(gins.SetIfNotExist("test-4", 5), false)
    42  		t.Assert(gins.Get("test-4"), 4)
    43  	})
    44  }