Skip to content

Commit 430a5a2

Browse files
committed
tests, readme
1 parent 5f8ef04 commit 430a5a2

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type config struct {
3030
Port int `env:"PORT" required:"true"`
3131
Peers []string `env:"PEERS"` // you can use `delimiter` tag to specify separator, for example `delimiter:" "`
3232
ConnectionTimeout time.Duration `env:"TIMEOUT" default:"10s"`
33+
LogLevel string `env:"LOG_LEVEL" enum:"debug,info,error"`
3334
}
3435

3536
func main() {

env_test.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,42 @@ func TestEnvWithDefaultWhenProvided(t *testing.T) {
388388
Equals(t, "goodbye", config.Prop)
389389
}
390390

391+
func TestEnvEnumUnmatched(t *testing.T) {
392+
os.Setenv("PROP", "foo")
393+
394+
config := struct {
395+
Prop string `env:"PROP" enum:"1,2,3"`
396+
}{}
397+
398+
err := Set(&config)
399+
ErrorNotNil(t, err)
400+
Assert(t, strings.HasPrefix(err.Error(), `error setting "Prop": "foo" is not a member of [1,2,3]`))
401+
}
402+
403+
func TestEnvEnumMatchedString(t *testing.T) {
404+
os.Setenv("PROP", "foo")
405+
406+
config := struct {
407+
Prop string `env:"PROP" enum:"foo,bar,baz"`
408+
}{}
409+
410+
err := Set(&config)
411+
ErrorNil(t, err)
412+
Assert(t, config.Prop == "foo")
413+
}
414+
415+
func TestEnvEnumMatchedInteger(t *testing.T) {
416+
os.Setenv("PROP", "1")
417+
418+
config := struct {
419+
Prop int `env:"PROP" enum:"1,2,3"`
420+
}{}
421+
422+
err := Set(&config)
423+
ErrorNil(t, err)
424+
Assert(t, config.Prop == 1)
425+
}
426+
391427
func TestEnvWithDefaultWhenMissing(t *testing.T) {
392428
unsetEnvironment()
393429

@@ -675,42 +711,6 @@ func TestEnvPrefixed(t *testing.T) {
675711
Equals(t, "hello", config.Prop)
676712
}
677713

678-
func TestEnumUnmatched(t *testing.T) {
679-
os.Setenv("PROP", "foo")
680-
681-
config := struct {
682-
Prop string `env:"PROP" enum:"1,2,3"`
683-
}{}
684-
685-
err := Set(&config)
686-
ErrorNotNil(t, err)
687-
Assert(t, strings.HasPrefix(err.Error(), `error setting "Prop": "foo" is not a member of [1,2,3]`))
688-
}
689-
690-
func TestEnumMatchedString(t *testing.T) {
691-
os.Setenv("PROP", "foo")
692-
693-
config := struct {
694-
Prop string `env:"PROP" enum:"foo,bar,baz"`
695-
}{}
696-
697-
err := Set(&config)
698-
ErrorNil(t, err)
699-
Assert(t, config.Prop == "foo")
700-
}
701-
702-
func TestEnumMatchedInteger(t *testing.T) {
703-
os.Setenv("PROP", "1")
704-
705-
config := struct {
706-
Prop int `env:"PROP" enum:"1,2,3"`
707-
}{}
708-
709-
err := Set(&config)
710-
ErrorNil(t, err)
711-
Assert(t, config.Prop == 1)
712-
}
713-
714714
type configDuration struct {
715715
Duration time.Duration
716716
}

0 commit comments

Comments
 (0)