117 lines
2.4 KiB
Go
117 lines
2.4 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
var _ encoding.TextMarshaler = Env(0)
|
|
var _ encoding.TextUnmarshaler = new(Env)
|
|
var _ fmt.Stringer = Env(0)
|
|
|
|
func TestEnvString(t *testing.T) {
|
|
testcases := []struct {
|
|
expected string
|
|
val Env
|
|
}{
|
|
{"Dev", Dev},
|
|
{"Prod", Prod},
|
|
{"Qa", Qa},
|
|
{"Test", Test},
|
|
{"Env(0)", Env(0)},
|
|
{"Env(255)", Env(255)},
|
|
}
|
|
for _, testcase := range testcases {
|
|
t.Run(testcase.expected, func(sub *testing.T) {
|
|
assert.Equal(t, testcase.expected, testcase.val.String())
|
|
by, er := testcase.val.MarshalText()
|
|
assert.NoError(t, er)
|
|
assert.Equal(t, []byte(testcase.expected), by)
|
|
})
|
|
}
|
|
}
|
|
|
|
type unmarshaltest struct {
|
|
name string
|
|
in []byte
|
|
exp Env
|
|
err string
|
|
}
|
|
|
|
func TestEnvUnmarshalText(t *testing.T) {
|
|
testcases := []unmarshaltest{
|
|
{"empty", []byte{}, Env(0), "invalid value: "},
|
|
{"bad value", []byte("foobar"), Env(0), "invalid value: foobar"},
|
|
}
|
|
for _, e := range Envs {
|
|
testcases = append(testcases, unmarshaltest{e.String() + "-lower", []byte(strings.ToLower(e.String())), e, ""})
|
|
testcases = append(testcases, unmarshaltest{e.String() + "-upper", []byte(strings.ToUpper(e.String())), e, ""})
|
|
testcases = append(testcases, unmarshaltest{e.String(), []byte(e.String()), e, ""})
|
|
}
|
|
|
|
for _, testcase := range testcases {
|
|
t.Run(testcase.name, func(sub *testing.T) {
|
|
var e Env
|
|
er := (&e).UnmarshalText(testcase.in)
|
|
if testcase.err == "" {
|
|
assert.NoError(t, er)
|
|
assert.Equal(t, testcase.exp, e)
|
|
} else {
|
|
assert.Empty(t, e)
|
|
assert.ErrorContains(t, er, testcase.err)
|
|
assert.ErrorIs(t, er, ErrEnvUnmarshal)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnvUnmarshalTextNil(t *testing.T) {
|
|
var e *Env
|
|
er := e.UnmarshalText([]byte("prod"))
|
|
assert.ErrorContains(t, er, "nil pointer")
|
|
assert.ErrorIs(t, er, ErrEnvUnmarshal)
|
|
}
|
|
|
|
func TestValidEnv(t *testing.T) {
|
|
cases := [...]struct {
|
|
e Env
|
|
exp bool
|
|
}{
|
|
{Dev, true},
|
|
{Prod, true},
|
|
{Qa, true},
|
|
{Test, true},
|
|
{Env(0), false},
|
|
{Env(255), false},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(string(c.e), func(t *testing.T) {
|
|
assert.Equal(t, c.exp, ValidEnv(c.e))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidEnvOrDev(t *testing.T) {
|
|
cases := [...]struct {
|
|
give Env
|
|
exp Env
|
|
}{
|
|
{Dev, Dev},
|
|
{Prod, Prod},
|
|
{Qa, Qa},
|
|
{Test, Test},
|
|
{Env(0), Dev},
|
|
{Env(255), Dev},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(string(c.give), func(t *testing.T) {
|
|
assert.Equal(t, c.exp, ValidEnvOrDev(c.give))
|
|
})
|
|
}
|
|
}
|