Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mutators.None predicate #105

Merged
merged 1 commit into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add mutators.None predicate
  • Loading branch information
seborama committed Aug 28, 2022
commit 3e24229e88b25e2df7cfd62622ff0b4fd2cf8a8c
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,15 @@ To help construct more complex yet readable predicates easily, **govcr** provide
- `Any` achieves a logical "**or**" of the provided predicates.
- `All` achieves a logical "**and**" of the provided predicates.
- `Not` achieves a logical "**not**" of the provided predicates.
- `None` is synonymous of "`Not` `Any`".

Examples:

```go
myMutator.
On(Any(...)). // proceeds if any of the "`...`" predicates is true
On(Not(Any(...))) // proceeds if none of the "`...`" predicates is true (i.e. all predicates are false)
On(Not(All(...))). // proceeds if not every of the "`...`" predicates is true (i.e. at least one predicate is false)
On(Not(All(...))). // proceeds if not every (including none) of the "`...`" predicates is true (i.e. at least one predicate is false, possibly all of them).
```

A **track recording mutator** can change both the request and the response that will be persisted to the cassette.
Expand Down
6 changes: 6 additions & 0 deletions cassette/track/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func All(predicates ...Predicate) Predicate {
)
}

// None requires all predicates to be false.
// I.e. it is the equivalent of Not(Any(...)).
func None(predicates ...Predicate) Predicate {
return Not(Any(predicates...))
}

// Not accepts one predicate and returns its logically contrary evaluation.
// I.e. it returns true when the supplied predicate is false and vice-versa.
func Not(predicate Predicate) Predicate {
Expand Down
28 changes: 28 additions & 0 deletions cassette/track/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ func Test_Mutator_Any(t *testing.T) {
require.True(t, result)
}

func Test_Mutator_None(t *testing.T) {
pTrue := track.Predicate(
func(trk *track.Track) bool {
return true
},
)

pFalse := track.Predicate(
func(trk *track.Track) bool {
return false
},
)

trk := track.NewTrack(nil, nil, nil)

result := track.None(pFalse, pTrue)(nil)
require.False(t, result)

result = track.None(pFalse, pTrue)(trk)
require.False(t, result)

result = track.None(pFalse, pFalse)(trk)
require.True(t, result)

result = track.None(pTrue, pTrue)(trk)
require.False(t, result)
}

func Test_Mutator_All(t *testing.T) {
pTrue := track.Predicate(
func(trk *track.Track) bool {
Expand Down