Skip to content

Commit

Permalink
Merge pull request #29 from Antonboom/feat/support-more-assertions
Browse files Browse the repository at this point in the history
Support more assertions like `Exactly`, `EqualValues`, etc.
  • Loading branch information
Antonboom authored Nov 5, 2023
2 parents c6fc0ad + cebee31 commit 8fe949b
Show file tree
Hide file tree
Showing 33 changed files with 1,213 additions and 386 deletions.
50 changes: 50 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ Describe a new checker in [checkers section](./README.md#checkers).
- [elements-match](#elements-match)
- [error-compare](#error-compare)
- [equal-values](#equal-values)
- [graceful-teardown](#graceful-teardown)
- [float-compare](#float-compare)
- [http-const](#http-const)
- [http-sugar](#http-sugar)
- [inefficient-assert](#inefficient-assert)
- [negative-positive](#negative-positive)
- [no-fmt-mess](#no-fmt-mess)
- [require-len](#require-len)
Expand Down Expand Up @@ -178,6 +180,29 @@ Describe a new checker in [checkers section](./README.md#checkers).

---

### graceful-teardown

Warns about usage of `require` in `t.Cleanup` functions and suite teardown methods:

```go
func (s *ServiceIntegrationSuite) TearDownTest() {
if p := s.verdictsProducer; p != nil {
s.Require().NoError(p.Close()) ❌
}
if c := s.dlqVerdictsConsumer; c != nil {
s.NoError(c.Close())
}
s.DBSuite.TearDownTest()
s.ks.TearDownTest()
}
```

**Autofix**: false. <br>
**Enabled by default**: false. <br>
**Reason**: Possible resource leaks, because `require` finishes the current goroutine.

---

### equal-values

```go
Expand Down Expand Up @@ -272,6 +297,31 @@ And similar idea for `assert.InEpsilonSlice` / `assert.InDeltaSlice`.

---

### inefficient-assert

Simple:
```go
body, err := io.ReadAll(rr.Body)
require.NoError(t, err)
require.NoError(t, err) ❌

expectedJSON, err := json.Marshal(expected)
require.NoError(t, err)
require.JSONEq(t, string(expectedJSON), string(body))
```

Complex:
```go
require.NoError(t, err)
assert.ErrorContains(t, err, "user") ❌
```

**Autofix**: false. <br>
**Enabled by default**: Probably false, depends on implementation performance. <br>
**Reason**: Code simplification, elimination of possible bugs.

---

### negative-positive

```go
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ func TestBoolCompareChecker(t *testing.T) {
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Truef"
assert.True(t, predicate) // want "bool-compare: need to simplify the assertion"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: need to simplify the assertion"
assert.True(t, predicate) // want "bool-compare: need to simplify the assertion"
Expand Down Expand Up @@ -60,6 +72,18 @@ func TestBoolCompareChecker(t *testing.T) {
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Falsef"
assert.False(t, predicate) // want "bool-compare: use assert\\.False"
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Falsef"
assert.False(t, predicate) // want "bool-compare: use assert\\.False"
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Falsef"
assert.False(t, predicate) // want "bool-compare: use assert\\.False"
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Falsef"
assert.False(t, predicate) // want "bool-compare: use assert\\.False"
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Falsef"
assert.False(t, predicate) // want "bool-compare: use assert\\.False"
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Falsef"
assert.False(t, predicate) // want "bool-compare: use assert\\.False"
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Falsef"
assert.False(t, predicate) // want "bool-compare: use assert\\.False"
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.Falsef"
assert.False(t, predicate) // want "bool-compare: need to simplify the assertion"
assert.Falsef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: need to simplify the assertion"
assert.False(t, predicate) // want "bool-compare: need to simplify the assertion"
Expand Down
28 changes: 20 additions & 8 deletions analyzer/testdata/src/checkers-default/empty/empty_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions analyzer/testdata/src/checkers-default/empty/empty_test.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func TestEmptyChecker(t *testing.T) {
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"
assert.Empty(t, elems) // want "empty: use assert\\.Empty"
assert.Emptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.Emptyf"

// Valid.
assert.Empty(t, elems)
Expand All @@ -45,6 +53,10 @@ func TestEmptyChecker(t *testing.T) {
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"
assert.NotEmpty(t, elems) // want "empty: use assert\\.NotEmpty"
assert.NotEmptyf(t, elems, "msg with args %d %s", 42, "42") // want "empty: use assert\\.NotEmptyf"

// Valid.
assert.NotEmpty(t, elems)
Expand Down
44 changes: 28 additions & 16 deletions analyzer/testdata/src/checkers-default/error-nil/error_nil_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ func TestErrorNilChecker(t *testing.T) {
assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf"
assert.NoError(t, err) // want "error-nil: use assert\\.NoError"
assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf"
assert.NoError(t, err) // want "error-nil: use assert\\.NoError"
assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf"
assert.NoError(t, err) // want "error-nil: use assert\\.NoError"
assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf"
assert.NoError(t, err) // want "error-nil: use assert\\.NoError"
assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf"
assert.NoError(t, err) // want "error-nil: use assert\\.NoError"
assert.NoErrorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.NoErrorf"
assert.Error(t, err) // want "error-nil: use assert\\.Error"
assert.Errorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.Errorf"
assert.Error(t, err) // want "error-nil: use assert\\.Error"
assert.Errorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.Errorf"
assert.Error(t, err) // want "error-nil: use assert\\.Error"
assert.Errorf(t, err, "msg with args %d %s", 42, "42") // want "error-nil: use assert\\.Errorf"
assert.Error(t, err) // want "error-nil: use assert\\.Error"
Expand Down
Loading

0 comments on commit 8fe949b

Please sign in to comment.