diff --git a/README.md b/README.md index 9336b51..d52a0b7 100644 --- a/README.md +++ b/README.md @@ -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") }) }) }) @@ -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. @@ -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{...}) ``` @@ -467,19 +452,11 @@ 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 [] - ``` - -2. by building a Go package and using reflection - - ``` - pegomock generate [] [] - ``` +```shell +pegomock generate [] [] +``` Flags can be any of the following: @@ -487,11 +464,9 @@ Flags can be any of the following: - `--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 ``` @@ -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 [] [] ``` @@ -546,7 +521,7 @@ mockDisplay := NewMockDisplay() ``` Generating it: -```sh +```shell cd package/path/to/calculator go generate ``` @@ -559,7 +534,7 @@ Continuously Generating Mocks The `watch` command lets Pegomock generate mocks continuously on every change to an interface: -``` +```shell pegomock watch ``` @@ -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 ```