Skip to content

Commit

Permalink
Bring code in line with latest Code Analysis rules
Browse files Browse the repository at this point in the history
  • Loading branch information
natsnudasoft committed Apr 3, 2019
1 parent 492cb97 commit 9248117
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 27 deletions.
Binary file modified _CodeAnalysis.docx
Binary file not shown.
12 changes: 12 additions & 0 deletions src/NatsnudaLibrary/IRandom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public interface IRandom
/// </summary>
/// <returns>A 32-bit signed integer that is greater than or equal to 0 and less than
/// <see cref="int.MaxValue"/>.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Naming",
"CA1716:IdentifiersShouldNotMatchKeywords",
Justification = "Matching existing interface with reserved language keyword.")]
int Next();

/// <summary>
Expand All @@ -35,6 +39,10 @@ public interface IRandom
/// </param>
/// <returns>A 32-bit signed integer that is greater than or equal to 0, and less than
/// <paramref name="maxValue"/>.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Naming",
"CA1716:IdentifiersShouldNotMatchKeywords",
Justification = "Matching existing interface with reserved language keyword.")]
int Next(int maxValue);

/// <summary>
Expand All @@ -46,6 +54,10 @@ public interface IRandom
/// </param>
/// <returns>A 32-bit signed integer that is greater than or equal to
/// <paramref name="minValue"/>, and less than <paramref name="maxValue"/>.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Naming",
"CA1716:IdentifiersShouldNotMatchKeywords",
Justification = "Matching existing interface with reserved language keyword.")]
int Next(int minValue, int maxValue);

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/NatsnudaLibrary/NatsnudaLibrary.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
</Rules>
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
<Rule Id="IDE0003" Action="None" />
<Rule Id="IDE0041" Action="None" />
</Rules>
</RuleSet>
8 changes: 8 additions & 0 deletions src/NatsnudaLibrary/SystemDateTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ namespace Natsnudasoft.NatsnudaLibrary
public class SystemDateTimeProvider : IDateTimeProvider
{
/// <inheritdoc/>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Menees.Analyzers",
"MEN013:UseUTCTime",
Justification = "Matching existing implementation.")]
public DateTime Now => DateTime.Now;

/// <inheritdoc/>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Menees.Analyzers",
"MEN013:UseUTCTime",
Justification = "Matching existing implementation.")]
public DateTime Today => DateTime.Today;

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ protected override void VerifyEqualsMethod(MethodInfo methodInfo)
methodInfo.ReflectedType.Name,
i));
}
else if (methodInfo.GetParameters().Single().ParameterType
.IsAssignableFrom(right.GetType()))
else if (methodInfo.GetParameters().Single().ParameterType.IsInstanceOfType(right))
{
var expectedResult = equalsOverrideTheory.ExpectedResult;
var resultsMatchExpected = Enumerable.Range(0, this.SuccessiveCount)
Expand Down
4 changes: 4 additions & 0 deletions src/TestExtensions/ExceptionBehaviorExpectation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public class ExceptionBehaviorExpectation<T> : IBehaviorExpectation
/// <param name="command">The command whose behaviour must be examined.</param>
/// <exception cref="ArgumentNullException"><paramref name="command"/> is
/// <see langword="null"/>.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1031:DoNotCatchGeneralExceptionTypes",
Justification = "Exception is propagated by guard clause.")]
public void Verify(IGuardClauseCommand command)
{
ParameterValidation.IsNotNull(command, nameof(command));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public ParameterNullReferenceBehaviorExpectation(ISpecimenBuilder specimenBuilde
/// <param name="command">The command whose behaviour must be examined.</param>
/// <exception cref="ArgumentNullException"><paramref name="command"/> is
/// <see langword="null"/>.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1031:DoNotCatchGeneralExceptionTypes",
Justification = "Exception is propagated by guard clause.")]
public void Verify(IGuardClauseCommand command)
{
ParameterValidation.IsNotNull(command, nameof(command));
Expand Down
2 changes: 1 addition & 1 deletion src/TestExtensions/ParameterSpecimenBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public object Create(object request, ISpecimenContext context)
var parameterInfo = request as ParameterInfo;
if (parameterInfo != null &&
parameterInfo.Member.DeclaringType == this.DeclaringType &&
parameterInfo.ParameterType.IsAssignableFrom(this.SpecimenValue.GetType()) &&
parameterInfo.ParameterType.IsInstanceOfType(this.SpecimenValue) &&
parameterInfo.Name == this.ParameterName)
{
specimen = this.SpecimenValue;
Expand Down
23 changes: 17 additions & 6 deletions src/TestExtensions/PropertyChangedRaisedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ namespace Natsnudasoft.NatsnudaLibrary.TestExtensions
[Serializable]
public sealed class PropertyChangedRaisedException : Exception
{
private const string PropertyInfoTypeName = nameof(PropertyInfo) + "TypeName";
private const string PropertyInfoMemberName = nameof(PropertyInfo) + "MemberName";
private const string DefaultMessage = "A property did not raise the PropertyChanged event.";

[NonSerialized]
private readonly PropertyInfo propertyInfo;

/// <summary>
/// Initializes a new instance of the <see cref="PropertyChangedRaisedException"/> class.
/// </summary>
Expand Down Expand Up @@ -75,7 +80,7 @@ public PropertyChangedRaisedException(string message, Exception innerException)
public PropertyChangedRaisedException(PropertyInfo propertyInfo)
: base(CreateMessage(propertyInfo))
{
this.PropertyInfo = propertyInfo;
this.propertyInfo = propertyInfo;
}

/// <summary>
Expand All @@ -90,29 +95,35 @@ public PropertyChangedRaisedException(PropertyInfo propertyInfo)
Exception innerException)
: base(CreateMessage(propertyInfo), innerException)
{
this.PropertyInfo = propertyInfo;
this.propertyInfo = propertyInfo;
}

[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
private PropertyChangedRaisedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
this.PropertyInfo = (PropertyInfo)info
.GetValue(nameof(this.PropertyInfo), typeof(PropertyInfo));
var typeName = info.GetString(PropertyInfoTypeName);
var memberName = info.GetString(PropertyInfoMemberName);
const BindingFlags bindingFlags = BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic;
this.propertyInfo = Type.GetType(typeName).GetProperty(memberName, bindingFlags);
}

/// <summary>
/// Gets the property information for the property that did correctly raise the
/// PropertyChanged event.
/// </summary>
public PropertyInfo PropertyInfo { get; }
public PropertyInfo PropertyInfo => this.propertyInfo;

/// <inheritdoc/>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(this.PropertyInfo), this.PropertyInfo);
info.AddValue(PropertyInfoTypeName, this.PropertyInfo.DeclaringType.FullName);
info.AddValue(PropertyInfoMemberName, this.PropertyInfo.Name);
}

private static string CreateMessage(PropertyInfo propertyInfo)
Expand Down
1 change: 1 addition & 0 deletions src/TestExtensions/TestExtensions.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
</Rules>
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
<Rule Id="IDE0003" Action="None" />
<Rule Id="IDE0041" Action="None" />
</Rules>
</RuleSet>
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void VerifyWithMethodThatIsNotEqualsMethodIsNotInvoked()
{
var sutMock = new Mock<EqualsOverrideAssertion>(new Mock<ISpecimenBuilder>().Object)
{
CallBase = true
CallBase = true,
};
var sut = sutMock.Object;

Expand All @@ -105,7 +105,7 @@ public void VerifyWithMethodThatIsEqualsMethodIsInvoked()
{
var sutMock = new Mock<EqualsOverrideAssertion>(new Mock<ISpecimenBuilder>().Object)
{
CallBase = true
CallBase = true,
};
var sut = sutMock.Object;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void VerifyEqualsMethodWithNonObjectParameterTypeDoesNotInvokeEquals()

var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, true, false)
{
CallBase = true
CallBase = true,
};
fixture.Inject(verifyHelperMock.Object);
var sut = fixture.Create<SutAlias>();
Expand Down Expand Up @@ -97,7 +97,7 @@ public void VerifyEqualsMethodCorrectBehaviourInvokesEquals()

var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, true, false)
{
CallBase = true
CallBase = true,
};
fixture.Inject(verifyHelperMock.Object);
var sut = fixture.Create<SutAlias>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void VerifyEqualsMethodWithNonNullableParameterTypeDoesNotInvokeEquals()
var fixture = new Fixture();
var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, true, false)
{
CallBase = true
CallBase = true,
};
fixture.Inject(verifyHelperMock.Object);
var sut = fixture.Create<SutAlias>();
Expand Down Expand Up @@ -96,7 +96,7 @@ public void VerifyEqualsMethodCorrectBehaviourInvokesEquals()
var fixture = new Fixture();
var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, true, false)
{
CallBase = true
CallBase = true,
};
fixture.Inject(verifyHelperMock.Object);
var sut = fixture.Create<SutAlias>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void VerifyEqualsMethodWithObjectParameterTypeDoesNotInvokeEquals()
var fixture = new Fixture();
var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, true, false)
{
CallBase = true
CallBase = true,
};
fixture.Inject(verifyHelperMock.Object);
var sut = fixture.Create<SutAlias>();
Expand All @@ -88,7 +88,7 @@ public void VerifyEqualsMethodViolatesEqualsSuccessiveContractThrows()
new EqualsOverrideAssertionVerifyHelper(
false,
false,
() => equalsResultQueue.Dequeue())
() => equalsResultQueue.Dequeue()),
});
fixture.Register(() => verifyHelperQueue.Dequeue());
var sut = fixture.Create<SutAlias>();
Expand All @@ -106,12 +106,12 @@ public void VerifyEqualsMethodCorrectBehaviourInvokesEquals()
var fixture = new Fixture();
var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, false, true)
{
CallBase = true
CallBase = true,
};
var verifyHelperQueue = new Queue<EqualsOverrideAssertionVerifyHelper>(new[]
{
verifyHelperMock.Object,
new EqualsOverrideAssertionVerifyHelper(false, false, true)
new EqualsOverrideAssertionVerifyHelper(false, false, true),
});
fixture.Register(() => verifyHelperQueue.Dequeue());
var sut = fixture.Create<SutAlias>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void VerifyEqualsMethodViolatesEqualsSelfContractThrows()
var fixture = new Fixture();
var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, false, true)
{
CallBase = true
CallBase = true,
};
fixture.Inject(verifyHelperMock.Object);
var sut = fixture.Create<SutAlias>();
Expand All @@ -82,7 +82,7 @@ public void VerifyEqualsMethodCorrectBehaviourInvokesEquals()
var fixture = new Fixture();
var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, true, true)
{
CallBase = true
CallBase = true,
};
fixture.Inject(verifyHelperMock.Object);
var sut = fixture.Create<SutAlias>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void VerifyEqualsMethodWithTheoryWithRightThatCannotBeAssignedDoesNotInvo
var fixture = new Fixture();
var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, true, false)
{
CallBase = true
CallBase = true,
};
var equalsOverrideTheory = new EqualsOverrideTheory(
verifyHelperMock.Object,
Expand Down Expand Up @@ -131,12 +131,12 @@ public void VerifyEqualsMethodCorrectBehaviourInvokesEquals()
var fixture = new Fixture();
var verifyHelperMock = new Mock<EqualsOverrideAssertionVerifyHelper>(false, false, true)
{
CallBase = true
CallBase = true,
};
var verifyHelperQueue = new Queue<EqualsOverrideAssertionVerifyHelper>(new[]
{
verifyHelperMock.Object,
new EqualsOverrideAssertionVerifyHelper(false, false, true)
new EqualsOverrideAssertionVerifyHelper(false, false, true),
});
fixture.Register(() => verifyHelperQueue.Dequeue());
var equalsOverrideTheory = new EqualsOverrideTheory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void ConstructorEmptyValuesThrows()
var ex = Record.Exception(() => new ExceptionBehaviorExpectation<Exception>(
new Mock<ISpecimenBuilder>().Object,
fixture.Create<string>(),
new object[] { }));
Array.Empty<object>()));

Assert.IsType<ArgumentException>(ex);
}
Expand All @@ -100,7 +100,7 @@ public void Constructor2EmptyValuesThrows()
new Mock<ISpecimenBuilder>().Object,
new Mock<IGuardClauseExtensions>().Object,
fixture.Create<string>(),
new object[] { }));
Array.Empty<object>()));

Assert.IsType<ArgumentException>(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ public void SelectMethodsConstructorFoundReturnsSingleConstructor()

var actual = sut.SelectMethods(typeof(SelectConstructorTestClass));

Assert.Equal(1, actual.Count());
Assert.Single(actual);
Assert.IsType<ConstructorMethod>(actual.First());
Assert.Equal(expected, ((ConstructorMethod)actual.First()).Constructor);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Performance",
"CA1812:AvoidUninstantiatedInternalClasses",
Justification = "Class is used via reflection.")]
private sealed class SelectConstructorTestClass
{
public SelectConstructorTestClass(int intParameter)
Expand Down

0 comments on commit 9248117

Please sign in to comment.