Skip to content

Commit

Permalink
Merge pull request #133 from gozeloglu/providerWithValue-env-test-add
Browse files Browse the repository at this point in the history
Add tests for ProviderWithValue func in env.go
  • Loading branch information
knadh committed Jan 19, 2022
2 parents ce5f63e + 3dec1b7 commit f600f4d
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions providers/env/env_test.go
Expand Up @@ -2,6 +2,7 @@ package env

import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)

Expand Down Expand Up @@ -42,3 +43,83 @@ func TestProvider(t *testing.T) {
})
}
}

func TestProviderWithValue(t *testing.T) {
testCases := []struct {
name string
prefix string
delim string
cb func(key string, value string) (string, interface{})
nilCallback bool
want *Env
}{
{
name: "Nil cb",
prefix: "TEST_",
delim: ".",
nilCallback: true,
want: &Env{
prefix: "TEST_",
delim: ".",
},
},
{
name: "Empty string nil cb",
prefix: "",
delim: ".",
nilCallback: true,
want: &Env{
prefix: "",
delim: ".",
},
},
{
name: "Return the same key-value pair in cb",
prefix: "TEST_",
delim: ".",
cb: func(key string, value string) (string, interface{}) {
return key, value
},
want: &Env{
prefix: "TEST_",
delim: ".",
cb: func(key string, value string) (string, interface{}) {
return key, value
},
},
},
{
name: "Custom cb function",
prefix: "TEST_",
delim: ".",
cb: func(key string, value string) (string, interface{}) {
key = strings.Replace(strings.TrimPrefix(strings.ToLower(key), "test_"), "_", ".", -1)
return key, value
},
want: &Env{
prefix: "TEST_",
delim: ".",
cb: func(key string, value string) (string, interface{}) {
key = strings.Replace(strings.TrimPrefix(strings.ToLower(key), "test_"), "_", ".", -1)
return key, value
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := ProviderWithValue(tc.prefix, tc.delim, tc.cb)
if tc.nilCallback {
assert.Equal(t, tc.want, got)
} else {
keyGot, valGot := got.cb("test_key_env_1", "test_val")
keyWant, valWant := tc.want.cb("test_key_env_1", "test_val")
assert.Equal(t, tc.prefix, got.prefix)
assert.Equal(t, tc.delim, got.delim)
assert.Equal(t, keyWant, keyGot)
assert.Equal(t, valWant, valGot)
}
})
}
}

0 comments on commit f600f4d

Please sign in to comment.