Skip to content

Commit

Permalink
add example to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Feb 28, 2024
1 parent 340d3d0 commit e1c8a71
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 80 deletions.
File renamed without changes.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
IMG=ghcr.io/sonalys/fake

ENTRYPOINT=./entrypoint/fake/main.go
.PHONYE: all

build:
@CGO_ENABLED=0 go build -o ./bin/fake ./entrypoint/cli/main.go
@CGO_ENABLED=0 go build -o ./bin/fake ${ENTRYPOINT}

image: build
@docker build -t ${IMG}:latest -f dockerfile .
Expand All @@ -12,9 +12,9 @@ push:
@docker push ${IMG}

build_all:
@GOOS=windows GOARCH=amd64 go build -o ./bin/windows/amd64/fake.exe ./entrypoint/cli/main.go
@GOOS=linux GOARCH=amd64 go build -o ./bin/linux/amd64/fake ./entrypoint/cli/main.go
@GOOS=linux GOARCH=arm64 go build -o ./bin/linux/arm64/fake ./entrypoint/cli/main.go
@GOOS=windows GOARCH=amd64 go build -o ./bin/windows/amd64/fake.exe ${ENTRYPOINT}
@GOOS=linux GOARCH=amd64 go build -o ./bin/linux/amd64/fake ${ENTRYPOINT}
@GOOS=linux GOARCH=arm64 go build -o ./bin/linux/arm64/fake ${ENTRYPOINT}
@mkdir releases
@zip releases/fake_Windows_amd64.zip bin/windows/amd64/fake.exe
@zip releases/fake_Linux_amd64.zip bin/linux/amd64/fake
Expand Down
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,63 @@ The flags are:
-input STRING Folder to scan for interfaces, can be invoked multiple times
-output STRING Output folder, it will follow a tree structure repeating the package path
-ignore STRING Folder to ignore, can be invoked multiple times
```


## Example

Running against our weird interface

```go
type StubInterface[T comparable] interface {
WeirdFunc1(a any, b interface {
A() int
})
WeirdFunc2(in *<-chan time.Time, outs ...chan int) error
Empty()
WeirdFunc3(map[T]func(in ...*chan<- time.Time)) T
}
```

Will generate the following mock:

```go
type StubInterface[T comparable] struct {
setupWeirdFunc1 mock[func(a any, b interface {
A() int
})]
setupWeirdFunc2 mock[func(in *<-chan time.Time, outs ...chan int) error]
setupEmpty mock[func()]
setupWeirdFunc3 mock[func(a0 map[stub.T]func(in ...*chan<- time.Time)) stub.T]
}

func (s *StubInterface) OnWeirdFunc1(funcs ...func(a any, b interface { A() int })) Config
func (s *StubInterface) WeirdFunc1(a any, b interface { A() int })
...
```

So you can use it like this

```go

func Test_Stub(t *testing.T) {
mock := mocks.NewStubInterface(t)
mock.OnWeirdFunc1(func(a any, b interface { A() int }) {
require.NotNil(t, a)
...
})

var Stub StubInterface[any] = mock

Stub.WeirdFunc1(1, nil) // Will call the previous function.
}
```

You can pass more than one function or set repetition groups with

```go
mock.OnWeirdFunc1(func(a any, b interface { A() int }) {
require.NotNil(t, a)
...
}).Repeat(2)
```
5 changes: 1 addition & 4 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ WORKDIR /build
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/root/.cache/go-build go mod download
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -o ./bin/fake ./entrypoint/cli/main.go
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -o ./bin/fake ./entrypoint/fake/main.go

FROM scratch

COPY ./builders/passwd /etc/passwd
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

USER nobody

COPY --from=builder ./build/bin/fake /fake
COPY --from=builder /var/run /var/run
42 changes: 0 additions & 42 deletions entrypoint/cli/main.go

This file was deleted.

52 changes: 26 additions & 26 deletions testdata/out/testdata/stub.gen.go

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

6 changes: 3 additions & 3 deletions testdata/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package stub
import "time"

type StubInterface[T comparable] interface {
Interf(a any, b interface {
WeirdFunc1(a any, b interface {
A() int
})
KillYourself(in *<-chan time.Time, outs ...chan int) error
WeirdFunc2(in *<-chan time.Time, outs ...chan int) error
Empty()
Weird(map[T]func(in ...*chan<- time.Time)) T
WeirdFunc3(map[T]func(in ...*chan<- time.Time)) T
}

0 comments on commit e1c8a71

Please sign in to comment.