From cab639ab83b14162986cc0a34700047274514d06 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 13 Feb 2025 11:22:03 -0800 Subject: [PATCH] chore: add test for decoding --- mapper_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mapper_test.go b/mapper_test.go index 113e9f5..a6ddbe3 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -756,3 +756,31 @@ func TestValuesThatLookLikeFlags(t *testing.T) { assert.NoError(t, err) assert.Equal(t, map[string]string{"-foo": "-bar"}, cli.Map) } + +type DecodeCLI struct { + Foo DecodeFoo `env:"FOO"` +} + +type DecodeFoo struct { + Bar string +} + +func (f DecodeFoo) Decode(ctx *kong.DecodeContext) error { + ctx.Value.Target.Set(reflect.ValueOf(struct { + Bar string + }{"baz"})) + return nil +} + +func TestDecode(t *testing.T) { + c := &DecodeCLI{} + parser, err := kong.New(c) + assert.NoError(t, err) + + t.Setenv("FOO", "foo") + + _, err = parser.Parse([]string{}) + assert.NoError(t, err) + + assert.Equal(t, c.Foo.Bar, "baz") +}