Skip to content

Commit

Permalink
Update README to be in sync with latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
petergtz committed May 11, 2023
1 parent b4ac288 commit c3b20cc
Showing 1 changed file with 17 additions and 42 deletions.
59 changes: 17 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var _ = Describe("Some function", func() {
When("certain condition", func() {
It("succeeds", func() {
mock := NewMockPhoneBook()
Whenever(mock.GetPhoneNumber(EqString("Tom"))).ThenReturn("123-456-789")
Whenever(mock.GetPhoneNumber(Eq("Tom"))).ThenReturn("123-456-789")
})
})
})
Expand Down Expand Up @@ -152,7 +152,6 @@ I've looked at some of the other frameworks, but found none of them satisfying:
fake.DoThings("stuff", 5)
fake.VerifyWasCalledOnce().DoThings("stuff", 5)
```
- [Hel](https://github.com/nelsam/hel) uses a new and interesting approach to setting up and verifying mocks. However, I wonder how flexible it actually is. E.g. how about providing a callback function when stubbing? Can this be modeled with its current approach using channels?

In addition, Pegomock provides a "watch" command similar to [Ginkgo](http://onsi.github.io/ginkgo/), which constantly watches over changes in an interface and updates its mocks. It gives the framework a much more dynamic feel, similar to mocking frameworks in Ruby or Java.

Expand Down Expand Up @@ -278,31 +277,17 @@ fmt.Println(phoneBook.GetPhoneNumber("Tom"))
// Incorrect, panics:
When(contactList.getContactByFullName("Dan", AnyString())).thenReturn(Contact{...})
// Correct:
When(contactList.getContactByFullName(EqString("Dan"), AnyString())).thenReturn(Contact{...})
When(contactList.getContactByFullName(Eq("Dan"), AnyString())).thenReturn(Contact{...})
```

### Writing Your Own Argument Matchers
Matching custom types:

**Important:** `Eq...`, `NotEq...`, `Any...` and `...That` matchers for types used in mock methods,
can now be _auto-generated_ while generating the mock. The `...That` argument matcher accepts an
argument implementing the `pegomock.ArgumentMatcher` interface and allows you to write and use custom
matcher logic without having to create a new argument matcher method for each type you want to match.

So writing your own argument matchers is not necessary for most use cases. See section
[The Pegomock CLI](#generating-mocks) for more information.

If you are not using the option to generate matchers you can write your own for non-basic types.
E.g. if you have a `struct MyType`, you can write an _Equals_ and _Any_ matcher like this:
```go
func EqMyType(value MyType) MyType {
RegisterMatcher(&EqMatcher{Value: value})
return MyType{}
}
type SearchQuery struct { ... }

func AnyMyType() MyType {
RegisterMatcher(NewAnyMatcher(reflect.TypeOf(MyType{})))
return MyType{}
}
When(contactList.searchContacts(Eq(SearchQuery{...}))).thenReturn([]Contact{...})

When(contactList.searchContacts(Any[SearchQuery]())).thenReturn([]Contact{...})
```


Expand Down Expand Up @@ -467,31 +452,21 @@ See [Tools as dependencies] for details.
Generating Mocks
----------------

Pegomock can generate mocks in two different ways:

1. by parsing source code Go files
To generate mocks, invoke Pegomock like this:

```
pegomock generate [<flags>] <gofile>
```

2. by building a Go package and using reflection

```
pegomock generate [<flags>] [<packagepath>] <interfacename>
```
```shell
pegomock generate [<flags>] [<packagepath>] <interfacename>
```

Flags can be any of the following:

- `--output,-o`: Output file; defaults to mock_<interface>_test.go.

- `--package`: Package of the generated code; defaults to the package from which pegomock was executed suffixed with _test

- `--generate-matchers,-m`: This will auto-generate argument matchers and place them in a `matchers` directory alongside the mock source code itself.

For more flags, run:

```
```shell
pegomock --help
```

Expand All @@ -502,7 +477,7 @@ There are a number of shortcomings in the current reflection-based implementatio
To overcome these, there is now an option to use a new, experimental implementation that is based on [golang.org/x/tools/go/loader](https://godoc.org/golang.org/x/tools/go/loader).
To use it when generating your mocks, invoke `pegomock` like this:

```
```shell
pegomock generate --use-experimental-model-gen [<flags>] [<packagepath>] <interfacename>
```

Expand Down Expand Up @@ -546,7 +521,7 @@ mockDisplay := NewMockDisplay()
```

Generating it:
```sh
```shell
cd package/path/to/calculator
go generate
```
Expand All @@ -559,7 +534,7 @@ Continuously Generating Mocks

The `watch` command lets Pegomock generate mocks continuously on every change to an interface:

```
```shell
pegomock watch
```

Expand Down Expand Up @@ -589,10 +564,10 @@ Removing Generated Mocks
-----------------------------

Sometimes it can be useful to systematically remove all mocks and matcher files generated by Pegomock. For this purpose, there is the `remove` command. By simply calling it from the current directory
```
```shell
pegomock remove
```
it will remove all Pegomock-generated files in the current directory. It supports additional flags, such as `--recursive` to recursively remove all Pegomock-generated files in sub-directories as well. To see all possible options, run:
```
```shell
pegomock remove --help
```

0 comments on commit c3b20cc

Please sign in to comment.