@@ -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+
391427func 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-
714714type configDuration struct {
715715 Duration time.Duration
716716}
0 commit comments