|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Reflection; |
| 3 | +using System.Text; |
| 4 | +using aweXpect.Core; |
| 5 | +using aweXpect.Core.Constraints; |
| 6 | +using aweXpect.Reflection.Helpers; |
| 7 | +using aweXpect.Results; |
| 8 | +#if NET8_0_OR_GREATER |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +#endif |
| 12 | + |
| 13 | +// ReSharper disable PossibleMultipleEnumeration |
| 14 | + |
| 15 | +namespace aweXpect.Reflection; |
| 16 | + |
| 17 | +public static partial class ThatEvents |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Verifies that all items in the filtered collection of <see cref="EventInfo" /> are virtual. |
| 21 | + /// </summary> |
| 22 | + public static AndOrResult<IEnumerable<EventInfo?>, IThat<IEnumerable<EventInfo?>>> AreVirtual( |
| 23 | + this IThat<IEnumerable<EventInfo?>> subject) |
| 24 | + => new(subject.Get().ExpectationBuilder.AddConstraint<IEnumerable<EventInfo?>>((it, grammars) |
| 25 | + => new AreVirtualConstraint(it, grammars)), |
| 26 | + subject); |
| 27 | + |
| 28 | +#if NET8_0_OR_GREATER |
| 29 | + /// <summary> |
| 30 | + /// Verifies that all items in the filtered collection of <see cref="EventInfo" /> are virtual. |
| 31 | + /// </summary> |
| 32 | + public static AndOrResult<IAsyncEnumerable<EventInfo?>, IThat<IAsyncEnumerable<EventInfo?>>> AreVirtual( |
| 33 | + this IThat<IAsyncEnumerable<EventInfo?>> subject) |
| 34 | + => new(subject.Get().ExpectationBuilder.AddConstraint<IAsyncEnumerable<EventInfo?>>((it, grammars) |
| 35 | + => new AreVirtualConstraint(it, grammars)), |
| 36 | + subject); |
| 37 | +#endif |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Verifies that all items in the filtered collection of <see cref="EventInfo" /> are not virtual. |
| 41 | + /// </summary> |
| 42 | + public static AndOrResult<IEnumerable<EventInfo?>, IThat<IEnumerable<EventInfo?>>> AreNotVirtual( |
| 43 | + this IThat<IEnumerable<EventInfo?>> subject) |
| 44 | + => new(subject.Get().ExpectationBuilder.AddConstraint<IEnumerable<EventInfo?>>((it, grammars) |
| 45 | + => new AreNotVirtualConstraint(it, grammars)), |
| 46 | + subject); |
| 47 | + |
| 48 | +#if NET8_0_OR_GREATER |
| 49 | + /// <summary> |
| 50 | + /// Verifies that all items in the filtered collection of <see cref="EventInfo" /> are not virtual. |
| 51 | + /// </summary> |
| 52 | + public static AndOrResult<IAsyncEnumerable<EventInfo?>, IThat<IAsyncEnumerable<EventInfo?>>> AreNotVirtual( |
| 53 | + this IThat<IAsyncEnumerable<EventInfo?>> subject) |
| 54 | + => new(subject.Get().ExpectationBuilder.AddConstraint<IAsyncEnumerable<EventInfo?>>((it, grammars) |
| 55 | + => new AreNotVirtualConstraint(it, grammars)), |
| 56 | + subject); |
| 57 | +#endif |
| 58 | + |
| 59 | + private sealed class AreVirtualConstraint(string it, ExpectationGrammars grammars) |
| 60 | + : CollectionConstraintResult<EventInfo?>(grammars), |
| 61 | + IValueConstraint<IEnumerable<EventInfo?>> |
| 62 | +#if NET8_0_OR_GREATER |
| 63 | + , IAsyncConstraint<IAsyncEnumerable<EventInfo?>> |
| 64 | +#endif |
| 65 | + { |
| 66 | +#if NET8_0_OR_GREATER |
| 67 | + public async Task<ConstraintResult> IsMetBy(IAsyncEnumerable<EventInfo?> actual, |
| 68 | + CancellationToken cancellationToken) |
| 69 | + => await SetAsyncValue(actual, @event => @event.IsReallyVirtual()); |
| 70 | +#endif |
| 71 | + |
| 72 | + public ConstraintResult IsMetBy(IEnumerable<EventInfo?> actual) |
| 73 | + => SetValue(actual, @event => @event.IsReallyVirtual()); |
| 74 | + |
| 75 | + protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null) |
| 76 | + => stringBuilder.Append("are all virtual"); |
| 77 | + |
| 78 | + protected override void AppendNormalResult(StringBuilder stringBuilder, string? indentation = null) |
| 79 | + { |
| 80 | + stringBuilder.Append(it).Append(" contained non-virtual events "); |
| 81 | + Formatter.Format(stringBuilder, NotMatching, FormattingOptions.Indented(indentation)); |
| 82 | + } |
| 83 | + |
| 84 | + protected override void AppendNegatedExpectation(StringBuilder stringBuilder, string? indentation = null) |
| 85 | + => stringBuilder.Append("are not all virtual"); |
| 86 | + |
| 87 | + protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null) |
| 88 | + { |
| 89 | + stringBuilder.Append(it).Append(" only contained virtual events "); |
| 90 | + Formatter.Format(stringBuilder, Matching, FormattingOptions.Indented(indentation)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private sealed class AreNotVirtualConstraint(string it, ExpectationGrammars grammars) |
| 95 | + : CollectionConstraintResult<EventInfo?>(grammars), |
| 96 | + IValueConstraint<IEnumerable<EventInfo?>> |
| 97 | +#if NET8_0_OR_GREATER |
| 98 | + , IAsyncConstraint<IAsyncEnumerable<EventInfo?>> |
| 99 | +#endif |
| 100 | + { |
| 101 | +#if NET8_0_OR_GREATER |
| 102 | + public async Task<ConstraintResult> IsMetBy(IAsyncEnumerable<EventInfo?> actual, |
| 103 | + CancellationToken cancellationToken) |
| 104 | + => await SetAsyncValue(actual, @event => !@event.IsReallyVirtual()); |
| 105 | +#endif |
| 106 | + |
| 107 | + public ConstraintResult IsMetBy(IEnumerable<EventInfo?> actual) |
| 108 | + => SetValue(actual, @event => !@event.IsReallyVirtual()); |
| 109 | + |
| 110 | + protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null) |
| 111 | + => stringBuilder.Append("are all not virtual"); |
| 112 | + |
| 113 | + protected override void AppendNormalResult(StringBuilder stringBuilder, string? indentation = null) |
| 114 | + { |
| 115 | + stringBuilder.Append(it).Append(" contained virtual events "); |
| 116 | + Formatter.Format(stringBuilder, NotMatching, FormattingOptions.Indented(indentation)); |
| 117 | + } |
| 118 | + |
| 119 | + protected override void AppendNegatedExpectation(StringBuilder stringBuilder, string? indentation = null) |
| 120 | + => stringBuilder.Append("also contain a virtual event"); |
| 121 | + |
| 122 | + protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null) |
| 123 | + { |
| 124 | + stringBuilder.Append(it).Append(" only contained non-virtual events "); |
| 125 | + Formatter.Format(stringBuilder, Matching, FormattingOptions.Indented(indentation)); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments