Skip to content

Commit bbcb9bf

Browse files
committed
split the huge the matherinfo.go file into smaller files
1 parent de271ea commit bbcb9bf

File tree

8 files changed

+669
-616
lines changed

8 files changed

+669
-616
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package matcher
2+
3+
import "github.com/nunnatsa/ginkgolinter/internal/expression/value"
4+
5+
type BeIdenticalToMatcher struct {
6+
value.Value
7+
}
8+
9+
func (BeIdenticalToMatcher) Type() Type {
10+
return BeIdenticalToMatcherType
11+
}
12+
13+
func (BeIdenticalToMatcher) MatcherName() string {
14+
return beIdenticalTo
15+
}
16+
17+
type BeEquivalentToMatcher struct {
18+
value.Value
19+
}
20+
21+
func (BeEquivalentToMatcher) Type() Type {
22+
return BeEquivalentToMatcherType
23+
}
24+
25+
func (BeEquivalentToMatcher) MatcherName() string {
26+
return beEquivalentTo
27+
}
28+
29+
type BeZeroMatcher struct{}
30+
31+
func (BeZeroMatcher) Type() Type {
32+
return BeZeroMatcherType
33+
}
34+
35+
func (BeZeroMatcher) MatcherName() string {
36+
return beZero
37+
}
38+
39+
type BeEmptyMatcher struct{}
40+
41+
func (BeEmptyMatcher) Type() Type {
42+
return BeEmptyMatcherType
43+
}
44+
45+
func (BeEmptyMatcher) MatcherName() string {
46+
return beEmpty
47+
}
48+
49+
type BeTrueMatcher struct{}
50+
51+
func (BeTrueMatcher) Type() Type {
52+
return BeTrueMatcherType | BoolValueTrue
53+
}
54+
55+
func (BeTrueMatcher) MatcherName() string {
56+
return beTrue
57+
}
58+
59+
type BeFalseMatcher struct{}
60+
61+
func (BeFalseMatcher) Type() Type {
62+
return BeFalseMatcherType | BoolValueFalse
63+
}
64+
65+
func (BeFalseMatcher) MatcherName() string {
66+
return beFalse
67+
}
68+
69+
type BeNilMatcher struct{}
70+
71+
func (BeNilMatcher) Type() Type {
72+
return BeNilMatcherType
73+
}
74+
75+
func (BeNilMatcher) MatcherName() string {
76+
return beNil
77+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package matcher
2+
3+
import (
4+
"go/ast"
5+
"go/constant"
6+
"go/token"
7+
gotypes "go/types"
8+
9+
"golang.org/x/tools/go/analysis"
10+
11+
"github.com/nunnatsa/ginkgolinter/internal/expression/value"
12+
)
13+
14+
type BeNumericallyMatcher struct {
15+
op token.Token
16+
value value.Valuer
17+
argType Type
18+
}
19+
20+
var compareOps = map[string]token.Token{
21+
`"=="`: token.EQL,
22+
`"<"`: token.LSS,
23+
`">"`: token.GTR,
24+
`"="`: token.ASSIGN,
25+
`"!="`: token.NEQ,
26+
`"<="`: token.LEQ,
27+
`">="`: token.GEQ,
28+
}
29+
30+
func getCompareOp(opExp ast.Expr) token.Token {
31+
basic, ok := opExp.(*ast.BasicLit)
32+
if !ok {
33+
return token.ILLEGAL
34+
}
35+
if basic.Kind != token.STRING {
36+
return token.ILLEGAL
37+
}
38+
39+
if tk, ok := compareOps[basic.Value]; ok {
40+
return tk
41+
}
42+
43+
return token.ILLEGAL
44+
}
45+
46+
func newBeNumericallyMatcher(opExp, orig, clone ast.Expr, pass *analysis.Pass) Info {
47+
op := getCompareOp(opExp)
48+
if op == token.ILLEGAL {
49+
return &UnspecifiedMatcher{
50+
matcherName: beNumerically,
51+
}
52+
}
53+
54+
val := value.GetValuer(orig, clone, pass)
55+
argType := BeNumericallyMatcherType
56+
57+
if val.IsValueNumeric() {
58+
if v := val.GetValue().String(); v == "0" {
59+
switch op {
60+
case token.EQL:
61+
argType |= EqualZero
62+
63+
case token.NEQ, token.GTR:
64+
argType |= GreaterThanZero
65+
}
66+
} else if v == "1" && op == token.GEQ {
67+
argType |= GreaterThanZero
68+
}
69+
}
70+
71+
return &BeNumericallyMatcher{
72+
op: op,
73+
value: val,
74+
argType: argType,
75+
}
76+
}
77+
78+
func (m BeNumericallyMatcher) Type() Type {
79+
return m.argType
80+
}
81+
82+
func (BeNumericallyMatcher) MatcherName() string {
83+
return beNumerically
84+
}
85+
86+
func (m BeNumericallyMatcher) GetValueExpr() ast.Expr {
87+
return m.value.GetValueExpr()
88+
}
89+
90+
func (m BeNumericallyMatcher) GetValue() constant.Value {
91+
return m.value.GetValue()
92+
}
93+
94+
func (m BeNumericallyMatcher) GetType() gotypes.Type {
95+
return m.value.GetType()
96+
}
97+
98+
func (m BeNumericallyMatcher) GetOp() token.Token {
99+
return m.op
100+
}
101+
102+
func (m BeNumericallyMatcher) IsValueZero() bool {
103+
return m.value.IsValueZero()
104+
}
105+
106+
func (m BeNumericallyMatcher) IsValueInt() bool {
107+
return m.value.IsValueInt()
108+
}
109+
110+
func (m BeNumericallyMatcher) IsValueNumeric() bool {
111+
return m.value.IsValueNumeric()
112+
}
113+
114+
func (m BeNumericallyMatcher) IsError() bool {
115+
return m.value.IsError()
116+
}
117+
118+
func (m BeNumericallyMatcher) IsFunc() bool {
119+
return m.value.IsFunc()
120+
}
121+
122+
func (m BeNumericallyMatcher) IsInterface() bool {
123+
return m.value.IsInterface()
124+
}
125+
126+
func (m BeNumericallyMatcher) IsPointer() bool {
127+
return m.value.IsPointer()
128+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package matcher
2+
3+
import (
4+
"go/ast"
5+
"go/constant"
6+
gotypes "go/types"
7+
8+
"golang.org/x/tools/go/analysis"
9+
10+
"github.com/nunnatsa/ginkgolinter/internal/expression/value"
11+
)
12+
13+
func newEqualMatcher(orig, clone ast.Expr, pass *analysis.Pass) Info {
14+
t := pass.TypesInfo.Types[orig]
15+
16+
if t.Value != nil {
17+
if t.Value.Kind() == constant.Bool {
18+
if t.Value.String() == "true" {
19+
return &EqualTrueMatcher{}
20+
}
21+
return &EqualFalseMatcher{}
22+
}
23+
}
24+
25+
if value.IsNil(orig, pass) {
26+
return &EqualNilMatcher{
27+
gotype: pass.TypesInfo.TypeOf(orig),
28+
}
29+
}
30+
31+
val := value.GetValuer(orig, clone, pass)
32+
33+
return &EqualMatcher{
34+
val: val,
35+
}
36+
}
37+
38+
type EqualMatcher struct {
39+
val value.Valuer
40+
}
41+
42+
func (EqualMatcher) Type() Type {
43+
return EqualMatcherType
44+
}
45+
46+
func (EqualMatcher) MatcherName() string {
47+
return equal
48+
}
49+
50+
func (m EqualMatcher) GetValue() constant.Value {
51+
return m.val.GetValue()
52+
}
53+
54+
func (m EqualMatcher) GetType() gotypes.Type {
55+
return m.val.GetType()
56+
}
57+
58+
func (m EqualMatcher) GetValueExpr() ast.Expr {
59+
return m.val.GetValueExpr()
60+
}
61+
62+
func (m EqualMatcher) IsValueZero() bool {
63+
return m.val.IsValueZero()
64+
}
65+
66+
func (m EqualMatcher) IsValueInt() bool {
67+
return m.val.IsValueInt()
68+
}
69+
70+
func (m EqualMatcher) IsValueNumeric() bool {
71+
return m.val.IsValueNumeric()
72+
}
73+
74+
func (m EqualMatcher) IsError() bool {
75+
return m.val.IsError()
76+
}
77+
78+
func (m EqualMatcher) IsFunc() bool {
79+
return m.val.IsFunc()
80+
}
81+
82+
func (m EqualMatcher) IsInterface() bool {
83+
return m.val.IsInterface()
84+
}
85+
86+
func (m EqualMatcher) IsPointer() bool {
87+
return m.val.IsPointer()
88+
}
89+
90+
type EqualNilMatcher struct {
91+
gotype gotypes.Type
92+
}
93+
94+
func (EqualNilMatcher) Type() Type {
95+
return EqualNilMatcherType | EqualMatcherType | EqualValueMatcherType
96+
}
97+
98+
func (EqualNilMatcher) MatcherName() string {
99+
return equal
100+
}
101+
102+
func (n EqualNilMatcher) GetType() gotypes.Type {
103+
return n.gotype
104+
}
105+
106+
type EqualTrueMatcher struct{}
107+
108+
func (EqualTrueMatcher) Type() Type {
109+
return EqualMatcherType | EqualBoolValueMatcherType | BoolValueTrue
110+
}
111+
112+
func (EqualTrueMatcher) MatcherName() string {
113+
return equal
114+
}
115+
116+
type EqualFalseMatcher struct{}
117+
118+
func (EqualFalseMatcher) Type() Type {
119+
return EqualMatcherType | EqualBoolValueMatcherType | BoolValueFalse
120+
}
121+
122+
func (EqualFalseMatcher) MatcherName() string {
123+
return equal
124+
}

0 commit comments

Comments
 (0)