Exemplo n.º 1
0
        public void Can_Create_From_Integer(int?statusCode)
        {
            StatusCodeRange range = statusCode;

            range.From.Should().Be(statusCode);
            range.To.Should().Be(statusCode);
        }
Exemplo n.º 2
0
        public void Initializes_From_And_To(int?from, int?to)
        {
            var range = new StatusCodeRange(from, to);

            range.From.Should().Be(from);
            range.To.Should().Be(to);
        }
Exemplo n.º 3
0
        public void LessThan_Returned_For_Less_Specific_Range(StatusCodeRange x, StatusCodeRange y)
        {
            var comparer = new StatusCodeRangeSpecificnessComparer();
            var result   = comparer.Compare(x, y);

            result.Should().BeLessThan(0);
        }
Exemplo n.º 4
0
        public void Can_Create_From_Tuple(int?from, int?to)
        {
            StatusCodeRange range = (from, to);

            range.From.Should().Be(from);
            range.To.Should().Be(to);
        }
Exemplo n.º 5
0
        public void Equal_Returned_For_Conflicting_Ranges(StatusCodeRange x, StatusCodeRange y)
        {
            var comparer = new StatusCodeRangeSpecificnessComparer();
            var result   = comparer.Compare(x, y);

            result.Should().Be(0);
        }
Exemplo n.º 6
0
        public void GreaterThan_Returned_For_More_Specific_Range(StatusCodeRange x, StatusCodeRange y)
        {
            var comparer = new StatusCodeRangeSpecificnessComparer();
            var result   = comparer.Compare(y, x);

            result.Should().BeGreaterThan(0);
        }
Exemplo n.º 7
0
        public void Returns_False_For_Other_Range(int?outerFrom, int?outerTo, int?innerFrom, int?innerTo)
        {
            var outer = new StatusCodeRange(outerFrom, outerTo);
            var inner = new StatusCodeRange(innerFrom, innerTo);

            outer.IsInRange(inner).Should().BeFalse();
        }
        /// <summary>
        ///     Returns a value indicating whether this status code range conflicts with the
        ///     specified one.
        ///     Two ranges conflict with each other if there is no reasonable way to decide
        ///     which range matches a specific status code better.
        /// </summary>
        /// <param name="range">This status code range.</param>
        /// <param name="other">The status code range to be compared with this one.</param>
        /// <returns>
        ///     <see langword="true"/> if the two ranges conflict with each other;
        ///     <see langword="false"/> if not.
        /// </returns>
        public static bool ConflictsWith(this StatusCodeRange range, StatusCodeRange other)
        {
            return(AreSame() ||
                   HaveStandardRangeConflicts() ||
                   HaveWildcardRangeConflict());

            bool AreSame() =>
            range == other;

            // Standard conflicts for ranges without wildcard components. Examples:
            //
            // - (200, 205) and (205, 210) conflict
            // - (200, 210) and (205, 210) conflict
            //
            // - (200, 205) and (210, 220) don't conflict because they cover different ranges.
            // - (200, 210) and (205) don't conflict because (205) is more specific.
            // - (200, 300) and (250, 300) don't conflict, because (250, 300) is more specific.
            bool HaveStandardRangeConflicts() =>
            !range.IsSingleStatusCode && !other.IsSingleStatusCode &&
            !range.HasWildcardComponent && !other.HasWildcardComponent &&
            !range.IsInRange(other) && !other.IsInRange(range) &&
            range.From <= other.To && other.From <= range.To;

            // Wildcard conflicts for ranges with wildcards on opposite sides. Examples:
            //
            // - (*, 200) and (200, *) conflict
            // - (200, *) and (*, 200) conflict
            //
            // - (*, 200) and (*, 205) don't conflict because (*, 200) is more specific.
            // - (200, *) and (205, *) don't conflict because (205, *) is more specific.
            bool HaveWildcardRangeConflict() =>
            range != StatusCodeRange.All && other != StatusCodeRange.All &&
            ((range is (null, _) && other is (_, null) && range.To >= other.From) ||
             (range is (_, null) && other is (null, _) && other.To >= range.From));
        }
        /// <summary>
        ///     Returns a value indicating whether this status code range can be considered
        ///     more specific than the <paramref name="other"/> specified one.
        ///     Specific means that the range defines a "tighter" area of status codes.
        ///     For example, <c>(200, 250)</c> is more specific than <c>(200, 300)</c>.
        ///
        ///     If the two ranges are not comparable, this returns <see langword="false"/>.
        /// </summary>
        /// <param name="range">This status code range.</param>
        /// <param name="other">The status code range to be compared with this one.</param>
        /// <returns>
        ///     <see langword="true"/> if this range is more specific than the other one;
        ///     <see langword="false"/> if not.
        /// </returns>
        public static bool IsMoreSpecificThan(this StatusCodeRange range, StatusCodeRange other)
        {
            // Short circuit which may save a lot of comparisons. Also required for certain edge cases.
            if (range == other)
            {
                return(false);
            }

            return(IsSingleAndOtherNot() ||
                   IsStandardAndOtherNot() ||
                   IsOneSidedWildcardAndOtherNot() ||
                   WinsAsMoreSpecificStandardRange() ||
                   WinsAsMoreSpecificLeftSidedWildcard() ||
                   WinsAsMoreSpecificRightSidedWildcard());

            bool IsSingleAndOtherNot() =>
            range.IsSingleStatusCode && !other.IsSingleStatusCode;

            bool IsStandardAndOtherNot() =>
            range.IsStandardRange() && other.HasWildcardComponent;

            bool IsOneSidedWildcardAndOtherNot() =>
            range.IsOneSidedWildcard() && other == StatusCodeRange.All;

            bool WinsAsMoreSpecificStandardRange() =>
            range.IsStandardRange() && other.IsStandardRange() && other.IsInRange(range);

            bool WinsAsMoreSpecificLeftSidedWildcard() =>
            range.IsLeftSidedWildcard() && other.IsLeftSidedWildcard() && range.To < other.To;

            bool WinsAsMoreSpecificRightSidedWildcard() =>
            range.IsRightSidedWildcard() && other.IsRightSidedWildcard() && range.From > other.From;
        }
Exemplo n.º 10
0
        public void Initializes_From_And_To_With_Single_Status_Code(int?statusCode)
        {
            var range = new StatusCodeRange(statusCode);

            range.From.Should().Be(statusCode);
            range.To.Should().Be(statusCode);
        }
Exemplo n.º 11
0
        public void Throws_ArgumentException_For_ForStatusCodes()
        {
            var    statusCodes = new StatusCodeRange[0];
            Action testCode    = () => Build(DefaultRequest, DefaultResponseType, DefaultResponseDeserializer, statusCodes);

            testCode.Should().Throw <ArgumentException>();
        }
Exemplo n.º 12
0
        public void Deconstructs_Into_From_And_To(int?from, int?to)
        {
            var range = new StatusCodeRange(from, to);

            range.Deconstruct(out var newFrom, out var newTo);
            newFrom.Should().Be(from);
            newTo.Should().Be(to);
        }
Exemplo n.º 13
0
        public void False_For_Unequal_Ranges(int?xFrom, int?xTo, int?yFrom, int?yTo)
        {
            var x = new StatusCodeRange(xFrom, xTo);
            var y = new StatusCodeRange(yFrom, yTo);

            x.Equals(y).Should().BeFalse();
            x.Equals((object)y).Should().BeFalse();
            (x == y).Should().BeFalse();
            (!(x != y)).Should().BeFalse();
            x.GetHashCode().Should().NotBe(y.GetHashCode());
        }
        public void CreateRange_WithOutsideExpectedThresholds_IsWithinRangeFails()
        {
            // Arrange
            int statusCode = BogusGenerator.Random.Int(min: 200, max: 299);
            var range      = new StatusCodeRange(500, 599);

            // Act
            bool isWithinRange = range.IsWithinRange(statusCode);

            // Assert
            Assert.False(isWithinRange);
        }
        public void CreateRange_WithExpectedThresholds_IsWithinRangeSucceeds()
        {
            // Arrange
            int statusCode = BogusGenerator.Random.Int(min: 500, max: 599);
            var range      = new StatusCodeRange(500, 599);

            // Act
            bool isWithinRange = range.IsWithinRange(statusCode);

            // Assert
            Assert.True(isWithinRange);
        }
        public void CreateRange_WithOutsideExpectedSingleThreshold_IsWithinRangeFails()
        {
            // Arrange
            int threshold  = BogusGenerator.Random.Int(400, 599);
            int statusCode = BogusGenerator.Random.Int(200, 399);
            var range      = new StatusCodeRange(threshold);

            // Act
            bool isWithinRange = range.IsWithinRange(statusCode);

            // Assert
            Assert.False(isWithinRange);
        }
Exemplo n.º 17
0
        public void Returns_False_For_Single_Status_Codes(int?rangeFrom, int?rangeTo, int statusCode)
        {
            var range = new StatusCodeRange(rangeFrom, rangeTo);

            range.IsInRange(statusCode).Should().BeFalse();
        }
Exemplo n.º 18
0
 public void Returns_True_For_Conflicting_Ranges(StatusCodeRange x, StatusCodeRange y)
 {
     x.ConflictsWith(y).Should().BeTrue();
     y.ConflictsWith(x).Should().BeTrue();
 }
Exemplo n.º 19
0
 public void Returns_False_For_Non_Conflicting_Ranges(StatusCodeRange x, StatusCodeRange y)
 {
     x.ConflictsWith(y).Should().BeFalse();
     y.ConflictsWith(x).Should().BeFalse();
 }
Exemplo n.º 20
0
 public void Have_Expected_Values(StatusCodeRange range, int?expectedFrom, int?expectedTo)
 {
     range.From.Should().Be(expectedFrom);
     range.To.Should().Be(expectedTo);
 }
Exemplo n.º 21
0
 public void Returns_False_For_Equal_And_Conflicting_Ranges(StatusCodeRange x, StatusCodeRange y)
 {
     x.IsMoreSpecificThan(y).Should().BeFalse();
 }
Exemplo n.º 22
0
 public void Returns_False_For_Less_Specific_Range(StatusCodeRange x, StatusCodeRange y)
 {
     x.IsMoreSpecificThan(y).Should().BeFalse();
 }
Exemplo n.º 23
0
 public void Returns_True_For_More_Specific_Range(StatusCodeRange x, StatusCodeRange y)
 {
     // By just reversing the less specific data, we should get a valid test result.
     y.IsMoreSpecificThan(x).Should().BeTrue();
 }