Skip to content

Commit 62e4ac9

Browse files
committed
feat: use quotes when reporting issue with string conversion
I faced an issue when testing with "string" string Then I realized that using quotes would make it clearer. Think about empty strings or strings with spaces.
1 parent f3f31ee commit 62e4ac9

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

asserters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func requireErrorContains(t *testing.T, err error, text string) {
7979

8080
errMessage := err.Error()
8181
if !strings.Contains(errMessage, text) {
82-
t.Fatalf("error message should contain %q: %q", text, errMessage)
82+
t.Fatalf("error message should contain %#q: %#q", text, errMessage)
8383
}
8484
}
8585

conversion_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,33 +1693,33 @@ func TestConvert(t *testing.T) {
16931693
"empty string": MapTest[string, uint]{
16941694
Input: "",
16951695
ExpectedError: safecast.ErrStringConversion,
1696-
ErrorContains: "cannot convert from string to uint",
1696+
ErrorContains: "cannot convert from `` to uint",
16971697
},
16981698
"simple space": MapTest[string, uint]{
16991699
Input: " ",
17001700
ExpectedError: safecast.ErrStringConversion,
1701-
ErrorContains: "cannot convert from string to uint",
1701+
ErrorContains: "cannot convert from ` ` to uint",
17021702
},
17031703
"simple dot": MapTest[string, uint]{
17041704
Input: ".",
17051705
ExpectedError: safecast.ErrStringConversion,
1706-
ErrorContains: "cannot convert from string . to uint"},
1706+
ErrorContains: "cannot convert from `.` to uint"},
17071707
"simple dash": MapTest[string, uint]{
17081708
Input: "-",
17091709
ExpectedError: safecast.ErrStringConversion,
1710-
ErrorContains: "cannot convert from string - to uint"},
1710+
ErrorContains: "cannot convert from `-` to uint"},
17111711
"invalid string": MapTest[string, uint]{
17121712
Input: "abc",
17131713
ExpectedError: safecast.ErrStringConversion,
1714-
ErrorContains: `cannot convert from string abc to uint`},
1714+
ErrorContains: "cannot convert from `abc` to uint"},
17151715
"invalid string with dot": MapTest[string, uint]{
17161716
Input: "ab.c",
17171717
ExpectedError: safecast.ErrStringConversion,
1718-
ErrorContains: `cannot convert from string ab.c to uint`},
1718+
ErrorContains: "cannot convert from `ab.c` to uint"},
17191719
"strings with leading +": MapTest[string, uint]{
17201720
Input: "+42",
17211721
ExpectedError: safecast.ErrStringConversion,
1722-
ErrorContains: "cannot convert from string +42 to uint",
1722+
ErrorContains: "cannot convert from `+42` to uint",
17231723
},
17241724
"invalid string multiple leading dashes": MapTest[string, uint]{Input: "--42", ExpectedError: safecast.ErrStringConversion},
17251725
"invalid string with dash": MapTest[string, uint]{Input: "-abc", ExpectedError: safecast.ErrStringConversion},
@@ -2222,5 +2222,5 @@ func ExampleRequireConvert_failure() {
22222222

22232223
// Output:
22242224
// --- FAIL:
2225-
// conversion issue: cannot convert from string foo to uint8 (base auto-detection)
2225+
// conversion issue: cannot convert from `foo` to uint8 (base auto-detection)
22262226
}

errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (e errorHelper[NumOut]) Error() string {
4545
errMessage = fmt.Sprintf("%s: %v (%T) is not supported", errMessage, e.value, e.value)
4646
case errors.Is(e.err, ErrStringConversion):
4747
targetType := NumOut(0)
48-
return fmt.Sprintf("%s: cannot convert from string %s to %T (base auto-detection)", errMessage, e.value, targetType)
48+
return fmt.Sprintf("%s: cannot convert from %#q to %T (base auto-detection)", errMessage, e.value, targetType)
4949
}
5050

5151
if e.err != nil {

examples_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func ExampleConvert() {
377377
// 17 <nil>
378378
// 0 conversion issue: -1 (int8) is less than 0 (uint): minimum value for this type exceeded
379379
// 0 conversion issue: -1 (int64) is less than 0 (uint): minimum value for this type exceeded
380-
// 0 conversion issue: cannot convert from string abc to uint (base auto-detection)
380+
// 0 conversion issue: cannot convert from `abc` to uint (base auto-detection)
381381
// 0 conversion issue: -1.1 (float64) is less than 0 (uint): minimum value for this type exceeded
382382
// 12345 <nil>
383383
}

0 commit comments

Comments
 (0)