-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Rule: should not compare two different types
The `Equal` and the `BeIdentical` matchers also check the type, not only the value. The following code will fail in runtime: ```go x := 5 // x is int Expect(x).Should(Eqaul(uint(5)) // x and uint(5) are with different types ``` When using negative checks, it's even worse, because we get a fale positive: ``` x := 5 Expect(x).ToNot(Equal(uint(5)) ``` To solve this problem, we want to find these errors before running the tests and failing on runtime. This is even more important for e2e or functional tests, where we sometimes can't run them on the local development environment. This PR adds a new rule to the linter, to find comaprison of values from different types.
- Loading branch information
Showing
13 changed files
with
498 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package comparetypes_test | ||
|
||
import ( | ||
"fmt" | ||
. "github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
) | ||
|
||
func fint64() int64 { | ||
return 5 | ||
} | ||
|
||
type mytype int | ||
|
||
func fmytype() mytype { | ||
return 5 | ||
} | ||
|
||
func multi() (int64, error) { | ||
return 4, nil | ||
} | ||
|
||
type myinf interface { | ||
doSomething() | ||
} | ||
|
||
type imp1 int | ||
|
||
func (imp1) doSomething() { | ||
fmt.Println("doing something") | ||
} | ||
|
||
type imp2 int | ||
|
||
func (imp2) doSomething() { | ||
fmt.Println("doing something else") | ||
} | ||
|
||
var _ = Describe("compare different types", func() { | ||
It("find false positive check", func() { | ||
a := 5 | ||
gomega.Expect(multi()).ShouldNot(gomega.Equal(4)) // want `ginkgo-linter: use Equal with different types: Comparing int64 with int; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(a).ShouldNot(gomega.Equal(5)) | ||
gomega.Expect(a).ShouldNot(gomega.Equal(uint(5))) // want `ginkgo-linter: use Equal with different types: Comparing int with uint; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(fint64()).ShouldNot(gomega.Equal(uint64(5))) // want `ginkgo-linter: use Equal with different types: Comparing int64 with uint64; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(fmytype()).ShouldNot(gomega.Equal(uint64(5))) // want `ginkgo-linter: use Equal with different types: Comparing a/comparetypes_test.mytype with uint64; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(a).ShouldNot(gomega.Equal(int32(5))) // want `ginkgo-linter: use Equal with different types: Comparing int with int32; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(a).ShouldNot(gomega.Equal(uint8(5))) // want `ginkgo-linter: use Equal with different types: Comparing int with uint8; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(a).ShouldNot(gomega.Equal(mytype(5))) // want `ginkgo-linter: use Equal with different types: Comparing int with a/comparetypes_test.mytype; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(5).ShouldNot(gomega.Equal(mytype(5))) // want `ginkgo-linter: use Equal with different types: Comparing int with a/comparetypes_test.mytype; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
|
||
b := int16(5) | ||
gomega.Expect(a).ShouldNot(gomega.Equal(b)) // want `ginkgo-linter: use Equal with different types: Comparing int with int16; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
|
||
c := mytype(5) | ||
gomega.Expect(a).ShouldNot(gomega.Equal(c)) // want `ginkgo-linter: use Equal with different types: Comparing int with a/comparetypes_test.mytype; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
}) | ||
|
||
It("compare interfaces", func() { | ||
var ( | ||
a myinf = imp1(3) | ||
b myinf = imp2(3) | ||
) | ||
gomega.Expect(a).ShouldNot(gomega.Equal(b)) // this is not good, but not an error. Should have kind of warning here. | ||
gomega.Expect(a).ShouldNot(gomega.BeEquivalentTo(b)) | ||
gomega.Expect(a).ShouldNot(gomega.BeIdenticalTo(b)) // this is not good, but not an error. Should have kind of warning here. | ||
}) | ||
|
||
It("check HaveValue(Equal)", func() { | ||
a := 5 | ||
pa := &a | ||
|
||
gomega.Expect(pa).Should(gomega.HaveValue(gomega.Equal(uint64(5)))) // want `ginkgo-linter: use Equal with different types: Comparing int with uint64; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(a).Should(gomega.Not(gomega.Equal(uint64(5)))) // want `ginkgo-linter: use Equal with different types: Comparing int with uint64; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(a).Should(gomega.And(gomega.Equal(uint64(5)), gomega.Not(gomega.Equal(int32(6))))) // want `ginkgo-linter: use Equal with different types: Comparing int with uint64; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` `ginkgo-linter: use Equal with different types: Comparing int with int32; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(a).Should(gomega.Or(gomega.Equal(uint64(5)), gomega.Not(gomega.Equal(int32(6))), gomega.Not(gomega.Equal(int8(4))))) // want `ginkgo-linter: use Equal with different types: Comparing int with uint64; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` `ginkgo-linter: use Equal with different types: Comparing int with int32; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` `ginkgo-linter: use Equal with different types: Comparing int with int8; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
gomega.Expect(a).Should(gomega.WithTransform(func(i int) int { return i + 1 }, gomega.Equal(uint(6)))) // want `ginkgo-linter: use Equal with different types: Comparing int with uint; either change the expected value type if possible, or use the BeEquivalentTo\(\) matcher, instead of Equal\(\)` | ||
}) | ||
}) |
Oops, something went wrong.