|
| 1 | +using System; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using FluentAssertions; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Light.GuardClauses.Tests.CollectionAssertions; |
| 7 | + |
| 8 | +public static class MustHaveLengthInTests |
| 9 | +{ |
| 10 | + [Theory] |
| 11 | + [MemberData(nameof(LengthInRangeData))] |
| 12 | + public static void LengthInRange(ImmutableArray<int> array, Range<int> range) => |
| 13 | + array.MustHaveLengthIn(range).Should().Equal(array); |
| 14 | + |
| 15 | + public static readonly TheoryData<ImmutableArray<int>, Range<int>> LengthInRangeData = |
| 16 | + new () |
| 17 | + { |
| 18 | + { [1, 2, 3], Range.FromInclusive(0).ToExclusive(10) }, |
| 19 | + { [1, 2, 3, 4, 5], Range.FromInclusive(3).ToInclusive(5) }, |
| 20 | + { ImmutableArray<int>.Empty, Range.FromInclusive(0).ToExclusive(100) }, |
| 21 | + { [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], Range.FromExclusive(5).ToInclusive(10) }, |
| 22 | + }; |
| 23 | + |
| 24 | + [Theory] |
| 25 | + [MemberData(nameof(LengthNotInRangeData))] |
| 26 | + public static void LengthNotInRange(ImmutableArray<int> array, Range<int> range) |
| 27 | + { |
| 28 | + var act = () => array.MustHaveLengthIn(range, nameof(array)); |
| 29 | + |
| 30 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 31 | + .And.Message.Should().Contain($"must have its length in between {range.CreateRangeDescriptionText("and")}") |
| 32 | + .And.Contain($"but it actually has length {array.Length}"); |
| 33 | + } |
| 34 | + |
| 35 | + public static readonly TheoryData<ImmutableArray<int>, Range<int>> LengthNotInRangeData = |
| 36 | + new () |
| 37 | + { |
| 38 | + { [1, 2, 3], Range.FromInclusive(10).ToInclusive(20) }, |
| 39 | + { [1, 2, 3, 4], Range.FromExclusive(4).ToExclusive(10) }, |
| 40 | + { ImmutableArray<int>.Empty, Range.FromInclusive(1).ToExclusive(50) }, |
| 41 | + { [1, 2], Range.FromInclusive(100).ToExclusive(256) }, |
| 42 | + }; |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public static void CustomException() => |
| 46 | + Test.CustomException( |
| 47 | + ImmutableArray.Create(1, 2, 3), |
| 48 | + Range.FromInclusive(5).ToInclusive(10), |
| 49 | + (array, r, exceptionFactory) => array.MustHaveLengthIn(r, exceptionFactory) |
| 50 | + ); |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public static void CustomMessage() => |
| 54 | + Test.CustomMessage<ArgumentOutOfRangeException>( |
| 55 | + message => ImmutableArray.Create(1, 2, 3).MustHaveLengthIn( |
| 56 | + Range.FromInclusive(42).ToInclusive(50), |
| 57 | + message: message |
| 58 | + ) |
| 59 | + ); |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public static void CallerArgumentExpression() |
| 63 | + { |
| 64 | + var testArray = ImmutableArray.Create(1, 2, 3, 4, 5); |
| 65 | + |
| 66 | + var act = () => testArray.MustHaveLengthIn(Range.FromInclusive(10).ToExclusive(20)); |
| 67 | + |
| 68 | + act.Should().Throw<ArgumentOutOfRangeException>() |
| 69 | + .WithParameterName(nameof(testArray)); |
| 70 | + } |
| 71 | +} |
0 commit comments