Skip to content

Commit 98a1943

Browse files
committed
Use counterfeiter for fakes
1 parent 5da972b commit 98a1943

File tree

6 files changed

+152
-38
lines changed

6 files changed

+152
-38
lines changed

CONTRIBUTING.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,8 @@ submit changes. Contributions can be made in the form of GitHub
66
[pull requests](https://github.com/BooleanCat/go-functional/pulls).
77

88
When submitting an issue, please choose the relevant template or choose a blank issue if your query
9-
doesn't naturally fit into an existing template.
10-
11-
## TL;DR contribution checklist
12-
13-
- [ ] My code is formatted (`make check`)
14-
- [ ] I have run tests (`make test`)
15-
- [ ] My code has no lint errors (`make lint`)
16-
- [ ] All commits in my PR conform to the commit hygiene section
17-
- [ ] I have added relevant tests
18-
- [ ] I have not added any dependencies
9+
doesn't naturally fit into an existing template. The pull request template contains a contribution
10+
checklist.
1911

2012
## Zero-dependency
2113

@@ -25,6 +17,13 @@ must only incur one dependency: go-functional.
2517
Development dependencies are OK as they will not be included as dependencies to end-users (such as
2618
`golangci-lint`).
2719

20+
## Development dependencies
21+
22+
1. [golangci-lint](https://github.com/golangci/golangci-lint) is used to lint the project when
23+
running `make check`.
24+
2. [counterfeiter](https://github.com/maxbrunsfeld/counterfeiter) is used to generate new fakes
25+
(using `go generate ./...`). Fakes are declared in `internal/fakes/fakes.go`.
26+
2827
## Commit hygiene
2928

3029
- Commits should contain only a single change

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ test:
1717

1818
cov: SHELL:=/bin/bash
1919
cov:
20-
$(GO_BINARY) test -race -coverprofile=coverage.txt -covermode=atomic $$( $(GO_BINARY) list ./... | grep -v assert )
20+
$(GO_BINARY) test -race -coverprofile=coverage.txt -covermode=atomic $$( $(GO_BINARY) list ./... | grep -v assert | grep -v fakes )

internal/fakes/fakes.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package fakes
2+
3+
//go:generate counterfeiter -generate
4+
5+
//counterfeiter:generate --fake-name Reader -o . io.Reader

internal/fakes/reader.go

+120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

it/iter_test.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package it_test
22

33
import (
4+
"errors"
45
"fmt"
56
"slices"
67
"strings"
78
"testing"
89

910
"github.com/BooleanCat/go-functional/v2/internal/assert"
11+
"github.com/BooleanCat/go-functional/v2/internal/fakes"
1012
"github.com/BooleanCat/go-functional/v2/it"
1113
"github.com/BooleanCat/go-functional/v2/it/op"
1214
)
@@ -157,11 +159,13 @@ func ExampleTryCollect() {
157159
func TestTryCollectError(t *testing.T) {
158160
t.Parallel()
159161

160-
text := new(failSecondTime)
161-
lines, err := it.TryCollect(it.LinesString(text))
162+
reader := new(fakes.Reader)
163+
reader.ReadReturns(0, errors.New("read error"))
164+
165+
lines, err := it.TryCollect(it.LinesString(reader))
162166

163167
assert.Equal(t, err.Error(), "read error")
164-
assert.SliceEqual(t, lines, []string{"o"})
168+
assert.Empty[string](t, lines)
165169
}
166170

167171
func ExampleLen() {
@@ -249,7 +253,10 @@ func TestMustCollectPanic(t *testing.T) {
249253
}
250254
}()
251255

252-
it.MustCollect(it.LinesString(new(failSecondTime)))
256+
reader := new(fakes.Reader)
257+
reader.ReadReturns(0, errors.New("read error"))
258+
259+
it.MustCollect(it.LinesString(reader))
253260
}
254261

255262
func ExampleAll() {

it/lines_test.go

+6-23
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package it_test
33
import (
44
"errors"
55
"fmt"
6-
"io"
76
"strings"
87
"testing"
98

109
"github.com/BooleanCat/go-functional/v2/internal/assert"
10+
"github.com/BooleanCat/go-functional/v2/internal/fakes"
1111
"github.com/BooleanCat/go-functional/v2/it"
1212
)
1313

@@ -70,27 +70,6 @@ func TestLinesYieldsFalseWithError(t *testing.T) {
7070
})
7171
}
7272

73-
type failSecondTime struct {
74-
count int
75-
}
76-
77-
func (f *failSecondTime) Read(p []byte) (n int, err error) {
78-
if f.count == 0 {
79-
f.count++
80-
copy(p, []byte("o"))
81-
return 1, nil
82-
}
83-
84-
if f.count == 1 {
85-
f.count++
86-
return 0, errors.New("read error")
87-
}
88-
89-
return 0, io.EOF
90-
}
91-
92-
var _ io.Reader = new(failSecondTime)
93-
9473
func TestLinesFailsLater(t *testing.T) {
9574
t.Parallel()
9675

@@ -99,7 +78,11 @@ func TestLinesFailsLater(t *testing.T) {
9978
lastErr error
10079
)
10180

102-
for _, err := range it.LinesString(new(failSecondTime)) {
81+
reader := new(fakes.Reader)
82+
reader.ReadReturnsOnCall(0, 1, nil)
83+
reader.ReadReturnsOnCall(1, 0, errors.New("read error"))
84+
85+
for _, err := range it.LinesString(reader) {
10386
count++
10487
lastErr = err
10588
}

0 commit comments

Comments
 (0)