|
| 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 MustHaveLengthTests |
| 10 | +{ |
| 11 | + [Theory] |
| 12 | + [InlineData(3, 2)] |
| 13 | + [InlineData(5, 6)] |
| 14 | + [InlineData(0, 1)] |
| 15 | + [InlineData(10, 11)] |
| 16 | + public static void ImmutableArrayInvalidLength(int arrayLength, int expectedLength) |
| 17 | + { |
| 18 | + var array = ImmutableArray.CreateRange(new int[arrayLength]); |
| 19 | + |
| 20 | + var act = () => array.MustHaveLength(expectedLength, nameof(array)); |
| 21 | + |
| 22 | + act.Should().Throw<InvalidCollectionCountException>() |
| 23 | + .And.Message.Should().Contain( |
| 24 | + $"{nameof(array)} must have length {expectedLength}, but it actually has length {arrayLength}." |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + [Theory] |
| 29 | + [InlineData(0)] |
| 30 | + [InlineData(1)] |
| 31 | + [InlineData(3)] |
| 32 | + [InlineData(6)] |
| 33 | + public static void ImmutableArrayValidLength(int arrayLength) |
| 34 | + { |
| 35 | + var array = ImmutableArray.CreateRange(new string[arrayLength]); |
| 36 | + |
| 37 | + var result = array.MustHaveLength(arrayLength); |
| 38 | + |
| 39 | + result.Should().Equal(array); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public static void ImmutableArrayCustomException() |
| 44 | + { |
| 45 | + var exception = new Exception(); |
| 46 | + var array = ImmutableArray.Create("a", "b", "c"); |
| 47 | + |
| 48 | + var act = () => array.MustHaveLength(4, (_, _) => exception); |
| 49 | + |
| 50 | + act.Should().Throw<Exception>().Which.Should().BeSameAs(exception); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public static void ImmutableArrayNoCustomException() |
| 55 | + { |
| 56 | + var array = ImmutableArray.Create(1, 2, 3, 4); |
| 57 | + |
| 58 | + var result = array.MustHaveLength(4, (_, _) => null); |
| 59 | + |
| 60 | + result.Should().Equal(array); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public static void ImmutableArrayCustomMessage() |
| 65 | + { |
| 66 | + var array = ImmutableArray<string>.Empty; |
| 67 | + |
| 68 | + var act = () => array.MustHaveLength(1, message: "Custom error message"); |
| 69 | + |
| 70 | + act.Should().Throw<InvalidCollectionCountException>() |
| 71 | + .And.Message.Should().Contain("Custom error message"); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public static void ImmutableArrayCallerArgumentExpression() |
| 76 | + { |
| 77 | + var myArray = ImmutableArray.Create("foo", "bar"); |
| 78 | + |
| 79 | + var act = () => myArray.MustHaveLength(10); |
| 80 | + |
| 81 | + act.Should().Throw<InvalidCollectionCountException>() |
| 82 | + .WithParameterName("myArray"); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public static void ImmutableArrayDefaultArray() |
| 87 | + { |
| 88 | + var defaultArray = default(ImmutableArray<int>); |
| 89 | + |
| 90 | + var act = () => defaultArray.MustHaveLength(5, nameof(defaultArray)); |
| 91 | + |
| 92 | + act.Should().Throw<InvalidCollectionCountException>() |
| 93 | + .And.Message.Should().Contain($"{nameof(defaultArray)} must have length 5, but it actually has length 0."); |
| 94 | + } |
| 95 | +} |
0 commit comments