diff options
| author | Ryan McKern <[email protected]> | 2020-05-04 07:38:53 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-05-04 07:38:53 -0700 |
| commit | 81378bbcd8a1005f72b1e8d7579e5dd7b2d612ab (patch) | |
| tree | 51deaf6915294ce3f47bd8a5bf77613d7b716d82 /flag_test.go | |
| parent | 2e9d26c8c37aae03e3f9d4e90b7116f5accb7cab (diff) | |
Add exported functions to preserve `pkg/flag` compatibility (#220)
* Rename out() to Output()
This brings behavior inline with go's flag library, and allows for
printing output directly to whatever the current FlagSet is using for
output. This change will make it easier to correctly emit output to
stdout or stderr (e.g. a user has requested a help screen, which
should emit to stdout since it's the desired outcome).
* improve compat. with pkg/flag by adding Name()
pkg/flag has a public `Name()` function, which returns the name of the
flag set when called. This commit adds that function, as well as a
test for it.
* Streamline testing Name()
Testing `Name()` will move into its own explicit test, instead of
running inline during `TestAddFlagSet()`.
Co-authored-by: Chloe Kudryavtsev <[email protected]>
Co-authored-by: Chloe Kudryavtsev <[email protected]>
Diffstat (limited to 'flag_test.go')
| -rw-r--r-- | flag_test.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/flag_test.go b/flag_test.go index 7d02dbc..58a5d25 100644 --- a/flag_test.go +++ b/flag_test.go @@ -159,6 +159,16 @@ func TestAnnotation(t *testing.T) { } } +func TestName(t *testing.T) { + flagSetName := "bob" + f := NewFlagSet(flagSetName, ContinueOnError) + + givenName := f.Name() + if givenName != flagSetName { + t.Errorf("Unexpected result when retrieving a FlagSet's name: expected %s, but found %s", flagSetName, givenName) + } +} + func testParse(f *FlagSet, t *testing.T) { if f.Parsed() { t.Error("f.Parse() = true before Parse") @@ -854,6 +864,17 @@ func TestSetOutput(t *testing.T) { } } +func TestOutput(t *testing.T) { + var flags FlagSet + var buf bytes.Buffer + expect := "an example string" + flags.SetOutput(&buf) + fmt.Fprint(flags.Output(), expect) + if out := buf.String(); !strings.Contains(out, expect) { + t.Errorf("expected output %q; got %q", expect, out) + } +} + // This tests that one can reset the flags. This still works but not well, and is // superseded by FlagSet. func TestChangingArgs(t *testing.T) { |
