2019-03-12 23:26:10 +08:00
|
|
|
// 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.
|
|
|
|
|
|
2020-03-16 22:47:39 +08:00
|
|
|
package gins_test
|
2019-03-12 23:26:10 +08:00
|
|
|
|
|
|
|
|
import (
|
2020-03-16 22:47:39 +08:00
|
|
|
"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"
|
2019-03-12 23:26:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Test_SetGet(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-16 22:47:39 +08:00
|
|
|
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
|
|
|
})
|
2019-03-12 23:26:10 +08:00
|
|
|
}
|