| 511 | } |
| 512 | |
| 513 | func 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 | |
| 558 | func TestPathMapperWithDefaultUsingStringPointer(t *testing.T) { |
| 559 | type CLI struct { |