|
| 1 | +using System; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using FluentAssertions; |
| 4 | +using Light.GuardClauses.Exceptions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Light.GuardClauses.Tests.CollectionAssertions; |
| 8 | + |
| 9 | +public static class MustHaveMinimumLengthTests |
| 10 | +{ |
| 11 | + [Theory] |
| 12 | + [InlineData(new[] { 1, 2 }, 3)] |
| 13 | + [InlineData(new[] { 1 }, 2)] |
| 14 | + [InlineData(new int[] { }, 1)] |
| 15 | + public static void ImmutableArrayFewerItems(int[] items, int length) |
| 16 | + { |
| 17 | + var immutableArray = items.ToImmutableArray(); |
| 18 | + Action act = () => immutableArray.MustHaveMinimumLength(length, nameof(immutableArray)); |
| 19 | + |
| 20 | + var assertion = act.Should().Throw<InvalidCollectionCountException>().Which; |
| 21 | + assertion.Message.Should().Contain( |
| 22 | + $"{nameof(immutableArray)} must have at least a length of {length}, but it actually has a length of {immutableArray.Length}." |
| 23 | + ); |
| 24 | + assertion.ParamName.Should().BeSameAs(nameof(immutableArray)); |
| 25 | + } |
| 26 | + |
| 27 | + [Theory] |
| 28 | + [InlineData(new[] { "Foo" }, 1)] |
| 29 | + [InlineData(new[] { "Bar" }, 0)] |
| 30 | + [InlineData(new[] { "Baz", "Qux", "Quux" }, 2)] |
| 31 | + public static void ImmutableArrayMoreOrEqualItems(string[] items, int length) |
| 32 | + { |
| 33 | + var immutableArray = items.ToImmutableArray(); |
| 34 | + var result = immutableArray.MustHaveMinimumLength(length); |
| 35 | + result.Should().Equal(immutableArray); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public static void ImmutableArrayEmpty() |
| 40 | + { |
| 41 | + var emptyArray = ImmutableArray<int>.Empty; |
| 42 | + var result = emptyArray.MustHaveMinimumLength(0); |
| 43 | + result.Should().Equal(emptyArray); |
| 44 | + } |
| 45 | + |
| 46 | + [Theory] |
| 47 | + [InlineData(new[] { 87 }, 3)] |
| 48 | + [InlineData(new[] { 1, 2 }, 5)] |
| 49 | + public static void ImmutableArrayCustomException(int[] items, int minimumLength) |
| 50 | + { |
| 51 | + var immutableArray = items.ToImmutableArray(); |
| 52 | + |
| 53 | + Action act = () => immutableArray.MustHaveMinimumLength( |
| 54 | + minimumLength, |
| 55 | + (array, length) => new ($"Custom exception for array with length {array.Length} and min {length}") |
| 56 | + ); |
| 57 | + |
| 58 | + act.Should().Throw<Exception>() |
| 59 | + .WithMessage($"Custom exception for array with length {immutableArray.Length} and min {minimumLength}"); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public static void ImmutableArrayNoCustomExceptionThrown() |
| 64 | + { |
| 65 | + var immutableArray = new[] { "Foo", "Bar" }.ToImmutableArray(); |
| 66 | + var result = immutableArray.MustHaveMinimumLength(2, (_, _) => new ()); |
| 67 | + result.Should().Equal(immutableArray); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public static void ImmutableArrayCustomMessage() |
| 72 | + { |
| 73 | + var immutableArray = new[] { 1 }.ToImmutableArray(); |
| 74 | + |
| 75 | + Test.CustomMessage<InvalidCollectionCountException>( |
| 76 | + message => immutableArray.MustHaveMinimumLength(3, message: message) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public static void ImmutableArrayCallerArgumentExpression() |
| 82 | + { |
| 83 | + var myImmutableArray = new[] { 1 }.ToImmutableArray(); |
| 84 | + |
| 85 | + var act = () => myImmutableArray.MustHaveMinimumLength(2); |
| 86 | + |
| 87 | + act.Should().Throw<InvalidCollectionCountException>() |
| 88 | + .WithParameterName(nameof(myImmutableArray)); |
| 89 | + } |
| 90 | + |
| 91 | + [Theory] |
| 92 | + [InlineData(0)] |
| 93 | + [InlineData(-1)] |
| 94 | + [InlineData(-5)] |
| 95 | + public static void |
| 96 | + DefaultImmutableArrayInstanceShouldNotThrowWhenLengthIsLessOrEqualToZero(int validLength) => |
| 97 | + default(ImmutableArray<int>).MustHaveMinimumLength(validLength).IsDefault.Should().BeTrue(); |
| 98 | + |
| 99 | + [Theory] |
| 100 | + [InlineData(1)] |
| 101 | + [InlineData(5)] |
| 102 | + [InlineData(12)] |
| 103 | + public static void DefaultImmutableArrayInstanceShouldThrowWhenLengthIsPositive(int positiveLength) |
| 104 | + { |
| 105 | + var act = () => default(ImmutableArray<int>).MustHaveMinimumLength(positiveLength); |
| 106 | + |
| 107 | + act.Should().Throw<InvalidCollectionCountException>() |
| 108 | + .WithParameterName("default(ImmutableArray<int>)") |
| 109 | + .WithMessage( |
| 110 | + $"default(ImmutableArray<int>) must have at least a length of {positiveLength}, but it actually has no length because it is the default instance.*" |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + [Theory] |
| 115 | + [InlineData(0)] |
| 116 | + [InlineData(-1)] |
| 117 | + [InlineData(-5)] |
| 118 | + public static void DefaultImmutableArrayInstanceCustomExceptionShouldNotThrow(int validLength) |
| 119 | + { |
| 120 | + var result = default(ImmutableArray<int>).MustHaveMinimumLength(validLength, (_, _) => new Exception()); |
| 121 | + result.IsDefault.Should().BeTrue(); |
| 122 | + } |
| 123 | + |
| 124 | + [Theory] |
| 125 | + [InlineData(1)] |
| 126 | + [InlineData(5)] |
| 127 | + [InlineData(12)] |
| 128 | + public static void DefaultImmutableArrayInstanceCustomExceptionShouldThrow(int positiveLength) |
| 129 | + { |
| 130 | + var act = () => default(ImmutableArray<int>).MustHaveMinimumLength( |
| 131 | + positiveLength, |
| 132 | + (array, length) => new ArgumentException( |
| 133 | + $"Custom: Array length {(array.IsDefault ? 0 : array.Length)} is below minimum {length}" |
| 134 | + ) |
| 135 | + ); |
| 136 | + |
| 137 | + act.Should().Throw<ArgumentException>() |
| 138 | + .WithMessage("Custom: Array length 0 is below minimum *"); |
| 139 | + } |
| 140 | +} |
0 commit comments