|
| 1 | +using System.Collections.Generic; |
| 2 | +using Mockolate.Parameters; |
| 3 | + |
| 4 | +namespace Mockolate.Tests.Parameters; |
| 5 | + |
| 6 | +public sealed class ParamsArrayParameterMatchTests |
| 7 | +{ |
| 8 | + [Fact] |
| 9 | + public async Task Matches_WhenLengthMatchesAndAllElementsSatisfy_ShouldReturnTrue() |
| 10 | + { |
| 11 | + StubParameter first = new(true, "first"); |
| 12 | + StubParameter second = new(true, "second"); |
| 13 | + ParamsArrayParameterMatch<int> sut = new(first, second); |
| 14 | + |
| 15 | + bool result = sut.Matches([1, 2,]); |
| 16 | + |
| 17 | + await That(result).IsTrue(); |
| 18 | + await That(first.MatchedValues).IsEqualTo([1,]); |
| 19 | + await That(second.MatchedValues).IsEqualTo([2,]); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public async Task Matches_WhenAnElementFails_ShouldReturnFalse() |
| 24 | + { |
| 25 | + ParamsArrayParameterMatch<int> sut = new(new StubParameter(true, "first"), new StubParameter(false, "second")); |
| 26 | + |
| 27 | + bool result = sut.Matches([1, 2,]); |
| 28 | + |
| 29 | + await That(result).IsFalse(); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public async Task Matches_WhenLengthDiffers_ShouldReturnFalseWithoutInvokingMatchers() |
| 34 | + { |
| 35 | + StubParameter only = new(true, "only"); |
| 36 | + ParamsArrayParameterMatch<int> sut = new(only); |
| 37 | + |
| 38 | + bool result = sut.Matches([1, 2,]); |
| 39 | + |
| 40 | + await That(result).IsFalse(); |
| 41 | + await That(only.MatchedValues).IsEmpty(); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public async Task Matches_WhenValueIsNull_ShouldReturnFalse() |
| 46 | + { |
| 47 | + ParamsArrayParameterMatch<int> sut = new(new StubParameter(true, "only")); |
| 48 | + |
| 49 | + bool result = sut.Matches(null!); |
| 50 | + |
| 51 | + await That(result).IsFalse(); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task Matches_WhenMatcherElementIsNull_ShouldReturnFalseWithoutThrowing() |
| 56 | + { |
| 57 | + ParamsArrayParameterMatch<int> sut = new(new StubParameter(true, "first"), null!); |
| 58 | + |
| 59 | + bool result = sut.Matches([1, 2,]); |
| 60 | + |
| 61 | + await That(result).IsFalse(); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async Task InvokeCallbacks_WhenLengthMatches_ShouldInvokeEachMatcher() |
| 66 | + { |
| 67 | + StubParameter first = new(true, "first"); |
| 68 | + StubParameter second = new(true, "second"); |
| 69 | + ParamsArrayParameterMatch<int> sut = new(first, second); |
| 70 | + |
| 71 | + sut.InvokeCallbacks([1, 2,]); |
| 72 | + |
| 73 | + await That(first.InvokedValues).IsEqualTo([1,]); |
| 74 | + await That(second.InvokedValues).IsEqualTo([2,]); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task InvokeCallbacks_WhenLengthDiffers_ShouldNotInvokeMatchers() |
| 79 | + { |
| 80 | + StubParameter only = new(true, "only"); |
| 81 | + ParamsArrayParameterMatch<int> sut = new(only); |
| 82 | + |
| 83 | + sut.InvokeCallbacks([1, 2,]); |
| 84 | + |
| 85 | + await That(only.InvokedValues).IsEmpty(); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public async Task InvokeCallbacks_WhenValueIsNull_ShouldNotInvokeMatchers() |
| 90 | + { |
| 91 | + StubParameter only = new(true, "only"); |
| 92 | + ParamsArrayParameterMatch<int> sut = new(only); |
| 93 | + |
| 94 | + sut.InvokeCallbacks(null!); |
| 95 | + |
| 96 | + await That(only.InvokedValues).IsEmpty(); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public async Task InvokeCallbacks_WhenMatcherElementIsNull_ShouldSkipItWithoutThrowing() |
| 101 | + { |
| 102 | + StubParameter first = new(true, "first"); |
| 103 | + ParamsArrayParameterMatch<int> sut = new(first, null!); |
| 104 | + |
| 105 | + sut.InvokeCallbacks([1, 2,]); |
| 106 | + |
| 107 | + await That(first.InvokedValues).IsEqualTo([1,]); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public async Task ToString_ShouldRenderMatchersInOrder() |
| 112 | + { |
| 113 | + ParamsArrayParameterMatch<int> sut = new(new StubParameter(true, "first"), new StubParameter(true, "second")); |
| 114 | + |
| 115 | + await That(sut.ToString()).IsEqualTo("[first, second]"); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public async Task ToString_WithNullMatcher_ShouldRenderNullToken() |
| 120 | + { |
| 121 | + ParamsArrayParameterMatch<int> sut = new(new StubParameter(true, "first"), null!); |
| 122 | + |
| 123 | + await That(sut.ToString()).IsEqualTo("[first, null]"); |
| 124 | + } |
| 125 | + |
| 126 | + private sealed class StubParameter(bool matches, string label) : IParameter<int> |
| 127 | + { |
| 128 | + public List<int> MatchedValues { get; } = []; |
| 129 | + public List<int> InvokedValues { get; } = []; |
| 130 | + |
| 131 | + public bool Matches(object? value) |
| 132 | + { |
| 133 | + MatchedValues.Add((int)value!); |
| 134 | + return matches; |
| 135 | + } |
| 136 | + |
| 137 | + public void InvokeCallbacks(object? value) => InvokedValues.Add((int)value!); |
| 138 | + |
| 139 | + public override string ToString() => label; |
| 140 | + } |
| 141 | +} |
0 commit comments