Skip to content

Commit cff2e4e

Browse files
committed
cosmetis
1 parent 0070f74 commit cff2e4e

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Remember that [assert.TestingT](https://pkg.go.dev/github.com/stretchr/testify/a
9999
[require.TestingT](https://pkg.go.dev/github.com/stretchr/testify/require#TestingT) are different interfaces,
100100
which may be important in some contexts.
101101

102+
Also, pay attention to `internal/checkers/helpers_*.go` files. Try to reuse existing code as much as possible.
103+
102104
### 8) Improve tests from p.4 if necessary
103105

104106
Pay attention to `Assertion` and `NewAssertionExpander`, but keep your tests as small as possible.

internal/checkers/helpers_context.go

+27
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,30 @@ func findNearestNodeWithIdx[T ast.Node](stack []ast.Node) (v T, index int) {
9797
}
9898
return
9999
}
100+
101+
func fnContainsAssertions(pass *analysis.Pass, fn *ast.FuncDecl) bool {
102+
if fn.Body == nil {
103+
return false
104+
}
105+
106+
for _, s := range fn.Body.List {
107+
if isAssertionStmt(pass, s) {
108+
return true
109+
}
110+
}
111+
return false
112+
}
113+
114+
func isAssertionStmt(pass *analysis.Pass, stmt ast.Stmt) bool {
115+
expr, ok := stmt.(*ast.ExprStmt)
116+
if !ok {
117+
return false
118+
}
119+
120+
ce, ok := expr.X.(*ast.CallExpr)
121+
if !ok {
122+
return false
123+
}
124+
125+
return NewCallMeta(pass, ce) != nil
126+
}

internal/checkers/helpers_naming.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import (
55
"regexp"
66
)
77

8-
func isIdentNamedAsExpected(pattern *regexp.Regexp, e ast.Expr) bool {
9-
id, ok := e.(*ast.Ident)
10-
return ok && pattern.MatchString(id.Name)
11-
}
12-
138
func isStructVarNamedAsExpected(pattern *regexp.Regexp, e ast.Expr) bool {
149
s, ok := e.(*ast.SelectorExpr)
1510
return ok && isIdentNamedAsExpected(pattern, s.X)
@@ -19,3 +14,8 @@ func isStructFieldNamedAsExpected(pattern *regexp.Regexp, e ast.Expr) bool {
1914
s, ok := e.(*ast.SelectorExpr)
2015
return ok && isIdentNamedAsExpected(pattern, s.Sel)
2116
}
17+
18+
func isIdentNamedAsExpected(pattern *regexp.Regexp, e ast.Expr) bool {
19+
id, ok := e.(*ast.Ident)
20+
return ok && pattern.MatchString(id.Name)
21+
}

internal/checkers/suite_thelper.go

+1-28
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (checker SuiteTHelper) Check(pass *analysis.Pass, inspector *inspector.Insp
3333
return
3434
}
3535

36-
if !containsAssertions(pass, fd) {
36+
if !fnContainsAssertions(pass, fd) {
3737
return
3838
}
3939

@@ -65,30 +65,3 @@ func (checker SuiteTHelper) Check(pass *analysis.Pass, inspector *inspector.Insp
6565
})
6666
return diagnostics
6767
}
68-
69-
func containsAssertions(pass *analysis.Pass, fn *ast.FuncDecl) bool {
70-
if fn.Body == nil {
71-
return false
72-
}
73-
74-
for _, s := range fn.Body.List {
75-
if isAssertionStmt(pass, s) {
76-
return true
77-
}
78-
}
79-
return false
80-
}
81-
82-
func isAssertionStmt(pass *analysis.Pass, stmt ast.Stmt) bool {
83-
expr, ok := stmt.(*ast.ExprStmt)
84-
if !ok {
85-
return false
86-
}
87-
88-
ce, ok := expr.X.(*ast.CallExpr)
89-
if !ok {
90-
return false
91-
}
92-
93-
return NewCallMeta(pass, ce) != nil
94-
}

0 commit comments

Comments
 (0)