Skip to content

Commit

Permalink
add mutators.None predicate (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama authored Aug 29, 2022
1 parent 3d1c95c commit 41c6f3a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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

0 comments on commit 41c6f3a

Please sign in to comment.