From 9055ff2313b5a10ae5238f01000ccffbe8748e5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Zaj=C4=85c?= Date: Tue, 5 Dec 2023 16:50:24 +0100 Subject: [PATCH] Stryker tests improvements (#89) * Provide Stryker tests improvements * Disable Stryker for ordering by random line of code --- .../Attributes/ExceptAttributeTests.cs | 3 +- .../Attributes/PickFromRangeAttributeTests.cs | 3 +- .../PickFromValuesAttributeTests.cs | 3 +- .../Common/RoundRobinEnumerableTests.cs | 3 +- .../Requests/ValuesRequestTests.cs | 30 +++++++++++++++---- .../RandomExceptValuesGeneratorTests.cs | 3 +- .../RequestFactoryRelayTests.cs | 4 +-- .../RandomFixedValuesGenerator.cs | 2 ++ 8 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/ExceptAttributeTests.cs b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/ExceptAttributeTests.cs index c0c9df5d..f3ebb162 100644 --- a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/ExceptAttributeTests.cs +++ b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/ExceptAttributeTests.cs @@ -58,7 +58,8 @@ public void GivenNoArguments_WhenConstructorIsInvoked_ThenExceptionIsThrown() // Arrange // Act // Assert - Assert.Throws(() => new ExceptAttribute()); + var exception = Assert.Throws(() => new ExceptAttribute()); + exception.Message.Should().NotBeNullOrEmpty().And.Contain("is expected"); } [InlineData(1, 1)] diff --git a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/PickFromRangeAttributeTests.cs b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/PickFromRangeAttributeTests.cs index d411ec10..63f664ce 100644 --- a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/PickFromRangeAttributeTests.cs +++ b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/PickFromRangeAttributeTests.cs @@ -31,7 +31,8 @@ public void GivenMinimumGreaterThanMaximum_WhenConstructorIsInvoked_ThenExceptio // Act // Assert - Assert.Throws(() => new PickFromRangeAttribute(min, max)); + var exception = Assert.Throws(() => new PickFromRangeAttribute(min, max)); + exception.Message.Should().NotBeNullOrEmpty().And.Contain("must be lower or equal"); } [Fact(DisplayName = "GIVEN valid parameters WHEN constructor is invoked THEN parameters are properly assigned")] diff --git a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/PickFromValuesAttributeTests.cs b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/PickFromValuesAttributeTests.cs index 37134f74..942aa134 100644 --- a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/PickFromValuesAttributeTests.cs +++ b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Attributes/PickFromValuesAttributeTests.cs @@ -65,7 +65,8 @@ public void GivenNoArguments_WhenConstructorIsInvoked_ThenExceptionIsThrown() // Arrange // Act // Assert - Assert.Throws(() => new PickFromValuesAttribute()); + var exception = Assert.Throws(() => new PickFromValuesAttribute()); + exception.Message.Should().NotBeNullOrEmpty().And.Contain("is expected"); } [InlineData(1, 1)] diff --git a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Common/RoundRobinEnumerableTests.cs b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Common/RoundRobinEnumerableTests.cs index 7125380b..58ac5f20 100644 --- a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Common/RoundRobinEnumerableTests.cs +++ b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Common/RoundRobinEnumerableTests.cs @@ -31,7 +31,8 @@ public void GivenNoArguments_WhenConstructorIsInvoked_ThenExceptionIsThrown() // Arrange // Act // Assert - Assert.Throws(() => new RoundRobinEnumerable()); + var exception = Assert.Throws(() => new RoundRobinEnumerable()); + exception.Message.Should().NotBeNullOrEmpty().And.Contain("is expected"); } [AutoData] diff --git a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Requests/ValuesRequestTests.cs b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Requests/ValuesRequestTests.cs index 6ce13611..487dd7d0 100644 --- a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Requests/ValuesRequestTests.cs +++ b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/Requests/ValuesRequestTests.cs @@ -60,7 +60,8 @@ public void GivenEmptyArgument_WhenConstructorIsInvoked_ThenExceptionIsThrown() // Act // Assert - Assert.Throws(() => new FixedValuesRequest(type, values)); + var exception = Assert.Throws(() => new FixedValuesRequest(type, values)); + exception.Message.Should().NotBeNullOrEmpty().And.Contain("is expected"); } [InlineData(typeof(int), 2)] @@ -97,9 +98,9 @@ public void GivenValidArguments_WhenToStringIsInvoked_ThenTextConteinsNecessaryI text.Should().Contain(nameof(FixedValuesRequest)) .And.Contain(type.Name) .And.Contain("[Int32]") - .And.Contain(first.ToString(CultureInfo.InvariantCulture)) + .And.Contain($"{first.ToString(CultureInfo.InvariantCulture)},") .And.Contain("[Int64]") - .And.Contain(second.Value.ToString(CultureInfo.InvariantCulture)) + .And.Contain($"{second.Value.ToString(CultureInfo.InvariantCulture)},") .And.Contain("[Object]") .And.Contain("null"); } @@ -195,8 +196,8 @@ public void GivenDifferentTypeOfValuesRequest_WhenEqualsIsInvoked_ThenFalseIsRet } [AutoData] - [Theory(DisplayName = "GIVEN different type of ValuesRequest WHEN GetHashCode is invoked THEN False is returned")] - public void GivenDifferentTypeOfValuesRequest_WhenGetHashCodeIsInvoked_ThenFalseIsReturned( + [Theory(DisplayName = "GIVEN different type of ValuesRequest WHEN hashcodes are compared THEN hashcodes are not equal")] + public void GivenDifferentTypeOfValuesRequest_WhenHashCodesAreCompared_ThenHashCodesAreNotEqual( int value) { // Arrange @@ -212,5 +213,24 @@ public void GivenDifferentTypeOfValuesRequest_WhenGetHashCodeIsInvoked_ThenFalse // Assert result.Should().Be(false); } + + [InlineAutoData(typeof(ExceptValuesRequest))] + [InlineAutoData(typeof(FixedValuesRequest))] + [Theory(DisplayName = "GIVEN ValuesRequest with single value WHEN GetHashCode is invoked THEN expected value is returned")] + public void GivenValuesRequestWithSingleValue_WhenGetHashCodeIsInvoked_ThenExpectedValueIsReturned( + Type requestType, + int value) + { + // Arrange + var valueType = value.GetType(); + var request = Activator.CreateInstance(requestType, valueType, value); + var expectedHashCode = valueType.GetHashCode() ^ requestType.GetHashCode() ^ value.GetHashCode(); + + // Act + var actualHashCode = request.GetHashCode(); + + // Assert + actualHashCode.Should().Be(expectedHashCode); + } } } diff --git a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/SpecimenBuilders/RandomExceptValuesGeneratorTests.cs b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/SpecimenBuilders/RandomExceptValuesGeneratorTests.cs index c8f24e56..86f5d962 100644 --- a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/SpecimenBuilders/RandomExceptValuesGeneratorTests.cs +++ b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/SpecimenBuilders/RandomExceptValuesGeneratorTests.cs @@ -53,7 +53,8 @@ public void GivenExcludedValueAndContextWhichResolvesToTheSameValue_WhenCreateIs // Act // Assert - Assert.Throws(() => builder.Create(request, context.Object)); + var exception = Assert.Throws(() => builder.Create(request, context.Object)); + exception.Message.Should().NotBeNullOrEmpty(); } [Fact(DisplayName = "GIVEN unsupported request WHEN Create is invoked THEN NoSpecimen is returned")] diff --git a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/SpecimenBuilders/RequestFactoryRelayTests.cs b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/SpecimenBuilders/RequestFactoryRelayTests.cs index 14612069..6578ee6b 100644 --- a/src/Objectivity.AutoFixture.XUnit2.Core.Tests/SpecimenBuilders/RequestFactoryRelayTests.cs +++ b/src/Objectivity.AutoFixture.XUnit2.Core.Tests/SpecimenBuilders/RequestFactoryRelayTests.cs @@ -110,7 +110,7 @@ public void GivenTypeRequest_WhenCreateIsInvoked_ThenExpectedTypeIsReturned( { // Arrange var factory = new Mock>(); - factory.Setup(x => x(It.IsAny())) + factory.Setup(x => x(It.IsIn(expectedType))) .Returns(x => x); var builder = new RequestFactoryRelay(factory.Object); var context = new Mock(); @@ -126,7 +126,7 @@ public void GivenTypeRequest_WhenCreateIsInvoked_ThenExpectedTypeIsReturned( // Assert result.Should().NotBeNull() .And.BeOfType(expectedType); - factory.Verify(x => x(It.IsAny()), Times.Once); + factory.Verify(x => x(It.IsIn(expectedType)), Times.Once); factory.VerifyNoOtherCalls(); context.Verify(x => x.Resolve(It.IsAny()), Times.Once); context.VerifyNoOtherCalls(); diff --git a/src/Objectivity.AutoFixture.XUnit2.Core/SpecimenBuilders/RandomFixedValuesGenerator.cs b/src/Objectivity.AutoFixture.XUnit2.Core/SpecimenBuilders/RandomFixedValuesGenerator.cs index a709766d..5e26e31a 100644 --- a/src/Objectivity.AutoFixture.XUnit2.Core/SpecimenBuilders/RandomFixedValuesGenerator.cs +++ b/src/Objectivity.AutoFixture.XUnit2.Core/SpecimenBuilders/RandomFixedValuesGenerator.cs @@ -34,6 +34,8 @@ public object Create(object request, ISpecimenContext context) private static IEnumerator CreateEnumerable(FixedValuesRequest request) { var random = new Random(); + + // Stryker disable once all : mutating ordering by random still brings random results var values = request.Values.OrderBy((_) => random.Next()).ToArray(); return new RoundRobinEnumerable(values).GetEnumerator();