refactor(context): refactor Keys type to map[any]any (#3963)
* refactor(context): refactor keys to `map[any]any` Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com> * refactor(context): refactor keys to `map[any]any` Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com> * style(context): remove empty lines before GetInt16, GetIntSlice, and GetStringMapString methods - Remove unnecessary empty lines in the context.go file - Improve code readability and consistency Signed-off-by: flc1125 <four_leaf_clover@foxmail.com> * refactor(context): simplify GetStringSlice function - Replace manual type assertion with generic getTyped function - Reduce code duplication and improve type safety Signed-off-by: flc1125 <four_leaf_clover@foxmail.com> * 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> * refactor(context): replace fmt.Errorf with fmt.Sprintf in panic message * test(context): remove trailing hyphen from context_test.go * refactor(context): improve error message for missing key in context - Remove unnecessary quotes around the key in the error message - Simplify the error message format for better readability * test(context): improve panic test message for non-existent key --------- Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com> Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
This commit is contained in:
+40
-1
@@ -257,7 +257,46 @@ func TestContextSetGet(t *testing.T) {
|
||||
assert.False(t, err)
|
||||
|
||||
assert.Equal(t, "bar", c.MustGet("foo"))
|
||||
assert.Panics(t, func() { c.MustGet("no_exist") })
|
||||
assert.Panicsf(t, func() {
|
||||
c.MustGet("no_exist")
|
||||
}, "key no_exist does not exist")
|
||||
}
|
||||
|
||||
func TestContextSetGetAnyKey(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
|
||||
type key struct{}
|
||||
|
||||
tests := []struct {
|
||||
key any
|
||||
}{
|
||||
{1},
|
||||
{int32(1)},
|
||||
{int64(1)},
|
||||
{uint(1)},
|
||||
{float32(1)},
|
||||
{key{}},
|
||||
{&key{}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(reflect.TypeOf(tt.key).String(), func(t *testing.T) {
|
||||
c.Set(tt.key, 1)
|
||||
value, ok := c.Get(tt.key)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 1, value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextSetGetPanicsWhenKeyNotComparable(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
|
||||
assert.Panics(t, func() {
|
||||
c.Set([]int{1}, 1)
|
||||
c.Set(func() {}, 1)
|
||||
c.Set(make(chan int), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestContextSetGetValues(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user