Skip to content

Commit

Permalink
Stryker tests improvements (#89)
Browse files Browse the repository at this point in the history
* Provide Stryker tests improvements
* Disable Stryker for ordering by random line of code
  • Loading branch information
piotrzajac authored Dec 5, 2023
1 parent 63a6f5c commit 9055ff2
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public void GivenNoArguments_WhenConstructorIsInvoked_ThenExceptionIsThrown()
// Arrange
// Act
// Assert
Assert.Throws<ArgumentException>(() => new ExceptAttribute());
var exception = Assert.Throws<ArgumentException>(() => new ExceptAttribute());
exception.Message.Should().NotBeNullOrEmpty().And.Contain("is expected");
}

[InlineData(1, 1)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void GivenMinimumGreaterThanMaximum_WhenConstructorIsInvoked_ThenExceptio

// Act
// Assert
Assert.Throws<ArgumentOutOfRangeException>(() => new PickFromRangeAttribute(min, max));
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => 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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void GivenNoArguments_WhenConstructorIsInvoked_ThenExceptionIsThrown()
// Arrange
// Act
// Assert
Assert.Throws<ArgumentException>(() => new PickFromValuesAttribute());
var exception = Assert.Throws<ArgumentException>(() => new PickFromValuesAttribute());
exception.Message.Should().NotBeNullOrEmpty().And.Contain("is expected");
}

[InlineData(1, 1)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void GivenNoArguments_WhenConstructorIsInvoked_ThenExceptionIsThrown()
// Arrange
// Act
// Assert
Assert.Throws<ArgumentException>(() => new RoundRobinEnumerable<object>());
var exception = Assert.Throws<ArgumentException>(() => new RoundRobinEnumerable<object>());
exception.Message.Should().NotBeNullOrEmpty().And.Contain("is expected");
}

[AutoData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public void GivenEmptyArgument_WhenConstructorIsInvoked_ThenExceptionIsThrown()

// Act
// Assert
Assert.Throws<ArgumentException>(() => new FixedValuesRequest(type, values));
var exception = Assert.Throws<ArgumentException>(() => new FixedValuesRequest(type, values));
exception.Message.Should().NotBeNullOrEmpty().And.Contain("is expected");
}

[InlineData(typeof(int), 2)]
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void GivenExcludedValueAndContextWhichResolvesToTheSameValue_WhenCreateIs

// Act
// Assert
Assert.Throws<ObjectCreationException>(() => builder.Create(request, context.Object));
var exception = Assert.Throws<ObjectCreationException>(() => builder.Create(request, context.Object));
exception.Message.Should().NotBeNullOrEmpty();
}

[Fact(DisplayName = "GIVEN unsupported request WHEN Create is invoked THEN NoSpecimen is returned")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void GivenTypeRequest_WhenCreateIsInvoked_ThenExpectedTypeIsReturned(
{
// Arrange
var factory = new Mock<Func<Type, object>>();
factory.Setup(x => x(It.IsAny<Type>()))
factory.Setup(x => x(It.IsIn(expectedType)))
.Returns<Type>(x => x);
var builder = new RequestFactoryRelay(factory.Object);
var context = new Mock<ISpecimenContext>();
Expand All @@ -126,7 +126,7 @@ public void GivenTypeRequest_WhenCreateIsInvoked_ThenExpectedTypeIsReturned(
// Assert
result.Should().NotBeNull()
.And.BeOfType(expectedType);
factory.Verify(x => x(It.IsAny<Type>()), Times.Once);
factory.Verify(x => x(It.IsIn(expectedType)), Times.Once);
factory.VerifyNoOtherCalls();
context.Verify(x => x.Resolve(It.IsAny<object>()), Times.Once);
context.VerifyNoOtherCalls();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>(values).GetEnumerator();
Expand Down

0 comments on commit 9055ff2

Please sign in to comment.