From 41c6f3ad7a10fd5e6ab05ba4d4c0634635b06172 Mon Sep 17 00:00:00 2001 From: Seb C Date: Mon, 29 Aug 2022 19:30:37 +0100 Subject: [PATCH] add mutators.None predicate (#105) --- README.md | 3 ++- cassette/track/mutator.go | 6 ++++++ cassette/track/mutator_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 746ed2a..5493988 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ 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: @@ -204,7 +205,7 @@ Examples: 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. diff --git a/cassette/track/mutator.go b/cassette/track/mutator.go index a6b37c6..cab4769 100644 --- a/cassette/track/mutator.go +++ b/cassette/track/mutator.go @@ -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 { diff --git a/cassette/track/mutator_test.go b/cassette/track/mutator_test.go index c5f7ff9..69266ec 100644 --- a/cassette/track/mutator_test.go +++ b/cassette/track/mutator_test.go @@ -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 {