Files
gf/frame/gins/gins_z_unit_basic_test.go

45 lines
1.1 KiB
Go
Raw Normal View History

// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gins_test
import (
"github.com/gogf/gf/frame/gins"
2019-06-19 09:06:52 +08:00
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/test/gtest"
)
func Test_SetGet(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
gins.Set("test-user", 1)
2020-03-19 22:56:12 +08:00
t.Assert(gins.Get("test-user"), 1)
t.Assert(gins.Get("none-exists"), nil)
2019-06-19 09:06:52 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gins.GetOrSet("test-1", 1), 1)
t.Assert(gins.Get("test-1"), 1)
2019-06-19 09:06:52 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gins.GetOrSetFunc("test-2", func() interface{} {
2019-06-19 09:06:52 +08:00
return 2
}), 2)
2020-03-19 22:56:12 +08:00
t.Assert(gins.Get("test-2"), 2)
2019-06-19 09:06:52 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gins.GetOrSetFuncLock("test-3", func() interface{} {
2019-06-19 09:06:52 +08:00
return 3
}), 3)
2020-03-19 22:56:12 +08:00
t.Assert(gins.Get("test-3"), 3)
2019-06-19 09:06:52 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gins.SetIfNotExist("test-4", 4), true)
t.Assert(gins.Get("test-4"), 4)
t.Assert(gins.SetIfNotExist("test-4", 5), false)
t.Assert(gins.Get("test-4"), 4)
2019-06-19 09:06:52 +08:00
})
}