Skip to content

Commit

Permalink
Merge pull request #21 from dibrinsofor/master
Browse files Browse the repository at this point in the history
Refactored Unit Tests + Extended Testcases
  • Loading branch information
alexkohler authored Oct 19, 2022
2 parents b2c0146 + 5ace50e commit 157f0f1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion nakedret.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func checkNakedReturns(args []string, maxLength *uint, setExitStatus bool) error

files, err := parseInput(args, fset)
if err != nil {
return fmt.Errorf("could not parse input %v", err)
return fmt.Errorf("could not parse input: %v", err)
}

if maxLength == nil {
Expand Down
56 changes: 39 additions & 17 deletions nakedret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@ import (
"testing"
)

type testParams struct {
filename string
maxLength uint
}

var testcases = []struct {
name string
expected string
params testParams
}{
{"return in block", `testdata/ret-in-block.go:9: Dummy naked returns on 8 line function
`, testParams{
filename: "testdata/ret-in-block.go",
maxLength: 0,
}},
{"ignore short functions", ``, testParams{
filename: "testdata/ret-in-block.go",
maxLength: 10,
}},
{"nested function literals", `testdata/nested.go:16: Bad naked returns on 6 line function
testdata/nested.go:21: <func():20> naked returns on 2 line function
testdata/nested.go:28: <func():27> naked returns on 2 line function
testdata/nested.go:32: <func():31> naked returns on 2 line function
testdata/nested.go:36: <func():35> naked returns on 2 line function
testdata/nested.go:40: <func():39> naked returns on 2 line function
`, testParams{
filename: "testdata/nested.go",
maxLength: 0,
}},
}

func runNakedret(t *testing.T, filename string, maxLength uint, expected string) {
t.Helper()
defer func() {
Expand All @@ -21,25 +52,16 @@ func runNakedret(t *testing.T, filename string, maxLength uint, expected string)
if err := checkNakedReturns([]string{filename}, &maxLength, false); err != nil {
t.Fatal(err)
}
actual := string(logBuf.Bytes())
actual := logBuf.String()
if expected != actual {
t.Errorf("Unexpected output:\n-----\n%s\n-----\n", actual)
t.Errorf("Unexpected output:\n-----\ngot: \n%s\nexpected: \n%v\n-----\n", actual, expected)
}
}

func TestReturnInBlock(t *testing.T) {
expected := `testdata/ret-in-block.go:9: Dummy naked returns on 8 line function
`
runNakedret(t, "testdata/ret-in-block.go", 0, expected)
}

func TestIgnoreShortFunctions(t *testing.T) {
runNakedret(t, "testdata/ret-in-block.go", 10, "")
}

func TestNestedFunctionLiterals(t *testing.T) {
expected := `testdata/nested.go:16: Bad naked returns on 6 line function
testdata/nested.go:21: <func():20> naked returns on 2 line function
`
runNakedret(t, "testdata/nested.go", 0, expected)
func TestCheckNakedReturns(t *testing.T) {
for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
runNakedret(t, tt.params.filename, tt.params.maxLength, tt.expected)
})
}
}
18 changes: 18 additions & 0 deletions testdata/nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,21 @@ func BadNested() {
}
return
}

func MoreBad() {
var _ = func() (err error) {
return
}

func() (err error) {
return
}()

defer func() (err error) {
return
}()

go func() (err error) {
return
}()
}

0 comments on commit 157f0f1

Please sign in to comment.