1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00

test: fix testpackage lint issues for test and ifelse (#1582)

This commit is contained in:
Oleksandr Redko
2025-11-15 20:24:16 +02:00
committed by GitHub
parent d1dd6ee961
commit d81298b0ce
98 changed files with 152 additions and 137 deletions

View File

@@ -16,6 +16,7 @@ linters:
- musttag
- nolintlint
- revive
- testpackage
- thelper
- staticcheck
- unparam
@@ -30,6 +31,18 @@ linters:
- linters:
- godoclint
text: 'symbol should have a godoc \("Visit"\)'
- linters:
- testpackage
text: 'package should be `rule_test` instead of `rule`'
- linters:
- testpackage
text: 'package should be `lint_test` instead of `lint`'
- linters:
- testpackage
text: 'package should be `cli_test` instead of `cli`'
- linters:
- testpackage
text: 'package should be `formatter_test` instead of `formatter`'
settings:
gocritic:

View File

@@ -1,24 +1,26 @@
package ifelse
package ifelse_test
import (
"go/ast"
"go/token"
"testing"
"github.com/mgechev/revive/internal/ifelse"
)
func TestBlockBranch(t *testing.T) {
t.Run("empty", func(t *testing.T) {
block := &ast.BlockStmt{List: []ast.Stmt{}}
b := BlockBranch(block)
if b.BranchKind != Empty {
b := ifelse.BlockBranch(block)
if b.BranchKind != ifelse.Empty {
t.Errorf("want Empty branch, got %v", b.BranchKind)
}
})
t.Run("non empty", func(t *testing.T) {
stmt := &ast.ReturnStmt{}
block := &ast.BlockStmt{List: []ast.Stmt{stmt}}
b := BlockBranch(block)
if b.BranchKind != Return {
b := ifelse.BlockBranch(block)
if b.BranchKind != ifelse.Return {
t.Errorf("want Return branch, got %v", b.BranchKind)
}
})
@@ -28,33 +30,33 @@ func TestStmtBranch(t *testing.T) {
cases := []struct {
name string
stmt ast.Stmt
kind BranchKind
call *Call
kind ifelse.BranchKind
call *ifelse.Call
}{
{
name: "ReturnStmt",
stmt: &ast.ReturnStmt{},
kind: Return,
kind: ifelse.Return,
},
{
name: "BreakStmt",
stmt: &ast.BranchStmt{Tok: token.BREAK},
kind: Break,
kind: ifelse.Break,
},
{
name: "ContinueStmt",
stmt: &ast.BranchStmt{Tok: token.CONTINUE},
kind: Continue,
kind: ifelse.Continue,
},
{
name: "GotoStmt",
stmt: &ast.BranchStmt{Tok: token.GOTO},
kind: Goto,
kind: ifelse.Goto,
},
{
name: "EmptyStmt",
stmt: &ast.EmptyStmt{},
kind: Empty,
kind: ifelse.Empty,
},
{
name: "ExprStmt with DeviatingFunc (panic)",
@@ -63,8 +65,8 @@ func TestStmtBranch(t *testing.T) {
Fun: &ast.Ident{Name: "panic"},
},
},
kind: Panic,
call: &Call{Name: "panic"},
kind: ifelse.Panic,
call: &ifelse.Call{Name: "panic"},
},
{
name: "ExprStmt with DeviatingFunc (os.Exit)",
@@ -76,8 +78,8 @@ func TestStmtBranch(t *testing.T) {
},
},
},
kind: Exit,
call: &Call{Pkg: "os", Name: "Exit"},
kind: ifelse.Exit,
call: &ifelse.Call{Pkg: "os", Name: "Exit"},
},
{
name: "ExprStmt with non-deviating func",
@@ -86,7 +88,7 @@ func TestStmtBranch(t *testing.T) {
Fun: &ast.Ident{Name: "foo"},
},
},
kind: Regular,
kind: ifelse.Regular,
},
{
name: "LabeledStmt wrapping ReturnStmt",
@@ -94,7 +96,7 @@ func TestStmtBranch(t *testing.T) {
Label: &ast.Ident{Name: "lbl"},
Stmt: &ast.ReturnStmt{},
},
kind: Return,
kind: ifelse.Return,
},
{
name: "LabeledStmt wrapping ExprStmt",
@@ -102,21 +104,21 @@ func TestStmtBranch(t *testing.T) {
Label: &ast.Ident{Name: "lbl"},
Stmt: &ast.ExprStmt{X: &ast.CallExpr{Fun: &ast.Ident{Name: "foo"}}},
},
kind: Regular,
kind: ifelse.Regular,
},
{
name: "BlockStmt with ReturnStmt",
stmt: &ast.BlockStmt{List: []ast.Stmt{&ast.ReturnStmt{}}},
kind: Return,
kind: ifelse.Return,
},
{
name: "BlockStmt with ExprStmt",
stmt: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{X: &ast.CallExpr{Fun: &ast.Ident{Name: "foo"}}}}},
kind: Regular,
kind: ifelse.Regular,
},
}
for _, c := range cases {
b := StmtBranch(c.stmt)
b := ifelse.StmtBranch(c.stmt)
if b.BranchKind != c.kind {
t.Errorf("%s: want %v, got %v", c.name, c.kind, b.BranchKind)
}
@@ -131,37 +133,37 @@ func TestStmtBranch(t *testing.T) {
func TestBranch_String_LongString(t *testing.T) {
tests := []struct {
name string
branch Branch
branch ifelse.Branch
wantStr string
wantLong string
}{
{
name: "Return branch",
branch: Branch{BranchKind: Return},
branch: ifelse.Branch{BranchKind: ifelse.Return},
wantStr: "{ ... return }",
wantLong: "a return statement",
},
{
name: "Panic branch with Call",
branch: Branch{BranchKind: Panic, Call: Call{Name: "panic"}},
branch: ifelse.Branch{BranchKind: ifelse.Panic, Call: ifelse.Call{Name: "panic"}},
wantStr: "{ ... panic() }",
wantLong: "call to panic function",
},
{
name: "Exit branch with Call",
branch: Branch{BranchKind: Exit, Call: Call{Pkg: "os", Name: "Exit"}},
branch: ifelse.Branch{BranchKind: ifelse.Exit, Call: ifelse.Call{Pkg: "os", Name: "Exit"}},
wantStr: "{ ... os.Exit() }",
wantLong: "call to os.Exit function",
},
{
name: "Empty branch",
branch: Branch{BranchKind: Empty},
branch: ifelse.Branch{BranchKind: ifelse.Empty},
wantStr: "{ }",
wantLong: "an empty block",
},
{
name: "Regular branch",
branch: Branch{BranchKind: Regular},
branch: ifelse.Branch{BranchKind: ifelse.Regular},
wantStr: "{ ... }",
wantLong: "a regular statement",
},
@@ -179,27 +181,27 @@ func TestBranch_String_LongString(t *testing.T) {
func TestBranch_HasDecls(t *testing.T) {
tests := []struct {
name string
block []ast.Stmt
block *ast.BlockStmt
want bool
}{
{
name: "DeclStmt",
block: []ast.Stmt{&ast.DeclStmt{}},
block: &ast.BlockStmt{List: []ast.Stmt{&ast.DeclStmt{}}},
want: true,
},
{
name: "AssignStmt with :=",
block: []ast.Stmt{&ast.AssignStmt{Tok: token.DEFINE}},
block: &ast.BlockStmt{List: []ast.Stmt{&ast.AssignStmt{Tok: token.DEFINE}}},
want: true,
},
{
name: "ExprStmt",
block: []ast.Stmt{&ast.ExprStmt{}},
block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}}},
want: false,
},
}
for _, tt := range tests {
b := Branch{block: tt.block}
b := ifelse.BlockBranch(tt.block)
if got := b.HasDecls(); got != tt.want {
t.Errorf("%s: want HasDecls to be %v, got %v", tt.name, tt.want, got)
}
@@ -209,42 +211,42 @@ func TestBranch_HasDecls(t *testing.T) {
func TestBranch_IsShort(t *testing.T) {
tests := []struct {
name string
block []ast.Stmt
block *ast.BlockStmt
want bool
}{
{
name: "nil block",
block: nil,
block: &ast.BlockStmt{},
want: true,
},
{
name: "single ExprStmt",
block: []ast.Stmt{&ast.ExprStmt{}},
block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}}},
want: true,
},
{
name: "single BlockStmt",
block: []ast.Stmt{&ast.BlockStmt{}},
block: &ast.BlockStmt{List: []ast.Stmt{&ast.BlockStmt{}}},
want: false,
},
{
name: "two short statements",
block: []ast.Stmt{&ast.ExprStmt{}, &ast.ExprStmt{}},
block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}, &ast.ExprStmt{}}},
want: true,
},
{
name: "second non-short statement",
block: []ast.Stmt{&ast.ExprStmt{}, &ast.BlockStmt{}},
block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}, &ast.BlockStmt{}}},
want: false,
},
{
name: "three statements (should return false)",
block: []ast.Stmt{&ast.ExprStmt{}, &ast.ExprStmt{}, &ast.ExprStmt{}},
block: &ast.BlockStmt{List: []ast.Stmt{&ast.ExprStmt{}, &ast.ExprStmt{}, &ast.ExprStmt{}}},
want: false,
},
}
for _, tt := range tests {
b := Branch{block: tt.block}
b := ifelse.BlockBranch(tt.block)
if got := b.IsShort(); got != tt.want {
t.Errorf("%s: want IsShort to be %v, got %v", tt.name, tt.want, got)
}

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"flag"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"

View File

@@ -1,4 +1,4 @@
package test
package test_test
import (
"testing"