diff --git a/decoy/verifier.py b/decoy/verifier.py index 39644b1..a25e063 100644 --- a/decoy/verifier.py +++ b/decoy/verifier.py @@ -14,20 +14,31 @@ def verify( calls: Sequence[SpyCall], times: Optional[int] = None, ) -> None: - """Verify that a list of calls satisfies a given list of rehearsals.""" - if times is not None: - if len(calls) == times: - return None - - else: - for i in range(len(calls)): - calls_subset = calls[i : i + len(rehearsals)] - - if calls_subset == rehearsals: - return None - - raise VerifyError( - rehearsals=rehearsals, - calls=calls, - times=times, - ) + """Verify that a list of calls satisfies a given list of rehearsals. + + Arguments: + rehearsals: Rehearsal calls to verify. + calls: All calls made to the spies in the rehearsals list. + times: Number of times the rehearsal sequence should appear in calls. + If omitted, will look for "at least once." + + Raises: + VerifyError: the actual calls to the spy did not match the given + rehearsals. + """ + match_count = 0 + + for i in range(len(calls)): + calls_subset = calls[i : i + len(rehearsals)] + + if calls_subset == rehearsals: + match_count = match_count + 1 + + calls_verified = match_count != 0 if times is None else match_count == times + + if not calls_verified: + raise VerifyError( + rehearsals=rehearsals, + calls=calls, + times=times, + ) diff --git a/tests/test_verifier.py b/tests/test_verifier.py index 7016241..355407e 100644 --- a/tests/test_verifier.py +++ b/tests/test_verifier.py @@ -64,6 +64,16 @@ class VerifySpec(NamedTuple): ], times=1, ), + VerifySpec( + rehearsals=[ + VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}), + ], + calls=[ + SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}), + SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}), + ], + times=0, + ), ] verify_pass_specs = [ @@ -110,6 +120,23 @@ class VerifySpec(NamedTuple): ], times=2, ), + VerifySpec( + rehearsals=[ + VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}), + ], + calls=[ + SpyCall(spy_id=101, spy_name="spy_101", args=(4, 5, 6), kwargs={}), + SpyCall(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}), + ], + times=1, + ), + VerifySpec( + rehearsals=[ + VerifyRehearsal(spy_id=101, spy_name="spy_101", args=(1, 2, 3), kwargs={}), + ], + calls=[], + times=0, + ), ]