diff --git a/analyzer/testdata/src/checkers-default/len/len_test.go b/analyzer/testdata/src/checkers-default/len/len_test.go index 4a42a85..5cb7723 100644 --- a/analyzer/testdata/src/checkers-default/len/len_test.go +++ b/analyzer/testdata/src/checkers-default/len/len_test.go @@ -3,6 +3,7 @@ package len import ( + "encoding/json" "testing" "github.com/stretchr/testify/assert" @@ -13,49 +14,60 @@ const constNum = 10 func TestLenChecker(t *testing.T) { var arr, expArr [3]int var value int + var resp string // Invalid. { - assert.Equal(t, len(arr), 42) // want "len: use assert\\.Len" - assert.Equalf(t, len(arr), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Equal(t, 42, len(arr)) // want "len: use assert\\.Len" - assert.Equalf(t, 42, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Equal(t, value, len(arr)) // want "len: use assert\\.Len" - assert.Equalf(t, value, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Equal(t, len(expArr), len(arr)) // want "len: use assert\\.Len" - assert.Equalf(t, len(expArr), len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.EqualValues(t, len(arr), 42) // want "len: use assert\\.Len" - assert.EqualValuesf(t, len(arr), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.EqualValues(t, 42, len(arr)) // want "len: use assert\\.Len" - assert.EqualValuesf(t, 42, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.EqualValues(t, value, len(arr)) // want "len: use assert\\.Len" - assert.EqualValuesf(t, value, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.EqualValues(t, len(expArr), len(arr)) // want "len: use assert\\.Len" - assert.EqualValuesf(t, len(expArr), len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Exactly(t, len(arr), 42) // want "len: use assert\\.Len" - assert.Exactlyf(t, len(arr), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Exactly(t, 42, len(arr)) // want "len: use assert\\.Len" - assert.Exactlyf(t, 42, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Exactly(t, value, len(arr)) // want "len: use assert\\.Len" - assert.Exactlyf(t, value, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Exactly(t, len(expArr), len(arr)) // want "len: use assert\\.Len" - assert.Exactlyf(t, len(expArr), len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.True(t, len(arr) == 42) // want "len: use assert\\.Len" - assert.Truef(t, len(arr) == 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.True(t, 42 == len(arr)) // want "len: use assert\\.Len" - assert.Truef(t, 42 == len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.True(t, len(arr) == value) // want "len: use assert\\.Len" - assert.Truef(t, len(arr) == value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.True(t, len(arr) == len(expArr)) // want "len: use assert\\.Len" - assert.Truef(t, len(arr) == len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Equal(t, constNum, len(arr)) // want "len: use assert\\.Len" - assert.Equalf(t, constNum, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.EqualValues(t, constNum, len(arr)) // want "len: use assert\\.Len" - assert.EqualValuesf(t, constNum, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Exactly(t, constNum, len(arr)) // want "len: use assert\\.Len" - assert.Exactlyf(t, constNum, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.True(t, len(arr) == constNum) // want "len: use assert\\.Len" - assert.Truef(t, len(arr) == constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, len(arr), 42) // want "len: use assert\\.Len" + assert.Equalf(t, len(arr), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, 42, len(arr)) // want "len: use assert\\.Len" + assert.Equalf(t, 42, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, value, len(arr)) // want "len: use assert\\.Len" + assert.Equalf(t, value, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, len(expArr), len(arr)) // want "len: use assert\\.Len" + assert.Equalf(t, len(expArr), len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.EqualValues(t, len(arr), 42) // want "len: use assert\\.Len" + assert.EqualValuesf(t, len(arr), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.EqualValues(t, 42, len(arr)) // want "len: use assert\\.Len" + assert.EqualValuesf(t, 42, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.EqualValues(t, value, len(arr)) // want "len: use assert\\.Len" + assert.EqualValuesf(t, value, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.EqualValues(t, len(expArr), len(arr)) // want "len: use assert\\.Len" + assert.EqualValuesf(t, len(expArr), len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Exactly(t, len(arr), 42) // want "len: use assert\\.Len" + assert.Exactlyf(t, len(arr), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Exactly(t, 42, len(arr)) // want "len: use assert\\.Len" + assert.Exactlyf(t, 42, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Exactly(t, value, len(arr)) // want "len: use assert\\.Len" + assert.Exactlyf(t, value, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Exactly(t, len(expArr), len(arr)) // want "len: use assert\\.Len" + assert.Exactlyf(t, len(expArr), len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.True(t, len(arr) == 42) // want "len: use assert\\.Len" + assert.Truef(t, len(arr) == 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.True(t, 42 == len(arr)) // want "len: use assert\\.Len" + assert.Truef(t, 42 == len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.True(t, len(arr) == value) // want "len: use assert\\.Len" + assert.Truef(t, len(arr) == value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.True(t, len(arr) == len(expArr)) // want "len: use assert\\.Len" + assert.Truef(t, len(arr) == len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, constNum, len(arr)) // want "len: use assert\\.Len" + assert.Equalf(t, constNum, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.EqualValues(t, constNum, len(arr)) // want "len: use assert\\.Len" + assert.EqualValuesf(t, constNum, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Exactly(t, constNum, len(arr)) // want "len: use assert\\.Len" + assert.Exactlyf(t, constNum, len(arr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.True(t, len(arr) == constNum) // want "len: use assert\\.Len" + assert.Truef(t, len(arr) == constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, 42, len(string(resp))) // want "len: use assert\\.Len" + assert.Equalf(t, 42, len(string(resp)), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, 42, len([]byte(resp))) // want "len: use assert\\.Len" + assert.Equalf(t, 42, len([]byte(resp)), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, 42, len(json.RawMessage(resp))) // want "len: use assert\\.Len" + assert.Equalf(t, 42, len(json.RawMessage(resp)), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Equal(t, 42, len(string([]byte(json.RawMessage(resp))))) // want "len: use assert\\.Len" + assert.Equalf(t, 42, len(string([]byte(json.RawMessage(resp)))), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.True(t, len(string(resp)) == 42) // want "len: use assert\\.Len" + assert.Truef(t, len(string(resp)) == 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" } // Valid. diff --git a/analyzer/testdata/src/checkers-default/len/len_test.go.golden b/analyzer/testdata/src/checkers-default/len/len_test.go.golden index 111395c..efa7898 100644 --- a/analyzer/testdata/src/checkers-default/len/len_test.go.golden +++ b/analyzer/testdata/src/checkers-default/len/len_test.go.golden @@ -3,6 +3,7 @@ package len import ( + "encoding/json" "testing" "github.com/stretchr/testify/assert" @@ -13,49 +14,60 @@ const constNum = 10 func TestLenChecker(t *testing.T) { var arr, expArr [3]int var value int + var resp string // Invalid. { - assert.Len(t, arr, 42) // want "len: use assert\\.Len" - assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, 42) // want "len: use assert\\.Len" - assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, value) // want "len: use assert\\.Len" - assert.Lenf(t, arr, value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, len(expArr)) // want "len: use assert\\.Len" - assert.Lenf(t, arr, len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, 42) // want "len: use assert\\.Len" - assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, 42) // want "len: use assert\\.Len" - assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, value) // want "len: use assert\\.Len" - assert.Lenf(t, arr, value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, len(expArr)) // want "len: use assert\\.Len" - assert.Lenf(t, arr, len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, 42) // want "len: use assert\\.Len" - assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, 42) // want "len: use assert\\.Len" - assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, value) // want "len: use assert\\.Len" - assert.Lenf(t, arr, value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, len(expArr)) // want "len: use assert\\.Len" - assert.Lenf(t, arr, len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, 42) // want "len: use assert\\.Len" - assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, 42) // want "len: use assert\\.Len" - assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, value) // want "len: use assert\\.Len" - assert.Lenf(t, arr, value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, len(expArr)) // want "len: use assert\\.Len" - assert.Lenf(t, arr, len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, constNum) // want "len: use assert\\.Len" - assert.Lenf(t, arr, constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, constNum) // want "len: use assert\\.Len" - assert.Lenf(t, arr, constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, constNum) // want "len: use assert\\.Len" - assert.Lenf(t, arr, constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" - assert.Len(t, arr, constNum) // want "len: use assert\\.Len" - assert.Lenf(t, arr, constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, 42) // want "len: use assert\\.Len" + assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, 42) // want "len: use assert\\.Len" + assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, value) // want "len: use assert\\.Len" + assert.Lenf(t, arr, value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, len(expArr)) // want "len: use assert\\.Len" + assert.Lenf(t, arr, len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, 42) // want "len: use assert\\.Len" + assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, 42) // want "len: use assert\\.Len" + assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, value) // want "len: use assert\\.Len" + assert.Lenf(t, arr, value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, len(expArr)) // want "len: use assert\\.Len" + assert.Lenf(t, arr, len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, 42) // want "len: use assert\\.Len" + assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, 42) // want "len: use assert\\.Len" + assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, value) // want "len: use assert\\.Len" + assert.Lenf(t, arr, value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, len(expArr)) // want "len: use assert\\.Len" + assert.Lenf(t, arr, len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, 42) // want "len: use assert\\.Len" + assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, 42) // want "len: use assert\\.Len" + assert.Lenf(t, arr, 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, value) // want "len: use assert\\.Len" + assert.Lenf(t, arr, value, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, len(expArr)) // want "len: use assert\\.Len" + assert.Lenf(t, arr, len(expArr), "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, constNum) // want "len: use assert\\.Len" + assert.Lenf(t, arr, constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, constNum) // want "len: use assert\\.Len" + assert.Lenf(t, arr, constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, constNum) // want "len: use assert\\.Len" + assert.Lenf(t, arr, constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, arr, constNum) // want "len: use assert\\.Len" + assert.Lenf(t, arr, constNum, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, string(resp), 42) // want "len: use assert\\.Len" + assert.Lenf(t, string(resp), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, []byte(resp), 42) // want "len: use assert\\.Len" + assert.Lenf(t, []byte(resp), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, json.RawMessage(resp), 42) // want "len: use assert\\.Len" + assert.Lenf(t, json.RawMessage(resp), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, string([]byte(json.RawMessage(resp))), 42) // want "len: use assert\\.Len" + assert.Lenf(t, string([]byte(json.RawMessage(resp))), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" + assert.Len(t, string(resp), 42) // want "len: use assert\\.Len" + assert.Lenf(t, string(resp), 42, "msg with args %d %s", 42, "42") // want "len: use assert\\.Lenf" } // Valid. diff --git a/internal/testgen/gen_len.go b/internal/testgen/gen_len.go index ade360a..446a247 100644 --- a/internal/testgen/gen_len.go +++ b/internal/testgen/gen_len.go @@ -51,11 +51,33 @@ func (g LenTestsGenerator) TemplateData() any { {Fn: "True", Argsf: "len(arr) == value", ReportMsgf: report, ProposedFn: proposedFn, ProposedArgsf: "arr, value"}, {Fn: "True", Argsf: "len(arr) == len(expArr)", ReportMsgf: report, ProposedFn: proposedFn, ProposedArgsf: "arr, len(expArr)"}, - // Constant case. + // Constant cases. {Fn: "Equal", Argsf: "constNum, len(arr)", ReportMsgf: report, ProposedFn: proposedFn, ProposedArgsf: "arr, constNum"}, {Fn: "EqualValues", Argsf: "constNum, len(arr)", ReportMsgf: report, ProposedFn: proposedFn, ProposedArgsf: "arr, constNum"}, {Fn: "Exactly", Argsf: "constNum, len(arr)", ReportMsgf: report, ProposedFn: proposedFn, ProposedArgsf: "arr, constNum"}, {Fn: "True", Argsf: "len(arr) == constNum", ReportMsgf: report, ProposedFn: proposedFn, ProposedArgsf: "arr, constNum"}, + + // Type conversions cases. + { + Fn: "Equal", Argsf: "42, len(string(resp))", ReportMsgf: report, + ProposedFn: proposedFn, ProposedArgsf: "string(resp), 42", + }, + { + Fn: "Equal", Argsf: "42, len([]byte(resp))", ReportMsgf: report, + ProposedFn: proposedFn, ProposedArgsf: "[]byte(resp), 42", + }, + { + Fn: "Equal", Argsf: "42, len(json.RawMessage(resp))", ReportMsgf: report, + ProposedFn: proposedFn, ProposedArgsf: "json.RawMessage(resp), 42", + }, + { + Fn: "Equal", Argsf: "42, len(string([]byte(json.RawMessage(resp))))", ReportMsgf: report, + ProposedFn: proposedFn, ProposedArgsf: "string([]byte(json.RawMessage(resp))), 42", + }, + { + Fn: "True", Argsf: "len(string(resp)) == 42", ReportMsgf: report, + ProposedFn: proposedFn, ProposedArgsf: "string(resp), 42", + }, }, ValidAssertions: []Assertion{ {Fn: "Len", Argsf: "arr, 42"}, @@ -125,6 +147,7 @@ const lenTestTmpl = header + ` package {{ .CheckerName.AsPkgName }} import ( + "encoding/json" "testing" "github.com/stretchr/testify/assert" @@ -135,6 +158,7 @@ const constNum = 10 func {{ .CheckerName.AsTestName }}(t *testing.T) { var arr, expArr [3]int var value int + var resp string // Invalid. {