MCPcopy Create free account
hub / github.com/alecthomas/kong / TestPathMapperUsingStringPointer

Function TestPathMapperUsingStringPointer

mapper_test.go:513–556  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

511}
512
513func TestPathMapperUsingStringPointer(t *testing.T) {
514 type CLI struct {
515 Path *string `type:"path"`
516 }
517 var cli CLI
518
519 t.Run("With value", func(t *testing.T) {
520 pwd, err := os.Getwd()
521 assert.NoError(t, err)
522 p := mustNew(t, &cli)
523 _, err = p.Parse([]string{"--path", "."})
524 assert.NoError(t, err)
525 assert.NotZero(t, cli.Path)
526 assert.Equal(t, pwd, *cli.Path)
527 })
528
529 t.Run("Zero value", func(t *testing.T) {
530 p := mustNew(t, &cli)
531 _, err := p.Parse([]string{"--path", ""})
532 assert.NoError(t, err)
533 assert.NotZero(t, cli.Path)
534 wd, err := os.Getwd()
535 assert.NoError(t, err)
536 assert.Equal(t, wd, *cli.Path)
537 })
538
539 t.Run("Without value", func(t *testing.T) {
540 p := mustNew(t, &cli)
541 _, err := p.Parse([]string{"--"})
542 assert.NoError(t, err)
543 assert.Equal(t, nil, cli.Path)
544 })
545
546 t.Run("Non-string pointer", func(t *testing.T) {
547 type CLI struct {
548 Path *any `type:"path"`
549 }
550 var cli CLI
551 p := mustNew(t, &cli)
552 _, err := p.Parse([]string{"--path", ""})
553 assert.Error(t, err)
554 assert.Contains(t, err.Error(), `"path" type must be applied to a string`)
555 })
556}
557
558func TestPathMapperWithDefaultUsingStringPointer(t *testing.T) {
559 type CLI struct {

Callers

nothing calls this directly

Calls 4

mustNewFunction · 0.85
RunMethod · 0.45
ParseMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected