Skip to content

Commit

Permalink
refactor: Replace(a, b, -1) → ReplaceAll(a, b)
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Oct 17, 2022
1 parent 766775c commit e1156b8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion helpers/tdhttp/internal/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func canBackquote(b []byte) bool {
}

func replaceCrLf(b []byte) []byte {
return bytes.Replace(b, []byte("\r\n"), []byte("\n"), -1) //nolint: gocritic
return bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n"))
}

func backquote(b []byte) ([]byte, bool) {
Expand Down
4 changes: 2 additions & 2 deletions helpers/tdhttp/multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestMultipartPart(t *testing.T) {
if assert.CmpNoError(err) {
_, err := io.Copy(&final, part)
if assert.CmpNoError(err) {
assert.Cmp(final.String(), strings.Replace(expected, "%CR", "\r", -1)) //nolint: gocritic
assert.Cmp(final.String(), strings.ReplaceAll(expected, "%CR", "\r"))
}
}
}
Expand Down Expand Up @@ -239,7 +239,7 @@ Content-Disposition: form-data; name="io"%CR
if !assert.CmpNoError(err) {
continue
}
if !assert.Cmp(final.String(), strings.Replace(expected, "%CR", "\r", -1)) { //nolint: gocritic
if !assert.Cmp(final.String(), strings.ReplaceAll(expected, "%CR", "\r")) {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions helpers/tdhttp/test_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestNewTestAPI(t *testing.T) {
}).
CmpStatus(200).
CmpHeader(containsKey).
CmpBody(strings.Replace( //nolint: gocritic
CmpBody(strings.ReplaceAll(
`POST!
---
--BoUnDaRy%CR
Expand All @@ -269,7 +269,7 @@ Content-Type: text/plain; charset=utf-8%CR
bingo%CR
--BoUnDaRy--%CR
`,
"%CR", "\r", -1)).
"%CR", "\r")).
Failed())
td.CmpEmpty(t, mockT.LogBuf())

Expand Down
2 changes: 1 addition & 1 deletion internal/json/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (m *marshaler) marshal(v any) error {
}
repl := bytes.Repeat([]byte(" "), 1+m.indent)
repl[0] = '\n'
m.buf.Write(bytes.Replace(b, []byte("\n"), repl, -1)) //nolint: gocritic
m.buf.Write(bytes.ReplaceAll(b, []byte("\n"), repl))

default:
return fmt.Errorf("Cannot marshal %T", vt)
Expand Down
4 changes: 2 additions & 2 deletions internal/json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ func TestJSON(t *testing.T) {
} {
for j, s := range []string{
js,
strings.Replace(js, "\n", "\r", -1), //nolint: gocritic
strings.Replace(js, "\n", "\r\n", -1), //nolint: gocritic
strings.ReplaceAll(js, "\n", "\r"),
strings.ReplaceAll(js, "\n", "\r\n"),
} {
got, err := json.Parse([]byte(s))
if !test.NoError(t, err, "#%d/%d, json.Parse succeeds", i, j) {
Expand Down
2 changes: 1 addition & 1 deletion internal/util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func ToString(val any) string {
// IndentString indents str lines (from 2nd one = 1st line is not
// indented) by indent.
func IndentString(str, indent string) string {
return strings.Replace(str, "\n", "\n"+indent, -1) //nolint: gocritic
return strings.ReplaceAll(str, "\n", "\n"+indent)
}

// IndentStringIn indents str lines (from 2nd one = 1st line is not
Expand Down
38 changes: 24 additions & 14 deletions td/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ func mustContain(str string) expectedErrorMatch {
}

func indent(str string, numSpc int) string {
return strings.Replace(str, "\n", "\n\t"+strings.Repeat(" ", numSpc), -1) //nolint: gocritic
return strings.ReplaceAll(str, "\n", "\n\t"+strings.Repeat(" ", numSpc))
}

func cmpErrorStr(t *testing.T, err *ctxerr.Error,
got string, expected expectedErrorMatch, fieldName string,
args ...any) bool {
args ...any,
) bool {
t.Helper()

if expected.Exact != "" && got != expected.Exact {
Expand All @@ -99,7 +100,7 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
> %s`,
tdutil.BuildTestName(args...),
fieldName, indent(got, 10), indent(expected.Exact, 10),
strings.Replace(err.Error(), "\n\t", "\n\t> ", -1)) //nolint: gocritic
strings.ReplaceAll(err.Error(), "\n\t", "\n\t> "))
return false
}

Expand All @@ -112,7 +113,7 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
tdutil.BuildTestName(args...),
fieldName,
indent(got, 16), indent(expected.Contain, 16),
strings.Replace(err.Error(), "\n\t", "\n\t> ", -1)) //nolint: gocritic
strings.ReplaceAll(err.Error(), "\n\t", "\n\t> "))
return false
}

Expand All @@ -125,15 +126,16 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
tdutil.BuildTestName(args...),
fieldName,
indent(got, 14), indent(expected.Match.String(), 14),
strings.Replace(err.Error(), "\n\t", "\n\t> ", -1)) //nolint: gocritic
strings.ReplaceAll(err.Error(), "\n\t", "\n\t> "))
return false
}

return true
}

func matchError(t *testing.T, err *ctxerr.Error, expectedError expectedError,
expectedIsTestDeep bool, args ...any) bool {
expectedIsTestDeep bool, args ...any,
) bool {
t.Helper()

if !cmpErrorStr(t, err, err.Message, expectedError.Message,
Expand Down Expand Up @@ -201,7 +203,8 @@ func matchError(t *testing.T, err *ctxerr.Error, expectedError expectedError,
}

func _checkError(t *testing.T, got, expected any,
expectedError expectedError, args ...any) bool {
expectedError expectedError, args ...any,
) bool {
t.Helper()

err := td.EqDeeplyError(got, expected)
Expand Down Expand Up @@ -247,7 +250,8 @@ func ifaceExpectedError(t *testing.T, expectedError expectedError) expectedError
// checkError calls _checkError twice. The first time with the same
// parameters, the second time in an any context.
func checkError(t *testing.T, got, expected any,
expectedError expectedError, args ...any) bool {
expectedError expectedError, args ...any,
) bool {
t.Helper()

if ok := _checkError(t, got, expected, expectedError, args...); !ok {
Expand All @@ -270,7 +274,8 @@ func checkError(t *testing.T, got, expected any,

func checkErrorForEach(t *testing.T,
gotList []any, expected any,
expectedError expectedError, args ...any) (ret bool) {
expectedError expectedError, args ...any,
) (ret bool) {
t.Helper()

globalTestName := tdutil.BuildTestName(args...)
Expand Down Expand Up @@ -314,7 +319,8 @@ func customCheckOK(t *testing.T,
}

func _checkOK(t *testing.T, got, expected any,
args ...any) bool {
args ...any,
) bool {
t.Helper()

if !td.Cmp(t, got, expected, args...) {
Expand All @@ -340,13 +346,15 @@ func _checkOK(t *testing.T, got, expected any,
// checkOK calls _checkOK twice. The first time with the same
// parameters, the second time in an any context.
func checkOK(t *testing.T, got, expected any,
args ...any) bool {
args ...any,
) bool {
t.Helper()
return customCheckOK(t, _checkOK, got, expected, args...)
}

func checkOKOrPanicIfUnsafeDisabled(t *testing.T, got, expected any,
args ...any) bool {
args ...any,
) bool {
t.Helper()

var ret bool
Expand All @@ -366,7 +374,8 @@ func checkOKOrPanicIfUnsafeDisabled(t *testing.T, got, expected any,
}

func checkOKForEach(t *testing.T, gotList []any, expected any,
args ...any) (ret bool) {
args ...any,
) (ret bool) {
t.Helper()

globalTestName := tdutil.BuildTestName(args...)
Expand All @@ -383,7 +392,8 @@ func checkOKForEach(t *testing.T, gotList []any, expected any,
}

func equalTypes(t *testing.T, got td.TestDeep, expected any,
args ...any) bool {
args ...any,
) bool {
gotType := got.TypeBehind()

expectedType, ok := expected.(reflect.Type)
Expand Down

0 comments on commit e1156b8

Please sign in to comment.