Skip to content

Commit

Permalink
encapsulate the GomegaExpression fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nunnatsa committed Oct 29, 2024
1 parent 0612576 commit 96291f1
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 89 deletions.
2 changes: 1 addition & 1 deletion internal/expression/actual/actual.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package actual

import (
"github.com/nunnatsa/ginkgolinter/internal/gomegainfo"
"go/ast"
gotypes "go/types"

"golang.org/x/tools/go/analysis"

"github.com/nunnatsa/ginkgolinter/internal/gomegahandler"
"github.com/nunnatsa/ginkgolinter/internal/gomegainfo"
)

type Actual struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/expression/actual/actualarg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package actual

import (
"github.com/nunnatsa/ginkgolinter/internal/gomegainfo"
"go/ast"
"go/token"
gotypes "go/types"
Expand All @@ -10,6 +9,7 @@ import (
"golang.org/x/tools/go/analysis"

"github.com/nunnatsa/ginkgolinter/internal/expression/value"
"github.com/nunnatsa/ginkgolinter/internal/gomegainfo"
"github.com/nunnatsa/ginkgolinter/internal/interfaces"
"github.com/nunnatsa/ginkgolinter/internal/reverseassertion"
)
Expand Down
102 changes: 78 additions & 24 deletions internal/expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package expression

import (
"fmt"
"github.com/nunnatsa/ginkgolinter/internal/gomegainfo"
"github.com/nunnatsa/ginkgolinter/internal/formatter"
"go/ast"
"go/token"
gotypes "go/types"

"github.com/go-toolsmith/astcopy"
"golang.org/x/tools/go/analysis"
Expand All @@ -13,21 +14,22 @@ import (
"github.com/nunnatsa/ginkgolinter/internal/expression/matcher"
"github.com/nunnatsa/ginkgolinter/internal/expression/value"
"github.com/nunnatsa/ginkgolinter/internal/gomegahandler"
"github.com/nunnatsa/ginkgolinter/internal/gomegainfo"
"github.com/nunnatsa/ginkgolinter/internal/reverseassertion"
)

type GomegaExpression struct {
Orig *ast.CallExpr
Clone *ast.CallExpr
orig *ast.CallExpr
clone *ast.CallExpr

assertionFuncName string
origAssertionFuncName string
actualFuncName string

isAsync bool

Actual *actual.Actual
Matcher *matcher.Matcher
actual *actual.Actual
matcher *matcher.Matcher

handler gomegahandler.Handler
}
Expand All @@ -41,7 +43,7 @@ func New(origExpr *ast.CallExpr, pass *analysis.Pass, handler gomegahandler.Hand
origSel, ok := origExpr.Fun.(*ast.SelectorExpr)
if !ok || !gomegainfo.IsAssertionFunc(origSel.Sel.Name) {
return &GomegaExpression{
Orig: origExpr,
orig: origExpr,
actualFuncName: actualMethodName,
}, true
}
Expand Down Expand Up @@ -79,17 +81,17 @@ func New(origExpr *ast.CallExpr, pass *analysis.Pass, handler gomegahandler.Hand
exprClone.Args[0] = mtchr.Clone

gexp := &GomegaExpression{
Orig: origExpr,
Clone: exprClone,
orig: origExpr,
clone: exprClone,

assertionFuncName: origSel.Sel.Name,
origAssertionFuncName: origSel.Sel.Name,
actualFuncName: actualMethodName,

isAsync: actl.IsAsync(),

Actual: actl,
Matcher: mtchr,
actual: actl,
matcher: mtchr,

handler: handler,
}
Expand All @@ -102,7 +104,7 @@ func New(origExpr *ast.CallExpr, pass *analysis.Pass, handler gomegahandler.Hand
}

func (e *GomegaExpression) IsMissingAssertion() bool {
return e.Matcher == nil
return e.matcher == nil
}

func (e *GomegaExpression) GetActualFuncName() string {
Expand Down Expand Up @@ -131,38 +133,38 @@ func (e *GomegaExpression) IsAsync() bool {
}

func (e *GomegaExpression) ReverseAssertionFuncLogic() {
assertionFunc := e.Clone.Fun.(*ast.SelectorExpr).Sel
assertionFunc := e.clone.Fun.(*ast.SelectorExpr).Sel
newName := reverseassertion.ChangeAssertionLogic(assertionFunc.Name)
assertionFunc.Name = newName
e.assertionFuncName = newName
}

func (e *GomegaExpression) ReplaceAssertionMethod(name string) {
e.Clone.Fun.(*ast.SelectorExpr).Sel.Name = name
e.clone.Fun.(*ast.SelectorExpr).Sel.Name = name
}

func (e *GomegaExpression) ReplaceMatcherFuncName(name string) {
e.Matcher.ReplaceMatcherFuncName(name)
e.matcher.ReplaceMatcherFuncName(name)
}

func (e *GomegaExpression) ReplaceMatcherArgs(newArgs []ast.Expr) {
e.Matcher.ReplaceMatcherArgs(newArgs)
e.matcher.ReplaceMatcherArgs(newArgs)
}

func (e *GomegaExpression) RemoveMatcherArgs() {
e.Matcher.ReplaceMatcherArgs(nil)
e.matcher.ReplaceMatcherArgs(nil)
}

func (e *GomegaExpression) ReplaceActual(newArg ast.Expr) {
e.Actual.ReplaceActual(newArg)
e.actual.ReplaceActual(newArg)
}

func (e *GomegaExpression) ReplaceActualWithItsFirstArg() {
e.Actual.ReplaceActualWithItsFirstArg()
e.actual.ReplaceActualWithItsFirstArg()
}

func (e *GomegaExpression) replaceMathcerFuncNoArgs(name string) {
e.Matcher.ReplaceMatcherFuncName(name)
e.matcher.ReplaceMatcherFuncName(name)
e.RemoveMatcherArgs()
}

Expand All @@ -175,7 +177,7 @@ func (e *GomegaExpression) SetMatcherBeEmpty() {
}

func (e *GomegaExpression) SetLenNumericMatcher() {
if m, ok := e.Matcher.GetMatcherInfo().(value.Valuer); ok && m.IsValueZero() {
if m, ok := e.matcher.GetMatcherInfo().(value.Valuer); ok && m.IsValueZero() {
e.SetMatcherBeEmpty()
} else {
e.ReplaceMatcherFuncName("HaveLen")
Expand All @@ -184,7 +186,7 @@ func (e *GomegaExpression) SetLenNumericMatcher() {
}

func (e *GomegaExpression) SetLenNumericActual() {
if m, ok := e.Matcher.GetMatcherInfo().(value.Valuer); ok && m.IsValueZero() {
if m, ok := e.matcher.GetMatcherInfo().(value.Valuer); ok && m.IsValueZero() {
e.SetMatcherBeEmpty()
} else {
e.ReplaceMatcherFuncName("HaveLen")
Expand Down Expand Up @@ -228,9 +230,9 @@ func (e *GomegaExpression) SetMatcherBeFalse() {
}

func (e *GomegaExpression) SetMatcherHaveValue() {
newMatcherExp := e.handler.GetNewWrapperMatcher("HaveValue", e.Matcher.Clone)
e.Clone.Args[0] = newMatcherExp
e.Matcher.Clone = newMatcherExp
newMatcherExp := e.handler.GetNewWrapperMatcher("HaveValue", e.matcher.Clone)
e.clone.Args[0] = newMatcherExp
e.matcher.Clone = newMatcherExp
}

func (e *GomegaExpression) SetMatcherEqual(arg ast.Expr) {
Expand All @@ -254,3 +256,55 @@ func (e *GomegaExpression) SetMatcherBeNumerically(op token.Token, arg ast.Expr)
func (e *GomegaExpression) IsNegativeAssertion() bool {
return reverseassertion.IsNegativeLogic(e.assertionFuncName)
}

func (e *GomegaExpression) GetClone() *ast.CallExpr {
return e.clone
}

// Actual proxies:

func (e *GomegaExpression) GetActualClone() *ast.CallExpr {
return e.actual.Clone
}

func (e *GomegaExpression) AppendWithArgsToActual() {
e.actual.AppendWithArgsMethod()
}

func (e *GomegaExpression) GetAsyncActualArg() *actual.AsyncArg {
return e.actual.GetAsyncArg()
}

func (e *GomegaExpression) GetActualArg() actual.ArgPayload {
return e.actual.Arg
}

func (e *GomegaExpression) GetActualArgExpr() ast.Expr {
return e.actual.GetActualArg()
}

func (e *GomegaExpression) GetActualArgGOType() gotypes.Type {
return e.actual.ArgGOType()
}

func (e *GomegaExpression) ActualArgTypeIs(other actual.ArgType) bool {
return e.actual.Arg.ArgType().Is(other)
}

// Matcher proxies

func (e *GomegaExpression) GetMatcher() *matcher.Matcher {
return e.matcher
}

func (e *GomegaExpression) GetMatcherInfo() matcher.Info {
return e.matcher.GetMatcherInfo()
}

func (e *GomegaExpression) MatcherTypeIs(other matcher.Type) bool {
return e.matcher.GetMatcherInfo().Type().Is(other)
}

func (e *GomegaExpression) FormatOrig(frm *formatter.GoFmtFormatter) string {
return frm.Format(e.orig)
}
4 changes: 2 additions & 2 deletions internal/rules/asyncfunccallrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (r AsyncFuncCallRule) isApplied(gexp *expression.GomegaExpression, config t
return false
}

if asyncArg := gexp.Actual.GetAsyncArg(); asyncRules != nil {
if asyncArg := gexp.GetAsyncActualArg(); asyncRules != nil {
return !asyncArg.IsValid()
}

Expand All @@ -33,7 +33,7 @@ func (r AsyncFuncCallRule) isApplied(gexp *expression.GomegaExpression, config t
func (r AsyncFuncCallRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool {
if r.isApplied(gexp, config) {

gexp.Actual.AppendWithArgsMethod()
gexp.AppendWithArgsToActual()

reportBuilder.AddIssue(true, valueInEventually, gexp.GetActualFuncName())
}
Expand Down
4 changes: 2 additions & 2 deletions internal/rules/asynctimeintervalsrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (r AsyncTimeIntervalsRule) isApplied(gexp *expression.GomegaExpression, con

func (r AsyncTimeIntervalsRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool {
if r.isApplied(gexp, config) {
asyncArg := gexp.Actual.GetAsyncArg()
asyncArg := gexp.GetAsyncActualArg()
if asyncArg.TooManyTimeouts() {
reportBuilder.AddIssue(false, multipleTimeouts)
}
Expand All @@ -51,7 +51,7 @@ func checkInterval(gexp *expression.GomegaExpression, durVal intervals.DurationV
case *intervals.RealDurationValue, *intervals.UnknownDurationTypeValue:

case *intervals.NumericDurationValue:
if checkNumericInterval(gexp.Actual.Clone, to) {
if checkNumericInterval(gexp.GetActualClone(), to) {
reportBuilder.AddIssue(true, onlyUseTimeDurationForInterval)
}

Expand Down
24 changes: 12 additions & 12 deletions internal/rules/caprule.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,39 @@ func (r *CapRule) isApplied(gexp *expression.GomegaExpression, config types.Conf
return false
}

matcherType := gexp.Matcher.GetMatcherInfo().Type()
if gexp.Actual.Arg.ArgType().Is(actual.CapFuncActualArgType) {
if matcherType.Is(matcher.EqualMatcherType | matcher.BeZeroMatcherType) {
//matcherType := gexp.matcher.GetMatcherInfo().Type()
if gexp.ActualArgTypeIs(actual.CapFuncActualArgType) {
if gexp.MatcherTypeIs(matcher.EqualMatcherType | matcher.BeZeroMatcherType) {
return true
}

if matcherType.Is(matcher.BeNumericallyMatcherType) {
mtchr := gexp.Matcher.GetMatcherInfo().(*matcher.BeNumericallyMatcher)
return mtchr.GetOp() == token.EQL || mtchr.GetOp() == token.NEQ || matcherType.Is(matcher.EqualZero|matcher.GreaterThanZero)
if gexp.MatcherTypeIs(matcher.BeNumericallyMatcherType) {
mtchr := gexp.GetMatcherInfo().(*matcher.BeNumericallyMatcher)
return mtchr.GetOp() == token.EQL || mtchr.GetOp() == token.NEQ || gexp.MatcherTypeIs(matcher.EqualZero|matcher.GreaterThanZero)
}
}

if gexp.Actual.Arg.ArgType().Is(actual.CapComparisonActualArgType) && matcherType.Is(matcher.BeTrueMatcherType|matcher.BeFalseMatcherType|matcher.EqualBoolValueMatcherType) {
if gexp.ActualArgTypeIs(actual.CapComparisonActualArgType) && gexp.MatcherTypeIs(matcher.BeTrueMatcherType|matcher.BeFalseMatcherType|matcher.EqualBoolValueMatcherType) {
return true
}

return false
}

func (r *CapRule) fixExpression(gexp *expression.GomegaExpression) bool {
if gexp.Actual.Arg.ArgType().Is(actual.CapFuncActualArgType) {
if gexp.ActualArgTypeIs(actual.CapFuncActualArgType) {
return r.fixEqual(gexp)
}

if gexp.Actual.Arg.ArgType().Is(actual.CapComparisonActualArgType) {
if gexp.ActualArgTypeIs(actual.CapComparisonActualArgType) {
return r.fixComparison(gexp)
}

return false
}

func (r *CapRule) fixEqual(gexp *expression.GomegaExpression) bool {
matcherInfo := gexp.Matcher.GetMatcherInfo()
matcherInfo := gexp.GetMatcherInfo()
switch mtchr := matcherInfo.(type) {
case *matcher.EqualMatcher:
gexp.SetMatcherCap(mtchr.GetValueExpr())
Expand All @@ -89,7 +89,7 @@ func (r *CapRule) fixEqual(gexp *expression.GomegaExpression) bool {
}

func (r *CapRule) fixComparison(gexp *expression.GomegaExpression) bool {
actl := gexp.Actual.Arg.(*actual.FuncComparisonPayload)
actl := gexp.GetActualArg().(*actual.FuncComparisonPayload)
if op := actl.GetOp(); op == token.NEQ {
gexp.ReverseAssertionFuncLogic()
} else if op != token.EQL {
Expand All @@ -99,7 +99,7 @@ func (r *CapRule) fixComparison(gexp *expression.GomegaExpression) bool {
gexp.SetMatcherCap(actl.GetValueExpr())
gexp.ReplaceActual(actl.GetFuncArg())

if gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BoolValueFalse) {
if gexp.MatcherTypeIs(matcher.BoolValueFalse) {
gexp.ReverseAssertionFuncLogic()
}

Expand Down
4 changes: 2 additions & 2 deletions internal/rules/comparepointerrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const comparePointerToValue = "comparing a pointer to a value will always fail"
type ComparePointRule struct{}

func (r ComparePointRule) isApplied(gexp *expression.GomegaExpression) bool {
actl, ok := gexp.Actual.Arg.(*actual.RegularArgPayload)
actl, ok := gexp.GetActualArg().(*actual.RegularArgPayload)
if !ok {
return false
}
Expand All @@ -26,7 +26,7 @@ func (r ComparePointRule) Apply(gexp *expression.GomegaExpression, config types.
return false
}

switch mtchr := gexp.Matcher.GetMatcherInfo().(type) {
switch mtchr := gexp.GetMatcherInfo().(type) {
case *matcher.EqualMatcher:
if mtchr.IsPointer() || mtchr.IsInterface() {
return false
Expand Down
6 changes: 3 additions & 3 deletions internal/rules/comparisonrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ func (r ComparisonRule) isApplied(gexp *expression.GomegaExpression, config type
return false
}

return gexp.Actual.Arg.ArgType().Is(actual.ComparisonActualArgType)
return gexp.ActualArgTypeIs(actual.ComparisonActualArgType)
}

func (r ComparisonRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool {
if !r.isApplied(gexp, config) {
return false
}

actl, ok := gexp.Actual.Arg.(actual.ComparisonActualPayload)
actl, ok := gexp.GetActualArg().(actual.ComparisonActualPayload)
if !ok {
return false
}
Expand All @@ -50,7 +50,7 @@ func (r ComparisonRule) Apply(gexp *expression.GomegaExpression, config types.Co
return false
}

if gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BoolValueFalse) {
if gexp.MatcherTypeIs(matcher.BoolValueFalse) {
gexp.ReverseAssertionFuncLogic()
}

Expand Down
Loading

0 comments on commit 96291f1

Please sign in to comment.