Skip to content

Commit 011db0c

Browse files
committed
- update text_test.go based on PR review comments
- return is missing in one error path of GetText(), fixed
1 parent 2a2b77a commit 011db0c

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

text.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (f *FlagSet) GetText(name string, out encoding.TextUnmarshaler) error {
5555
return fmt.Errorf("flag accessed but not defined: %s", name)
5656
}
5757
if flag.Value.Type() != reflect.TypeOf(out).Name() {
58-
fmt.Errorf("trying to get %s value of flag of type %s", reflect.TypeOf(out).Name(), flag.Value.Type())
58+
return fmt.Errorf("trying to get %s value of flag of type %s", reflect.TypeOf(out).Name(), flag.Value.Type())
5959
}
6060
return out.UnmarshalText([]byte(flag.Value.String()))
6161
}

text_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestText(t *testing.T) {
2020
expected time.Time
2121
}{
2222
{"2003-01-02T15:04:05Z", true, time.Date(2003, 1, 2, 15, 04, 05, 0, time.UTC)},
23-
{"2003-01-02 15:05:01", false, time.Date(2002, 1, 2, 15, 05, 05, 07, time.UTC)},
23+
{"2003-01-02 15:05:01", false, time.Time{}}, //negative case, invalid layout
2424
{"2024-11-22T03:01:02Z", true, time.Date(2024, 11, 22, 3, 1, 02, 0, time.UTC)},
2525
{"2006-01-02T15:04:05+07:00", true, time.Date(2006, 1, 2, 15, 4, 5, 0, time.FixedZone("UTC+7", 7*60*60))},
2626
}
@@ -33,21 +33,24 @@ func TestText(t *testing.T) {
3333
tc := &testCases[i]
3434
arg := fmt.Sprintf("--time=%s", tc.input)
3535
err := f.Parse([]string{arg})
36-
if err != nil && tc.success == true {
37-
t.Errorf("expected success, got %q", err)
36+
if err != nil {
37+
if tc.success {
38+
t.Errorf("expected parsing to succeed, but got %q", err)
39+
}
3840
continue
39-
} else if err == nil && tc.success == false {
40-
t.Errorf("expected failure, but succeeded")
41+
}
42+
if !tc.success {
43+
t.Errorf("expected parsing failure, but parsing succeeded")
4144
continue
42-
} else if tc.success {
43-
parsedT := new(time.Time)
44-
err := f.GetText("time", parsedT)
45-
if err != nil {
46-
t.Errorf("Got error trying to fetch the time flag: %v", err)
47-
}
48-
if !parsedT.Equal(tc.expected) {
49-
t.Errorf("expected %q, got %q", tc.expected, parsedT)
50-
}
5145
}
46+
parsedT := new(time.Time)
47+
err = f.GetText("time", parsedT)
48+
if err != nil {
49+
t.Errorf("Got error trying to fetch the time flag: %v", err)
50+
}
51+
if !parsedT.Equal(tc.expected) {
52+
t.Errorf("expected %q, got %q", tc.expected, parsedT)
53+
}
54+
5255
}
5356
}

0 commit comments

Comments
 (0)