flag: incorportate reviews

This commit is contained in:
KimMachineGun 2020-11-22 17:52:25 +09:00
parent fdc76e071e
commit f06b1e1767
1 changed files with 18 additions and 13 deletions

View File

@ -656,17 +656,18 @@ func TestExitCode(t *testing.T) {
} }
} }
func mustPanic(t *testing.T, expected string, f func()) { func mustPanic(t *testing.T, testName string, expected string, f func()) {
t.Helper()
defer func() { defer func() {
switch msg := recover().(type) { switch msg := recover().(type) {
case nil: case nil:
t.Error("did not panic") t.Errorf("%s\n: expected panic(%q), but did not panic", testName, expected)
case string: case string:
if msg != expected { if msg != expected {
t.Errorf("expected %q, bug got %q", expected, msg) t.Errorf("%s\n: expected panic(%q), but got panic(%q)", testName, expected, msg)
} }
default: default:
t.Errorf("unexpected panic value: %T(%v)", msg, msg) t.Errorf("%s\n: expected panic(%q), but got panic(%T%v)", testName, expected, msg, msg)
} }
}() }()
f() f()
@ -688,16 +689,18 @@ func TestInvalidFlags(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
fs := NewFlagSet("", ContinueOnError) testName := fmt.Sprintf("FlagSet.Var(&v, %q, \"\")", test.flag)
var buf bytes.Buffer
fs.SetOutput(&buf)
mustPanic(t, test.errorMsg, func() { fs := NewFlagSet("", ContinueOnError)
buf := bytes.NewBuffer(nil)
fs.SetOutput(buf)
mustPanic(t, testName, test.errorMsg, func() {
var v flagVar var v flagVar
fs.Var(&v, test.flag, "") fs.Var(&v, test.flag, "")
}) })
if msg := test.errorMsg + "\n"; msg != buf.String() { if msg := test.errorMsg + "\n"; msg != buf.String() {
t.Errorf("expected %q, bug got %q", msg, buf.String()) t.Errorf("%s\n: unexpected output: expected %q, bug got %q", testName, msg, buf)
} }
} }
} }
@ -718,18 +721,20 @@ func TestRedefinedFlags(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
testName := fmt.Sprintf("flag redefined in FlagSet(%q)", test.flagSetName)
fs := NewFlagSet(test.flagSetName, ContinueOnError) fs := NewFlagSet(test.flagSetName, ContinueOnError)
var buf bytes.Buffer buf := bytes.NewBuffer(nil)
fs.SetOutput(&buf) fs.SetOutput(buf)
var v flagVar var v flagVar
fs.Var(&v, "foo", "") fs.Var(&v, "foo", "")
mustPanic(t, test.errorMsg, func() { mustPanic(t, testName, test.errorMsg, func() {
fs.Var(&v, "foo", "") fs.Var(&v, "foo", "")
}) })
if msg := test.errorMsg + "\n"; msg != buf.String() { if msg := test.errorMsg + "\n"; msg != buf.String() {
t.Errorf("expected %q, bug got %q", msg, buf.String()) t.Errorf("%s\n: unexpected output: expected %q, bug got %q", testName, msg, buf)
} }
} }
} }