2024-09-13 13:38:30 -05:00
package config
import (
2025-01-27 15:23:46 -06:00
"encoding"
"fmt"
"strings"
2024-09-13 13:38:30 -05:00
"testing"
"github.com/stretchr/testify/assert"
)
2025-01-27 15:23:46 -06:00
var _ encoding . TextMarshaler = Env ( 0 )
var _ encoding . TextUnmarshaler = new ( Env )
var _ fmt . Stringer = Env ( 0 )
2024-09-13 13:38:30 -05:00
func TestEnvString ( t * testing . T ) {
2025-01-27 15:23:46 -06:00
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 )
}
2024-09-13 13:38:30 -05:00
} )
}
}
2025-01-27 15:23:46 -06:00
func TestEnvUnmarshalTextNil ( t * testing . T ) {
var e * Env
er := e . UnmarshalText ( [ ] byte ( "prod" ) )
assert . ErrorContains ( t , er , "nil pointer" )
assert . ErrorIs ( t , er , ErrEnvUnmarshal )
}
2024-09-13 13:38:30 -05:00
func TestValidEnv ( t * testing . T ) {
cases := [ ... ] struct {
e Env
exp bool
} {
2024-10-28 11:50:18 -05:00
{ Dev , true } ,
{ Prod , true } ,
{ Qa , true } ,
{ Test , true } ,
2025-01-27 15:23:46 -06:00
{ Env ( 0 ) , false } ,
{ Env ( 255 ) , false } ,
2024-09-13 13:38:30 -05:00
}
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
} {
2024-10-28 11:50:18 -05:00
{ Dev , Dev } ,
{ Prod , Prod } ,
{ Qa , Qa } ,
{ Test , Test } ,
2025-01-27 15:23:46 -06:00
{ Env ( 0 ) , Dev } ,
{ Env ( 255 ) , Dev } ,
2024-09-13 13:38:30 -05:00
}
for _ , c := range cases {
t . Run ( string ( c . give ) , func ( t * testing . T ) {
assert . Equal ( t , c . exp , ValidEnvOrDev ( c . give ) )
} )
}
}