Skip to content

Commit

Permalink
formatter: support non-string single arg (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonboom authored Nov 13, 2024
1 parent 8ca1d1a commit 639ce85
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 27 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func TestFormatterChecker_AllAssertions(t *testing.T) {
}

func TestFormatterChecker_Ignored(t *testing.T) {
assert.Equal(nil, nil, new(time.Time))
assert.ObjectsAreEqual(nil, nil)
assert.ObjectsAreEqualValues(nil, nil)
assert.ObjectsExportedFieldsAreEqual(nil, nil)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions analyzer/testdata/src/debug/formatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package debug

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFormatter(t *testing.T) {
assert.False(t, true)
assert.False(t, true, new(http.Response))
// assert.False(t, true, new(http.Response), 1, 2, 3) // panic
assert.False(t, true, "hello")
assert.False(t, true, "hello", 1, 2)
assert.False(t, true, "hello_%v_%d", 3, 4)
assert.Falsef(t, true, "world")
assert.Falsef(t, true, "world_%d_%v", 5, 6)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestFormatterChecker(t *testing.T) {
var args []any
assert.Errorf(t, err, "Parse(%v) should fail.", args) // want "formatter: use assert\\.Errorf$"
assert.Equal(t, 1, 2)
assert.Equal(t, 1, 2, new(time.Time))
assert.Equalf(t, 1, 2, "msg") // want "formatter: use assert\\.Equalf"
assert.Equalf(t, 1, 2, "msg with arg %d", 42) // want "formatter: use assert\\.Equalf"
assert.Equalf(t, 1, 2, "msg with args %d %s", 42, "42") // want "formatter: use assert\\.Equalf"
Expand Down
8 changes: 6 additions & 2 deletions internal/checkers/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,19 @@ func (checker Formatter) checkFmtAssertion(pass *analysis.Pass, call *CallMeta)

func isPrintfLikeCall(pass *analysis.Pass, call *CallMeta) (int, bool) {
msgAndArgsPos := getMsgAndArgsPosition(call.Fn.Signature)
if msgAndArgsPos < 0 {
if msgAndArgsPos <= 0 {
return -1, false
}

if !(len(call.ArgsRaw) > msgAndArgsPos && hasStringType(pass, call.ArgsRaw[msgAndArgsPos])) {
return -1, false
}

if !assertHasFormattedAnalogue(pass, call) {
return -1, false
}

return msgAndArgsPos, len(call.ArgsRaw) > msgAndArgsPos
return msgAndArgsPos, true
}

func assertHasFormattedAnalogue(pass *analysis.Pass, call *CallMeta) bool {
Expand Down
1 change: 1 addition & 0 deletions internal/testgen/gen_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (g FormatterTestsGenerator) TemplateData() any {
SprintfAssertions: sprintfAssertions,
AllAssertions: assertions,
IgnoredAssertions: []Assertion{
{Fn: "Equal", Argsf: "nil, nil, new(time.Time)"},
{Fn: "ObjectsAreEqual", Argsf: "nil, nil"},
{Fn: "ObjectsAreEqualValues", Argsf: "nil, nil"},
{Fn: "ObjectsExportedFieldsAreEqual", Argsf: "nil, nil"},
Expand Down
2 changes: 2 additions & 0 deletions internal/testgen/gen_formatter_not_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func (g FormatterNotDefaultsTestsGenerator) TemplateData() any {

baseAssertions := []Assertion{
{Fn: "Equal", Argsf: "1, 2"},
{Fn: "Equal", Argsf: "1, 2, new(time.Time)"},
// {Fn: "Equal", Argsf: "1, 2, new(time.Time), 2"}, // Panics.
{Fn: "Equal", Argsf: `1, 2, "msg"`, ReportMsgf: reportUse, ProposedFn: "Equalf"},
{Fn: "Equal", Argsf: `1, 2, "msg with arg %d", 42`, ReportMsgf: reportUse, ProposedFn: "Equalf"},
{Fn: "Equal", Argsf: `1, 2, "msg with args %d %s", 42, "42"`, ReportMsgf: reportUse, ProposedFn: "Equalf"},
Expand Down

0 comments on commit 639ce85

Please sign in to comment.