Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add required tests for internal/flag #15220

Merged
merged 5 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions go/internal/flag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestPreventGlogVFlagFromClobberingVersionFlagShorthand(t *testing.T) {

goflag.CommandLine = goflag.NewFlagSet(os.Args[0], goflag.ExitOnError)

var v, vtest bool
var v bool

goflag.BoolVar(&v, "v", true, "")

Expand All @@ -44,7 +44,7 @@ func TestPreventGlogVFlagFromClobberingVersionFlagShorthand(t *testing.T) {
assert.NotNil(t, f)
assert.Equal(t, "", f.Shorthand)

testFlagSet.BoolVar(&vtest, "vtest", true, "")
// The function should not throw any error if -v flag is already defined
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
PreventGlogVFlagFromClobberingVersionFlagShorthand(testFlagSet)
}

Expand Down Expand Up @@ -282,27 +282,16 @@ func TestArgs(t *testing.T) {
}

func TestIsZeroValue(t *testing.T) {
oldCommandLine := goflag.CommandLine

defer func() {
goflag.CommandLine = oldCommandLine
}()

var testFlag string

goflag.StringVar(&testFlag, "testflag", "default", "Description of testflag")
goflag.Parse()
testFlagSet := goflag.NewFlagSet("testFlagSet", goflag.ExitOnError)
testFlagSet.StringVar(&testFlag, "testflag", "default", "Description of testflag")

f := goflag.Lookup("testflag")
f := testFlagSet.Lookup("testflag")

// Test the isZeroValue function
result := isZeroValue(f, "")

assert.True(t, result)

// Test again with a non-zero value
result = isZeroValue(f, "anyValue")
if result {
t.Errorf("Expected false, got true. The value 'newvalue' should not be considered as zero value.")
}
assert.False(t, result)
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
}
17 changes: 2 additions & 15 deletions go/internal/flag/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,10 @@ func TestSetUsageWithNilFlagFilterAndPreface(t *testing.T) {
assert.Contains(t, output, "test epilogue")
}

type testBool struct{}

func (t testBool) IsBoolFlag() bool {
return true
}

func (t testBool) String() string {
return "true"
}

func (t testBool) Set(s string) error {
return nil
}

func TestSetUsageWithBoolFlag(t *testing.T) {
fs := goflag.NewFlagSet("test2", goflag.ExitOnError)
fs.Var(testBool{}, "t", "`t` flag")
var tBool bool
fs.BoolVar(&tBool, "t", true, "`t` flag")

opts := UsageOptions{
Preface: func(w io.Writer) {
Expand Down
Loading