Skip to content

Commit fb31c64

Browse files
test: add tests for helpers
Signed-off-by: Mateusz Urbanek <murbanek@akamai.com>
1 parent 1575573 commit fb31c64

File tree

2 files changed

+124
-3
lines changed

2 files changed

+124
-3
lines changed

observability/wrappers/helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023 Akamai Technologies, Inc.
2+
Copyright 2024 Akamai Technologies, Inc.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,10 +16,10 @@ limitations under the License.
1616

1717
package wrappers
1818

19-
func GetValue[T any](params map[string]any, key string) (T, bool) {
19+
func GetValue[T any](m map[string]any, key string) (T, bool) {
2020
var zero T
2121

22-
if val, ok := params[key]; ok {
22+
if val, ok := m[key]; ok {
2323
if val, ok := val.(T); ok {
2424
return val, true
2525
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Copyright 2024 Akamai Technologies, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package wrappers
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestOptional(t *testing.T) {
26+
t.Parallel()
27+
28+
t.Run("int", func(t *testing.T) {
29+
t.Parallel()
30+
31+
val := 1
32+
33+
ret := Optional[int](&val)
34+
assert.Equal(t, val, ret)
35+
36+
ret = Optional[int](nil)
37+
assert.Equal(t, 0, ret)
38+
})
39+
40+
t.Run("string", func(t *testing.T) {
41+
t.Parallel()
42+
43+
val := "foo"
44+
45+
ret := Optional[string](&val)
46+
assert.Equal(t, val, ret)
47+
48+
ret = Optional[string](nil)
49+
assert.Equal(t, "", ret)
50+
})
51+
52+
t.Run("slice", func(t *testing.T) {
53+
t.Parallel()
54+
55+
val := []string{"foo", "bar"}
56+
57+
ret := Optional[[]string](&val)
58+
assert.Equal(t, val, ret)
59+
60+
ret = Optional[[]string](nil)
61+
assert.Equal(t, []string(nil), ret)
62+
})
63+
}
64+
65+
func TestGetValue(t *testing.T) {
66+
t.Parallel()
67+
68+
t.Run("int", func(t *testing.T) {
69+
t.Parallel()
70+
71+
key := "key"
72+
val := 3
73+
valueMap := map[string]any{key: val}
74+
75+
ret, ok := GetValue[int](valueMap, key)
76+
assert.True(t, ok)
77+
assert.Equal(t, val, ret)
78+
79+
_, ok = GetValue[int](valueMap, "foo")
80+
assert.False(t, ok)
81+
82+
_, ok = GetValue[string](valueMap, key)
83+
assert.False(t, ok)
84+
})
85+
86+
t.Run("string", func(t *testing.T) {
87+
t.Parallel()
88+
89+
key := "key"
90+
val := "val"
91+
valueMap := map[string]any{key: val}
92+
93+
ret, ok := GetValue[string](valueMap, key)
94+
assert.True(t, ok)
95+
assert.Equal(t, val, ret)
96+
97+
_, ok = GetValue[string](valueMap, "foo")
98+
assert.False(t, ok)
99+
100+
_, ok = GetValue[int](valueMap, key)
101+
assert.False(t, ok)
102+
})
103+
104+
t.Run("slice", func(t *testing.T) {
105+
t.Parallel()
106+
107+
key := "key"
108+
val := []string{"foo", "bar"}
109+
valueMap := map[string]any{key: val}
110+
111+
ret, ok := GetValue[[]string](valueMap, key)
112+
assert.True(t, ok)
113+
assert.Equal(t, val, ret)
114+
115+
_, ok = GetValue[[]string](valueMap, "foo")
116+
assert.False(t, ok)
117+
118+
_, ok = GetValue[int](valueMap, key)
119+
assert.False(t, ok)
120+
})
121+
}

0 commit comments

Comments
 (0)