| 543 | } |
| 544 | |
| 545 | func TestPathMapperUsingStringPointer(t *testing.T) { |
| 546 | type CLI struct { |
| 547 | Path *string `type:"path"` |
| 548 | } |
| 549 | var cli CLI |
| 550 | |
| 551 | t.Run("With value", func(t *testing.T) { |
| 552 | pwd, err := os.Getwd() |
| 553 | assert.NoError(t, err) |
| 554 | p := mustNew(t, &cli) |
| 555 | _, err = p.Parse([]string{"--path", "."}) |
| 556 | assert.NoError(t, err) |
| 557 | assert.NotZero(t, cli.Path) |
| 558 | assert.Equal(t, pwd, *cli.Path) |
| 559 | }) |
| 560 | |
| 561 | t.Run("Zero value", func(t *testing.T) { |
| 562 | p := mustNew(t, &cli) |
| 563 | _, err := p.Parse([]string{"--path", ""}) |
| 564 | assert.NoError(t, err) |
| 565 | assert.NotZero(t, cli.Path) |
| 566 | wd, err := os.Getwd() |
| 567 | assert.NoError(t, err) |
| 568 | assert.Equal(t, wd, *cli.Path) |
| 569 | }) |
| 570 | |
| 571 | t.Run("Without value", func(t *testing.T) { |
| 572 | p := mustNew(t, &cli) |
| 573 | _, err := p.Parse([]string{"--"}) |
| 574 | assert.NoError(t, err) |
| 575 | assert.Equal(t, nil, cli.Path) |
| 576 | }) |
| 577 | |
| 578 | t.Run("Non-string pointer", func(t *testing.T) { |
| 579 | type CLI struct { |
| 580 | Path *any `type:"path"` |
| 581 | } |
| 582 | var cli CLI |
| 583 | p := mustNew(t, &cli) |
| 584 | _, err := p.Parse([]string{"--path", ""}) |
| 585 | assert.Error(t, err) |
| 586 | assert.Contains(t, err.Error(), `"path" type must be applied to a string`) |
| 587 | }) |
| 588 | } |
| 589 | |
| 590 | func TestPathMapperWithDefaultUsingStringPointer(t *testing.T) { |
| 591 | type CLI struct { |