-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add filter possibility for signals with parameters (#161)
Refactor: - Rename `Trigger` to `BeSignaled` and `NotTrigger` to `NotBeSignaled` - Add an `IsSignaled(Times)` method on the recording interfaces - Rename `CallbackRecorder` to `SignalCounter` - Remove the static `Record` class - Remove the `ISignalCounter` and `ISignalCounterResult` interfaces (and use the classes directly) - Change the return type of `BeSignaled` and `NotBeSignaled` to use the result ### New feature: You can now filter for signals with specific parameters by providing a `predicate`: ```csharp SignalCounter<string> signal = new(); signal.Signal("foo"); signal.Signal("bar"); signal.Signal("foo"); await That(signal).Should().BeSignaled(2.Times()).With(p => p == "foo"); ```
- Loading branch information
Showing
32 changed files
with
1,334 additions
and
1,029 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using aweXpect.Recording; | ||
|
||
namespace aweXpect.Options; | ||
|
||
/// <summary> | ||
/// Options for <see cref="SignalCounter" /> | ||
/// </summary> | ||
public class SignalCounterOptions | ||
{ | ||
/// <summary> | ||
/// The timeout to use for the recording. | ||
/// </summary> | ||
public TimeSpan? Timeout { get; set; } | ||
|
||
/// <inheritdoc cref="object.ToString()" /> | ||
public override string ToString() | ||
{ | ||
if (Timeout == null) | ||
{ | ||
return ""; | ||
} | ||
|
||
return $" within {Formatter.Format(Timeout.Value)}"; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Options for <see cref="SignalCounter{TParameter}" /> | ||
/// </summary> | ||
public class SignalCounterOptions<TParameter> : SignalCounterOptions | ||
{ | ||
private StringBuilder? _builder; | ||
private List<Func<TParameter, bool>>? _predicates; | ||
|
||
/// <summary> | ||
/// Add a predicate to the signal counter options. | ||
/// </summary> | ||
public void WithPredicate(Func<TParameter, bool> predicate, string predicateExpression) | ||
{ | ||
_predicates ??= new List<Func<TParameter, bool>>(); | ||
_predicates.Add(predicate); | ||
if (_builder is null) | ||
{ | ||
_builder = new StringBuilder(); | ||
_builder.Append(" with "); | ||
_builder.Append(predicateExpression); | ||
} | ||
else | ||
{ | ||
_builder.Append(" and with "); | ||
_builder.Append(predicateExpression); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Checks if the <paramref name="parameter" /> matches all registered predicates. | ||
/// </summary> | ||
public bool Matches(TParameter parameter) | ||
{ | ||
if (_predicates is null) | ||
{ | ||
return true; | ||
} | ||
|
||
return _predicates.All(predicate => predicate(parameter)); | ||
} | ||
|
||
/// <inheritdoc cref="object.ToString()" /> | ||
public override string ToString() | ||
=> (_builder, Timeout) switch | ||
{ | ||
(null, null) => "", | ||
(null, _) => $" within {Formatter.Format(Timeout.Value)}", | ||
(_, null) => _builder.ToString(), | ||
(_, _) => _builder.Append($" within {Formatter.Format(Timeout.Value)}").ToString() | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.