MCPcopy Create free account
hub / github.com/caarlos0/env / TestCustomParserBasicType

Function TestCustomParserBasicType

env_test.go:1236–1262  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1234}
1235
1236func TestCustomParserBasicType(t *testing.T) {
1237 type ConstT int32
1238
1239 type config struct {
1240 Const ConstT `env:"CONST_"`
1241 }
1242
1243 exp := ConstT(123)
1244 t.Setenv("CONST_", fmt.Sprintf("%d", exp))
1245
1246 customParserFunc := func(v string) (interface{}, error) {
1247 i, err := strconv.Atoi(v)
1248 if err != nil {
1249 return nil, err
1250 }
1251 r := ConstT(i)
1252 return r, nil
1253 }
1254
1255 cfg := &config{}
1256 err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
1257 reflect.TypeOf(ConstT(0)): customParserFunc,
1258 }})
1259
1260 isNoErr(t, err)
1261 isEqual(t, exp, cfg.Const)
1262}
1263
1264func TestCustomParserUint64Alias(t *testing.T) {
1265 type T uint64

Callers

nothing calls this directly

Calls 4

ParseWithOptionsFunction · 0.85
isNoErrFunction · 0.85
isEqualFunction · 0.85
ConstTTypeAlias · 0.70

Tested by

no test coverage detected