Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Sep 20, 2022
1 parent 0c6a9f3 commit 8b82618
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 98 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ issues:
- 'bad syntax for struct tag key'
- 'bad syntax for struct tag pair'
- 'result .* \(error\) is always nil'
- 'package io/ioutil is deprecated'
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/go
2 changes: 1 addition & 1 deletion bin/gofmt
191 changes: 95 additions & 96 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1742,40 +1742,40 @@ func TestStringPointer(t *testing.T) {
Foo *string
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--foo", "wtf"})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.Foo)
require.Equal(t, "wtf", *cli.Foo)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.Foo)
assert.Equal(t, "wtf", *cli.Foo)
}

func TestStringPointerNoValue(t *testing.T) {
var cli struct {
Foo *string
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{})
require.NoError(t, err)
require.NotNil(t, ctx)
require.Nil(t, cli.Foo)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.Zero(t, cli.Foo)
}

func TestStringPointerDefault(t *testing.T) {
var cli struct {
Foo *string `default:"stuff"`
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.Foo)
require.Equal(t, "stuff", *cli.Foo)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.Foo)
assert.Equal(t, "stuff", *cli.Foo)
}

func TestStringPointerAliasNoValue(t *testing.T) {
Expand All @@ -1784,12 +1784,12 @@ func TestStringPointerAliasNoValue(t *testing.T) {
F *Foo
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{})
require.NoError(t, err)
require.NotNil(t, ctx)
require.Nil(t, cli.F)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.Zero(t, cli.F)
}

func TestStringPointerAlias(t *testing.T) {
Expand All @@ -1798,13 +1798,13 @@ func TestStringPointerAlias(t *testing.T) {
F *Foo
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--f=value"})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.F)
require.Equal(t, Foo("value"), *cli.F)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.F)
assert.Equal(t, Foo("value"), *cli.F)
}

func TestStringPointerEmptyValue(t *testing.T) {
Expand All @@ -1813,15 +1813,15 @@ func TestStringPointerEmptyValue(t *testing.T) {
G *string
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--f", "", "--g="})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.F)
require.NotNil(t, cli.G)
require.Equal(t, "", *cli.F)
require.Equal(t, "", *cli.G)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.F)
assert.NotZero(t, cli.G)
assert.Equal(t, "", *cli.F)
assert.Equal(t, "", *cli.G)
}

func TestIntPtr(t *testing.T) {
Expand All @@ -1830,140 +1830,139 @@ func TestIntPtr(t *testing.T) {
G *int
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--f=6"})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.F)
require.Nil(t, cli.G)
require.Equal(t, 6, *cli.F)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.F)
assert.Zero(t, cli.G)
assert.Equal(t, 6, *cli.F)
}

func TestBoolPtr(t *testing.T) {
var cli struct {
X *bool
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--x"})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.X)
require.Equal(t, true, *cli.X)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.X)
assert.Equal(t, true, *cli.X)
}

func TestBoolPtrFalse(t *testing.T) {
var cli struct {
X *bool
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--x=false"})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.X)
require.Equal(t, false, *cli.X)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.X)
assert.Equal(t, false, *cli.X)
}

func TestBoolPtrNegated(t *testing.T) {
var cli struct {
X *bool `negatable:""`
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--no-x"})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.X)
require.Equal(t, false, *cli.X)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.X)
assert.Equal(t, false, *cli.X)
}

func TestNilNegatableBoolPtr(t *testing.T) {
var cli struct {
X *bool `negatable:""`
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{})
require.NoError(t, err)
require.NotNil(t, ctx)
require.Nil(t, cli.X)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.Zero(t, cli.X)
}

func TestBoolPtrNil(t *testing.T) {
var cli struct {
X *bool
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{})
require.NoError(t, err)
require.NotNil(t, ctx)
require.Nil(t, cli.X)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.Zero(t, cli.X)
}

func TestUnsupportedPtr(t *testing.T) {
//nolint:structcheck,unused
type Foo struct {
x int
y int
x int // nolint
y int // nolint
}

var cli struct {
F *Foo
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--f=whatever"})
require.Nil(t, ctx)
require.Error(t, err)
require.Equal(t, "--f: cannot find mapper for kong_test.Foo", err.Error())
assert.Zero(t, ctx)
assert.Error(t, err)
assert.Equal(t, "--f: cannot find mapper for kong_test.Foo", err.Error())
}

func TestEnumPtr(t *testing.T) {
var cli struct {
X *string `enum:"A,B,C" default:"C"`
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{"--x=A"})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.X)
require.Equal(t, "A", *cli.X)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.X)
assert.Equal(t, "A", *cli.X)
}

func TestEnumPtrOmitted(t *testing.T) {
var cli struct {
X *string `enum:"A,B,C" default:"C"`
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{})
require.NoError(t, err)
require.NotNil(t, ctx)
require.NotNil(t, cli.X)
require.Equal(t, "C", *cli.X)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.NotZero(t, cli.X)
assert.Equal(t, "C", *cli.X)
}

func TestEnumPtrOmittedNoDefault(t *testing.T) {
var cli struct {
X *string `enum:"A,B,C"`
}
k, err := kong.New(&cli)
require.NoError(t, err)
require.NotNil(t, k)
assert.NoError(t, err)
assert.NotZero(t, k)
ctx, err := k.Parse([]string{})
require.NoError(t, err)
require.NotNil(t, ctx)
require.Nil(t, cli.X)
assert.NoError(t, err)
assert.NotZero(t, ctx)
assert.Zero(t, cli.X)
}

0 comments on commit 8b82618

Please sign in to comment.