From c3cff03b2bf13b5d88048a60097ebe51cc1d6eae Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Fri, 5 Nov 2021 16:36:18 +0800 Subject: [PATCH] test(structure-hashx): add hashx test --- pkg/structure/hashx/hashx_test.go | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pkg/structure/hashx/hashx_test.go diff --git a/pkg/structure/hashx/hashx_test.go b/pkg/structure/hashx/hashx_test.go new file mode 100644 index 0000000..78c9241 --- /dev/null +++ b/pkg/structure/hashx/hashx_test.go @@ -0,0 +1,62 @@ +package hashx + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestHashX_Set_SetX(t *testing.T) { + h := NewHashXSingle() + h.Set("key", "opq") + res, err := h.Get("key") + require.NoError(t, err) + require.Equal(t, res, "opq") + b, _ := h.SetX("key", "opq") + require.Equal(t, b, false) + + b, _ = h.SetX("key1", "opq") + require.Equal(t, b, true) + res, err = h.Get("key1") + require.NoError(t, err) + require.Equal(t, res, "opq") +} + +func TestHashX_Del(t *testing.T) { + h := NewHashXSingle() + up := h.Set("key", "opq") + upu, err := h.Del("key") + require.NoError(t, err) + require.Equal(t, up, upu) +} + +func TestHashX_Key(t *testing.T) { + h := NewHashXSingle() + h.Set("key", "opq") + h.Set("key1", "opq") + h.Set("key2", "opq") + + require.Equal(t, h.Key(), []string{"key", "key1", "key2"}) +} + +func TestHashX_Value(t *testing.T) { + h := NewHashXSingle() + h.Set("key", "opq") + h.Set("key1", "opq") + h.Set("key2", "opq") + + require.Equal(t, h.Value(), []string{"opq", "opq", "opq"}) +} + +func TestHashX_Add(t *testing.T) { + h := NewHashXSingle() + h.Set("1", "1") + c, res, err := h.Add(1, "1") + require.NoError(t, err) + require.Equal(t, c, 1) + require.Equal(t, res, []string{"2"}) + + s, err := h.Get("1") + require.NoError(t, err) + require.Equal(t, s, "2") +}