github.com/zhongdalu/gf@v1.0.0/g/frame/gins/gins_basic_test.go (about)

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