Skip to content

Commit

Permalink
Re-enable xunit warning 2002 dotnet#39696 (dotnet#39706)
Browse files Browse the repository at this point in the history
* Removed null checks on value types and enabled xunit 2002

* Updated Fill_OpenDataReader_Throws to use range check

Updated Fill_OpenDataReader_Throws to use range check for passing path

* Removed uneeded asserts and updated others

- Removed any assert for a valuetype where it was succeeded by a value type assertion (e.g. Assert.Equal(1, 1))
- Updated reflection tests to check flags for the specified scenario

* Update src/Microsoft.VisualBasic.Core/tests/FileSystemTests.cs

Co-Authored-By: Jeremy Barton <[email protected]>

* Update src/System.Reflection/tests/AssemblyNameTests.cs

Co-Authored-By: Jeremy Barton <[email protected]>

* Fixed failing reflection tests and removed Assert.NotNull for enum

* Pulled ruleset up from master to merge

* Cleaned up assert ordering for TestGuid

* Updated TestResponseWithoutContentEvents

- Asserted for 200 OK based on response from DefaultAzureServer + EmptyContentHandler
  • Loading branch information
zoalasaurusrawr authored and stephentoub committed Jul 26, 2019
1 parent f14a40d commit c96e389
Show file tree
Hide file tree
Showing 19 changed files with 17 additions and 39 deletions.
1 change: 0 additions & 1 deletion src/CodeAnalysis.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
<Rule Id="xUnit1024" Action="None" /> <!-- Test methods cannot have overloads -->
<Rule Id="xUnit1026" Action="None" /> <!-- Theory methods should use all of their parameters -->
<Rule Id="xUnit2000" Action="None" /> <!-- Constants and literals should be the expected argument -->
<Rule Id="xUnit2002" Action="None" /> <!-- Do not use null check on value type -->
<Rule Id="xUnit2005" Action="None" /> <!-- Do not use identity check on value type -->
<Rule Id="xUnit2006" Action="None" /> <!-- Do not use invalid string equality check -->
<Rule Id="xUnit2009" Action="None" /> <!-- Do not use boolean check to check for substrings -->
Expand Down
11 changes: 10 additions & 1 deletion src/Microsoft.VisualBasic.Core/tests/FileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
using Xunit.Sdk;

namespace Microsoft.VisualBasic.Tests
{
Expand Down Expand Up @@ -723,7 +724,15 @@ private static void AssertThrows<TException>(Action action) where TException : E
{
ex = e;
}
Assert.NotNull(ex?.GetType() == typeof(TException));
if (ex == null)
{
throw new ThrowsException(typeof(TException));
}

if (ex.GetType() != typeof(TException))
{
throw new ThrowsException(typeof(TException), ex);
}
}
}
}
4 changes: 0 additions & 4 deletions src/System.Collections.Specialized/tests/BitVector32Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ public static IEnumerable<object[]> Section_Unequal_Data()
public static void Constructor_DefaultTest()
{
BitVector32 bv = new BitVector32();
Assert.NotNull(bv);
Assert.Equal(0, bv.Data);

// Copy constructor results in item with same data.
BitVector32 copied = new BitVector32(bv);
Assert.NotNull(bv);
Assert.Equal(0, copied.Data);
}

Expand All @@ -163,12 +161,10 @@ public static void Constructor_DefaultTest()
public static void Constructor_DataTest(int data)
{
BitVector32 bv = new BitVector32(data);
Assert.NotNull(bv);
Assert.Equal(data, bv.Data);

// Copy constructor results in item with same data.
BitVector32 copied = new BitVector32(bv);
Assert.NotNull(bv);
Assert.Equal(data, copied.Data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void NonDefaultValueHasNonNullHashCode()
}
}
};
Assert.NotNull(Element.GetHashCode());
}
}
}
2 changes: 1 addition & 1 deletion src/System.Data.OleDb/tests/OleDbDataAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void Fill_OpenDataReader_Throws()
}
else
{
Assert.NotNull(adapter.Fill(ds, tableName));
Assert.InRange(adapter.Fill(ds, tableName), 0, int.MaxValue);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public async Task TestResponseWithoutContentEvents()
HttpWebRequest stopRequest = ReadPublicProperty<HttpWebRequest>(stopEvent.Value, "Request");
Assert.Equal(startRequest, stopRequest);
HttpStatusCode status = ReadPublicProperty<HttpStatusCode>(stopEvent.Value, "StatusCode");
Assert.NotNull(status);
Assert.Equal(HttpStatusCode.OK, status);

WebHeaderCollection headers = ReadPublicProperty<WebHeaderCollection>(stopEvent.Value, "Headers");
Assert.NotNull(headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public void GetProviderNames_AssertProperties()
foreach (var keyword in providerMetadata.Keywords)
{
Assert.NotEmpty(keyword.Name);
Assert.NotNull(keyword.Value);
}
foreach (var logLink in providerMetadata.LogLinks)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public async Task WritesToArrayPoolByDefault()
var endSegment = (BufferSegment)end;

Assert.Same(startSegment, endSegment);
Assert.NotNull(startSegment.Memory);
Assert.IsType<byte[]>(startSegment.MemoryOwner);

pipe.Reader.AdvanceTo(result.Buffer.End);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,6 @@ private void ReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connectTo,
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

Assert.Equal(SocketFlags.None, socketFlags);
Assert.NotNull(ipPacketInformation);

Assert.Equal(connectTo, ipPacketInformation.Address);
}
Expand Down Expand Up @@ -2173,7 +2172,6 @@ private void BeginReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connec
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

Assert.Equal(SocketFlags.None, socketFlags);
Assert.NotNull(ipPacketInformation);
Assert.Equal(connectTo, ipPacketInformation.Address);
}
}
Expand Down Expand Up @@ -2345,7 +2343,6 @@ private void ReceiveMessageFromAsync_Helper(IPAddress listenOn, IPAddress connec
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

Assert.Equal(SocketFlags.None, args.SocketFlags);
Assert.NotNull(args.ReceiveMessageFromPacketInfo);
Assert.Equal(connectTo, args.ReceiveMessageFromPacketInfo.Address);
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,6 @@ public async Task SendAsync_ReceiveAsync_Success(bool ipv4)
}

UdpReceiveResult result = await receiver.ReceiveAsync();
Assert.NotNull(result);
Assert.NotNull(result.RemoteEndPoint);
Assert.NotNull(result.Buffer);
Assert.InRange(result.Buffer.Length, 1, int.MaxValue);
Expand All @@ -639,7 +638,6 @@ public async Task SendAsync_ReceiveAsync_Connected_Success()
}

UdpReceiveResult result = await receiver.ReceiveAsync();
Assert.NotNull(result);
Assert.NotNull(result.RemoteEndPoint);
Assert.NotNull(result.Buffer);
Assert.InRange(result.Buffer.Length, 1, int.MaxValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,6 @@ public static void XmlMembersMapping_PrimitiveValue()
var getDataRequestBodyValue = 3;
var getDataRequestBodyActual = RoundTripWithXmlMembersMapping<int>(getDataRequestBodyValue, memberName, "<?xml version=\"1.0\"?>\r\n<value xmlns=\"http://tempuri.org/\">3</value>");

Assert.NotNull(getDataRequestBodyActual);
Assert.Equal(getDataRequestBodyValue, getDataRequestBodyActual);
}

Expand Down Expand Up @@ -2434,7 +2433,6 @@ public static void XmlMembersMapping_Soap_PrimitiveValue()
var getDataRequestBodyValue = 3;
var getDataRequestBodyActual = RoundTripWithXmlMembersMappingSoap<int>(getDataRequestBodyValue, memberName, "<?xml version=\"1.0\"?>\r\n<int d1p1:type=\"int\" xmlns:d1p1=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.w3.org/2001/XMLSchema\">3</int>");

Assert.NotNull(getDataRequestBodyActual);
Assert.Equal(getDataRequestBodyValue, getDataRequestBodyActual);
}

Expand Down Expand Up @@ -2473,7 +2471,6 @@ public static void XmlMembersMapping_Soap_PrimitiveValue_HasWrapperElement()
"<?xml version=\"1.0\"?>\r\n<q1:wrapper xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:q1=\"http://tempuri.org/\">\r\n <value xsi:type=\"xsd:int\">3</value>\r\n</q1:wrapper>",
wrapperName: "wrapper");

Assert.NotNull(getDataRequestBodyActual);
Assert.Equal(getDataRequestBodyValue, getDataRequestBodyActual);
}

Expand All @@ -2488,7 +2485,6 @@ public static void XmlMembersMapping_Soap_PrimitiveValue_HasWrapperElement_Valid
wrapperName: "wrapper",
validate: true);

Assert.NotNull(getDataRequestBodyActual);
Assert.Equal(getDataRequestBodyValue, getDataRequestBodyActual);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,6 @@ public static void Xml_DeserializeEmptyTimeSpanType()
using (StringReader reader = new StringReader(xml))
{
TimeSpan deserializedObj = (TimeSpan)serializer.Deserialize(reader);
Assert.NotNull(deserializedObj);
Assert.Equal(default(TimeSpan), deserializedObj);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void TestGuid()
TypeInfo ti = t.GetTypeInfo();

Guid myguid = ti.GUID;
Assert.NotNull(myguid);
Assert.NotEqual(Guid.Empty, myguid);
}

// Verify HasElementType
Expand Down
2 changes: 1 addition & 1 deletion src/System.Reflection/tests/AssemblyNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public void Flags(AssemblyNameFlags flags)
public void Flags_CurrentlyExecutingAssembly()
{
AssemblyName assemblyName = Helpers.ExecutingAssembly.GetName();
Assert.NotNull(assemblyName.Flags);
Assert.NotEqual(AssemblyNameFlags.None, assemblyName.Flags);
}

[Theory]
Expand Down
4 changes: 2 additions & 2 deletions src/System.Reflection/tests/MethodInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ public void Attributes()
{
MethodInfo methodInfo = GetMethod(typeof(MI_SubClass), "ReturnVoidMethod");
MethodAttributes attributes = methodInfo.Attributes;
Assert.NotNull(attributes);
Assert.True(attributes.HasFlag(MethodAttributes.Public));
}

[Fact]
public void CallingConvention()
{
MethodInfo methodInfo = GetMethod(typeof(MI_SubClass), "ReturnVoidMethod");
CallingConventions callingConvention = methodInfo.CallingConvention;
Assert.NotNull(callingConvention);
Assert.True(callingConvention.HasFlag(CallingConventions.HasThis));
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public void EnumerateEnvironmentVariables(EnvironmentVariableTarget target)
IDictionaryEnumerator enumerator = results.GetEnumerator();
while (enumerator.MoveNext())
{
Assert.NotNull(enumerator.Entry);
Assert.NotNull(enumerator.Entry.Key);
}

if (lookForSetValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ public void Properties_Roundtrip()
f.Binder = binder;
Assert.Same(binder, f.Binder);

Assert.NotNull(f.Context);
Assert.Null(f.Context.Context);
Assert.Equal(StreamingContextStates.All, f.Context.State);
var context = new StreamingContext(StreamingContextStates.Clone);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,6 @@ public static void DCJS_KeyValuePairOfStringObject()
var x = new KeyValuePair<string, object>("key1", "key1value");

var y = SerializeAndDeserialize<KeyValuePair<string, object>>(x, @"{""key"":""key1"",""value"":""key1value""}");
Assert.NotNull(y);
Assert.StrictEqual(y.Key, "key1");
Assert.StrictEqual(y.Value, "key1value");
}
Expand Down Expand Up @@ -2468,7 +2467,6 @@ public static void DCJS_DifferentDateTimeStylesForDeserialization()
},
};
var actual = SerializeAndDeserialize(original, null, dcjsSettings, null, true);
Assert.NotNull(actual);
Assert.Equal(original, actual);
}
}
Expand Down Expand Up @@ -2512,7 +2510,6 @@ public static void DCJS_RoundtrippingDateTime()
};
var original = DateTime.Now;
var actual = SerializeAndDeserialize(original, null, dcjsSettings, null, true);
Assert.NotNull(actual);
Assert.Equal(original, actual);
}
}
Expand Down Expand Up @@ -2593,7 +2590,6 @@ public static void DCJS_VerifyDateTimeForFormatStringDCJsonSerSettings()
var graph = new DateTimeOffset(2008, 5, 1, 8, 6, 32, new TimeSpan(1, 0, 0));
dcjsSettings = new DataContractJsonSerializerSettings() { DateTimeFormat = jsonTypes.DTF_DMMMM };
var actual3 = SerializeAndDeserialize(graph, "{\"DateTime\":\"1, mayo\",\"OffsetMinutes\":60}", dcjsSettings);
Assert.NotNull(actual3);
var expected3 = new DateTimeOffset(DateTime.Now.Year, 5, 1, 0, 0, 0, new TimeSpan(1, 0, 0));
Assert.True(actual3 == expected3,
$"{nameof(actual3)} was not as expected.\r\nExpected: {expected3} \r\n Actual: {actual3}");
Expand Down Expand Up @@ -2621,7 +2617,6 @@ public static void DCJS_VerifyDateTimeForFormatStringDCJsonSerSettings()
expectedOutput = string.Format("\"{0}\"", expectedOutput);
dcjsSettings = new DataContractJsonSerializerSettings() { DateTimeFormat = jsonTypes.DTF_DefaultFormatProviderIsDateTimeFormatInfoDotCurrentInfo };
var actual6 = SerializeAndDeserialize(dateTime, expectedOutput, dcjsSettings);
Assert.NotNull(actual6);
Assert.True(actual6 == dateTime);
}

Expand All @@ -2639,7 +2634,6 @@ public static void DCJS_VerifyDateTimeForDateTimeFormat()
KnownTypes = new List<Type>()
};
var actual = SerializeAndDeserialize(dateTime, expectedString, dcjsSettings);
Assert.NotNull(actual);
Assert.True(actual == dateTime);

dcjsSettings = new DataContractJsonSerializerSettings()
Expand All @@ -2652,7 +2646,6 @@ public static void DCJS_VerifyDateTimeForDateTimeFormat()
string actualam = GetAmString(jsonTypes.DTF_hmsFt);
string baseline = $"\"03:58:32.00 {actualam}\"";
var actual2 = SerializeAndDeserialize(new DateTime(1, 1, 1, 3, 58, 32), baseline, dcjsSettings);
Assert.NotNull(actual2);
Assert.True(actual2 == new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 3, 58, 32));

dcjsSettings = new DataContractJsonSerializerSettings()
Expand All @@ -2664,7 +2657,6 @@ public static void DCJS_VerifyDateTimeForDateTimeFormat()
};
var value3 = new DateTime(DateTime.Now.Year, 12, 20);
var actual3 = SerializeAndDeserialize(value3, "\"20, diciembre\"", dcjsSettings);
Assert.NotNull(actual3);
Assert.Equal(value3, actual3);

dcjsSettings = new DataContractJsonSerializerSettings()
Expand All @@ -2676,7 +2668,6 @@ public static void DCJS_VerifyDateTimeForDateTimeFormat()
};
var value4 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 32);
var actual4 = SerializeAndDeserialize(value4, "\"32\"", dcjsSettings);
Assert.NotNull(actual4);
Assert.Equal(value4, actual4);

dcjsSettings = new DataContractJsonSerializerSettings()
Expand All @@ -2688,7 +2679,6 @@ public static void DCJS_VerifyDateTimeForDateTimeFormat()
};
var value5 = new DateTime(1998, 1, 1);
var actual5 = SerializeAndDeserialize(value5, "\"1998 A.D.\"", dcjsSettings);
Assert.NotNull(actual5);
Assert.Equal(value5, actual5);

dcjsSettings = new DataContractJsonSerializerSettings()
Expand All @@ -2700,7 +2690,6 @@ public static void DCJS_VerifyDateTimeForDateTimeFormat()
};
var value6 = new DateTime(1998, 1, 1, 8, 25, 32, DateTimeKind.Utc);
var actual6 = SerializeAndDeserialize(value6, "\"1998-01-01T08:25:32.000Z\"", dcjsSettings);
Assert.NotNull(actual6);
Assert.Equal(value6, actual6);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static void ExtensionPropertyRoundTrip()
void Verify()
{
Assert.NotNull(obj.MyOverflow);
Assert.NotNull(obj.MyOverflow["MyIntMissing"]);
Assert.Equal(1, obj.MyInt);
Assert.Equal(2, obj.MyOverflow["MyIntMissing"].GetInt32());

Expand Down

0 comments on commit c96e389

Please sign in to comment.