mirror of
https://github.com/gin-gonic/gin.git
synced 2025-05-22 20:49:23 +08:00
test(context): improve context.Set and context.Get tests
- Split existing test into separate functions for different scenarios - Add test for setting and getting values with any key type - Add test for handling non-comparable keys - Improve assertions to check for key existence and value correctness Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
This commit is contained in:
parent
f1bcc093b8
commit
ac59ed24d3
@ -255,8 +255,13 @@ func TestContextSetGet(t *testing.T) {
|
||||
assert.Equal(t, "bar", c.MustGet("foo"))
|
||||
assert.Panics(t, func() { c.MustGet("no_exist") })
|
||||
|
||||
// other types
|
||||
}
|
||||
|
||||
func TestContextSetGetAnyKey(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
|
||||
type key struct{}
|
||||
|
||||
tests := []struct {
|
||||
key any
|
||||
}{
|
||||
@ -272,11 +277,16 @@ func TestContextSetGet(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(reflect.TypeOf(tt.key).String(), func(t *testing.T) {
|
||||
c.Set(tt.key, 1)
|
||||
assert.Equal(t, 1, c.MustGet(tt.key))
|
||||
value, ok := c.Get(tt.key)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 1, value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextSetGetPanicsWhenKeyNotComparable(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
|
||||
// no comparable
|
||||
assert.Panics(t, func() {
|
||||
c.Set([]int{1}, 1)
|
||||
c.Set(func() {}, 1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user