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

Function TestTypeCustomParserBasicInvalid

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

Source from the content-addressed store, hash-verified

1294}
1295
1296func TestTypeCustomParserBasicInvalid(t *testing.T) {
1297 type ConstT int32
1298
1299 type config struct {
1300 Const ConstT `env:"CONST_"`
1301 }
1302
1303 t.Setenv("CONST_", "foobar")
1304
1305 customParserFunc := func(_ string) (interface{}, error) {
1306 return nil, errors.New("random error")
1307 }
1308
1309 cfg := &config{}
1310 err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
1311 reflect.TypeOf(ConstT(0)): customParserFunc,
1312 }})
1313
1314 isEqual(t, cfg.Const, ConstT(0))
1315 isErrorWithMessage(t, err, `env: parse error on field "Const" of type "env.ConstT": random error`)
1316 isTrue(t, errors.Is(err, ParseError{}))
1317}
1318
1319func TestCustomParserNotCalledForNonAlias(t *testing.T) {
1320 type T uint64

Callers

nothing calls this directly

Calls 6

ParseWithOptionsFunction · 0.85
isEqualFunction · 0.85
isErrorWithMessageFunction · 0.85
isTrueFunction · 0.85
IsMethod · 0.80
ConstTTypeAlias · 0.70

Tested by

no test coverage detected