Skip to content

Commit a948a8c

Browse files
Merge pull request #1674 from alexandear/refactor/simplify-with-sprintf-q
refactor: use %q and %T to simplify fmt.Sprintf
2 parents 16020e8 + c3915e8 commit a948a8c

File tree

7 files changed

+14
-14
lines changed

7 files changed

+14
-14
lines changed

assert/assertion_compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedCompare
474474

475475
compareResult, isComparable := compare(e1, e2, e1Kind)
476476
if !isComparable {
477-
return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
477+
return Fail(t, fmt.Sprintf(`Can not compare type "%T"`, e1), msgAndArgs...)
478478
}
479479

480480
if !containsValue(allowedComparesResults, compareResult) {

assert/assertion_order.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func isOrdered(t TestingT, object interface{}, allowedComparesResults []compareR
3333
compareResult, isComparable := compare(prevValueInterface, valueInterface, firstValueKind)
3434

3535
if !isComparable {
36-
return Fail(t, fmt.Sprintf("Can not compare type \"%s\" and \"%s\"", reflect.TypeOf(value), reflect.TypeOf(prevValue)), msgAndArgs...)
36+
return Fail(t, fmt.Sprintf(`Can not compare type "%T" and "%T"`, value, prevValue), msgAndArgs...)
3737
}
3838

3939
if !containsValue(allowedComparesResults, compareResult) {

assert/assertions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs
462462
}
463463

464464
if !ObjectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType)) {
465-
return Fail(t, fmt.Sprintf("Object expected to be of type %v, but was %v", reflect.TypeOf(expectedType), reflect.TypeOf(object)), msgAndArgs...)
465+
return Fail(t, fmt.Sprintf("Object expected to be of type %T, but was %T", expectedType, object), msgAndArgs...)
466466
}
467467

468468
return true
@@ -1087,7 +1087,7 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
10871087
element := subsetList.Index(i).Interface()
10881088
ok, found := containsElement(list, element)
10891089
if !ok {
1090-
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...)
1090+
return Fail(t, fmt.Sprintf("%q could not be applied builtin len()", list), msgAndArgs...)
10911091
}
10921092
if !found {
10931093
return true

assert/assertions_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,7 @@ func TestRegexp(t *testing.T) {
21182118
}
21192119

21202120
for _, tc := range cases {
2121-
False(t, Regexp(mockT, tc.rx, tc.str), "Expected \"%s\" to not match \"%s\"", tc.rx, tc.str)
2121+
False(t, Regexp(mockT, tc.rx, tc.str), "Expected %q to not match %q", tc.rx, tc.str)
21222122
False(t, Regexp(mockT, regexp.MustCompile(tc.rx), tc.str))
21232123
False(t, Regexp(mockT, regexp.MustCompile(tc.rx), []byte(tc.str)))
21242124
True(t, NotRegexp(mockT, tc.rx, tc.str))
@@ -2151,23 +2151,23 @@ func TestZero(t *testing.T) {
21512151
mockT := new(testing.T)
21522152

21532153
for _, test := range zeros {
2154-
True(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
2154+
True(t, Zero(mockT, test, "%#v is not the %T zero value", test, test))
21552155
}
21562156

21572157
for _, test := range nonZeros {
2158-
False(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
2158+
False(t, Zero(mockT, test, "%#v is not the %T zero value", test, test))
21592159
}
21602160
}
21612161

21622162
func TestNotZero(t *testing.T) {
21632163
mockT := new(testing.T)
21642164

21652165
for _, test := range zeros {
2166-
False(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
2166+
False(t, NotZero(mockT, test, "%#v is not the %T zero value", test, test))
21672167
}
21682168

21692169
for _, test := range nonZeros {
2170-
True(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
2170+
True(t, NotZero(mockT, test, "%#v is not the %T zero value", test, test))
21712171
}
21722172
}
21732173

assert/forward_assertions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ func TestRegexpWrapper(t *testing.T) {
546546
}
547547

548548
for _, tc := range cases {
549-
False(t, assert.Regexp(tc.rx, tc.str), "Expected \"%s\" to not match \"%s\"", tc.rx, tc.str)
549+
False(t, assert.Regexp(tc.rx, tc.str), "Expected %q to not match %q", tc.rx, tc.str)
550550
False(t, assert.Regexp(regexp.MustCompile(tc.rx), tc.str))
551551
True(t, assert.NotRegexp(tc.rx, tc.str))
552552
True(t, assert.NotRegexp(regexp.MustCompile(tc.rx), tc.str))

assert/http_assertions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string,
138138

139139
contains := strings.Contains(body, fmt.Sprint(str))
140140
if !contains {
141-
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body), msgAndArgs...)
141+
Fail(t, fmt.Sprintf("Expected response body for %q to contain %q but found %q", url+"?"+values.Encode(), str, body), msgAndArgs...)
142142
}
143143

144144
return contains
@@ -158,7 +158,7 @@ func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url strin
158158

159159
contains := strings.Contains(body, fmt.Sprint(str))
160160
if contains {
161-
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body), msgAndArgs...)
161+
Fail(t, fmt.Sprintf("Expected response body for %q to NOT contain %q but found %q", url+"?"+values.Encode(), str, body), msgAndArgs...)
162162
}
163163

164164
return !contains

mock/mock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
504504
// expected call found, but it has already been called with repeatable times
505505
if call != nil {
506506
m.mutex.Unlock()
507-
m.fail("\nassert: mock: The method has been called over %d times.\n\tEither do one more Mock.On(\"%s\").Return(...), or remove extra call.\n\tThis call was unexpected:\n\t\t%s\n\tat: %s", call.totalCalls, methodName, callString(methodName, arguments, true), assert.CallerInfo())
507+
m.fail("\nassert: mock: The method has been called over %d times.\n\tEither do one more Mock.On(%#v).Return(...), or remove extra call.\n\tThis call was unexpected:\n\t\t%s\n\tat: %s", call.totalCalls, methodName, callString(methodName, arguments, true), assert.CallerInfo())
508508
}
509509
// we have to fail here - because we don't know what to do
510510
// as the return arguments. This is because:
@@ -524,7 +524,7 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
524524
assert.CallerInfo(),
525525
)
526526
} else {
527-
m.fail("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo())
527+
m.fail("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(%#v).Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo())
528528
}
529529
}
530530

0 commit comments

Comments
 (0)