Skip to content

Commit

Permalink
Merge pull request #54 from muzzammilshahid/internal-tests
Browse files Browse the repository at this point in the history
Add tests for Map helper
  • Loading branch information
muzzammilshahid authored Jul 10, 2024
2 parents 3bfbefa + 55b4834 commit 9ddebed
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions internal/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package internal_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/xconnio/wampproto-go/internal"
)

func TestMapDelete(t *testing.T) {
m := &internal.Map[string, int]{}
m.Store("key1", 100)

m.Delete("key1")

_, ok := m.Load("key1")
require.False(t, ok)
}

func TestMapLoad(t *testing.T) {
m := &internal.Map[string, bool]{}
m.Store("key1", true)

value, ok := m.Load("key1")
require.True(t, ok)
require.True(t, value)

_, ok = m.Load("key2")
require.False(t, ok)
}

func TestMapLoadAndDelete(t *testing.T) {
m := &internal.Map[string, int]{}
m.Store("key1", 100)

value, loaded := m.LoadAndDelete("key1")
require.True(t, loaded)
require.Equal(t, 100, value)

_, loaded = m.Load("key1")
require.False(t, loaded)

_, loaded = m.LoadAndDelete("key2")
require.False(t, loaded)
}

func TestMapLoadOrStore(t *testing.T) {
m := &internal.Map[string, string]{}

actual, loaded := m.LoadOrStore("key1", "foo")
require.False(t, loaded)
require.Equal(t, "foo", actual)

actual, loaded = m.LoadOrStore("key1", "bar")
require.True(t, loaded)
require.Equal(t, "foo", actual)
}

func TestMapRange(t *testing.T) {
m := &internal.Map[string, int]{}
m.Store("key1", 100)
m.Store("key2", 200)

keys := make(map[string]bool)
values := make(map[int]bool)

m.Range(func(key string, value int) bool {
keys[key] = true
values[value] = true
return true
})

require.True(t, keys["key1"])
require.True(t, keys["key2"])
require.True(t, values[100])
require.True(t, values[200])
}

func TestMapStore(t *testing.T) {
m := &internal.Map[string, int]{}
m.Store("key1", 100)

value, ok := m.Load("key1")
require.True(t, ok)
require.Equal(t, 100, value)
}

0 comments on commit 9ddebed

Please sign in to comment.