Skip to content

Commit

Permalink
test: add tests for helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Urbanek <[email protected]>
  • Loading branch information
shanduur-akamai committed Jul 29, 2024
1 parent 5c13b82 commit 3550f63
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 3 deletions.
6 changes: 3 additions & 3 deletions observability/wrappers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 Akamai Technologies, Inc.
Copyright 2024 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,10 @@ limitations under the License.

package wrappers

func GetValue[T any](params map[string]any, key string) (T, bool) {
func GetValue[T any](m map[string]any, key string) (T, bool) {
var zero T

if val, ok := params[key]; ok {
if val, ok := m[key]; ok {
if val, ok := val.(T); ok {
return val, true
}
Expand Down
121 changes: 121 additions & 0 deletions observability/wrappers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2024 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package wrappers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestOptional(t *testing.T) {
t.Parallel()

t.Run("int", func(t *testing.T) {
t.Parallel()

val := 1

ret := Optional[int](&val)
assert.Equal(t, val, ret)

ret = Optional[int](nil)
assert.Equal(t, 0, ret)
})

t.Run("string", func(t *testing.T) {
t.Parallel()

val := "foo"

ret := Optional[string](&val)
assert.Equal(t, val, ret)

ret = Optional[string](nil)
assert.Equal(t, "", ret)
})

t.Run("slice", func(t *testing.T) {
t.Parallel()

val := []string{"foo", "bar"}

ret := Optional[[]string](&val)
assert.Equal(t, val, ret)

ret = Optional[[]string](nil)
assert.Equal(t, []string(nil), ret)
})
}

func TestGetValue(t *testing.T) {
t.Parallel()

t.Run("int", func(t *testing.T) {
t.Parallel()

key := "key"
val := 3
valueMap := map[string]any{key: val}

ret, ok := GetValue[int](valueMap, key)
assert.True(t, ok)
assert.Equal(t, val, ret)

_, ok = GetValue[int](valueMap, "foo")
assert.False(t, ok)

_, ok = GetValue[string](valueMap, key)
assert.False(t, ok)
})

t.Run("string", func(t *testing.T) {
t.Parallel()

key := "key"
val := "val"
valueMap := map[string]any{key: val}

ret, ok := GetValue[string](valueMap, key)
assert.True(t, ok)
assert.Equal(t, val, ret)

_, ok = GetValue[string](valueMap, "foo")
assert.False(t, ok)

_, ok = GetValue[int](valueMap, key)
assert.False(t, ok)
})

t.Run("slice", func(t *testing.T) {
t.Parallel()

key := "key"
val := []string{"foo", "bar"}
valueMap := map[string]any{key: val}

ret, ok := GetValue[[]string](valueMap, key)
assert.True(t, ok)
assert.Equal(t, val, ret)

_, ok = GetValue[[]string](valueMap, "foo")
assert.False(t, ok)

_, ok = GetValue[int](valueMap, key)
assert.False(t, ok)
})
}

0 comments on commit 3550f63

Please sign in to comment.