Skip to content

Commit 66e893d

Browse files
committed
Add xUnit analyzers and take fixer suggestions (4 of 5): System.Web.Http.Tracing.Test to System.Web.Mvc.Test
- part of aspnet#65 Few manual changes: - use `Assert.IsAssignableFrom<T>(...)`, `Assert.IsType<T>(...)` and `Assert.Single(...)` return values - fix xUnit1026, "`[Theory]` method doesn't use all parameters" issues - turn `[Theory]` that used no parameters into a `[Fact]` - add new dataset properties; see `EncodedDataSets` in particular - `Assert.Equal<T>(...)` -> `Assert.Equal(...)` - avoid Linq's `.Where` inside `Assert.Contains(...)` and similar
1 parent f08460b commit 66e893d

File tree

65 files changed

+446
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+446
-408
lines changed

test/System.Web.Http.Tracing.Test/System.Web.Http.Tracing.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
<ItemGroup>
7070
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
7171
</ItemGroup>
72+
<ItemGroup>
73+
<Analyzer Include="..\..\packages\xunit.analyzers.0.7.0\analyzers\dotnet\cs\xunit.analyzers.dll" />
74+
</ItemGroup>
7275
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7376
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
7477
<PropertyGroup>

test/System.Web.Http.Tracing.Test/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
4+
<package id="xunit" version="2.3.0" targetFramework="net452" />
45
<package id="xunit.abstractions" version="2.0.1" targetFramework="net452" />
6+
<package id="xunit.analyzers" version="0.7.0" targetFramework="net452" />
57
<package id="xunit.assert" version="2.3.0" targetFramework="net452" />
68
<package id="xunit.core" version="2.3.0" targetFramework="net452" />
79
<package id="xunit.extensibility.core" version="2.3.0" targetFramework="net452" />

test/System.Web.Http.WebHost.Test/GlobalConfigurationTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public void ExceptionHandler_WrapsDefaultExceptionHandler()
6666
object service = configuration.Services.GetService(typeof(IExceptionHandler));
6767

6868
// Assert
69-
Assert.IsType(typeof(WebHostExceptionHandler), service); // Guard
70-
WebHostExceptionHandler typedHandler = (WebHostExceptionHandler)service;
69+
WebHostExceptionHandler typedHandler = Assert.IsType<WebHostExceptionHandler>(service); // Guard
7170
Assert.IsType(defaultExceptionHandlerType, typedHandler.InnerHandler);
7271
}
7372

test/System.Web.Http.WebHost.Test/HttpControllerHandlerTest.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,8 @@ public async Task ConvertRequest_WithBufferedPolicy_ReturnsInputStreamIfBuffered
286286
Assert.Equal(inputStreamMessage, result);
287287
}
288288

289-
[Theory]
290-
[InlineData(ReadEntityBodyMode.None)]
291-
[InlineData(ReadEntityBodyMode.Bufferless)]
292-
public async Task ConvertRequest_DoesLazyGetBufferlessInputStream_IfRequestStreamHasNotBeenRead(ReadEntityBodyMode readEntityBodyMode)
289+
[Fact]
290+
public async Task ConvertRequest_DoesLazyGetBufferlessInputStream_IfRequestStreamHasNotBeenRead()
293291
{
294292
// Arrange
295293
bool inputStreamCalled = false;
@@ -404,8 +402,7 @@ public void ConvertRequest_AddsWebHostHttpRequestContext()
404402
{
405403
// Assert
406404
HttpRequestContext context = expectedRequest.GetRequestContext();
407-
Assert.IsType<WebHostHttpRequestContext>(context);
408-
WebHostHttpRequestContext typedContext = (WebHostHttpRequestContext)context;
405+
WebHostHttpRequestContext typedContext = Assert.IsType<WebHostHttpRequestContext>(context);
409406
Assert.Same(contextBase, typedContext.Context);
410407
Assert.Same(requestBase, typedContext.WebRequest);
411408
Assert.Same(expectedRequest, typedContext.Request);
@@ -613,8 +610,8 @@ public async Task CopyResponseAsync_Creates_Correct_HttpResponseBase()
613610
}
614611

615612
// Assert
616-
Assert.Equal<int>((int)HttpStatusCode.OK, responseBase.StatusCode);
617-
Assert.True(responseBase.Headers["Content-Type"].StartsWith(JsonMediaTypeFormatter.DefaultMediaType.MediaType));
613+
Assert.Equal((int)HttpStatusCode.OK, responseBase.StatusCode);
614+
Assert.StartsWith(JsonMediaTypeFormatter.DefaultMediaType.MediaType, responseBase.Headers["Content-Type"]);
618615
Assert.Equal("\"hello\"", responseString);
619616
}
620617

@@ -772,8 +769,8 @@ await HttpControllerHandler.CopyResponseAsync(contextMock.Object, request, respo
772769
}
773770

774771
// Assert
775-
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
776-
Assert.True(responseBase.Headers["Content-Type"].StartsWith(JsonMediaTypeFormatter.DefaultMediaType.MediaType));
772+
Assert.Equal((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
773+
Assert.StartsWith(JsonMediaTypeFormatter.DefaultMediaType.MediaType, responseBase.Headers["Content-Type"]);
777774
Assert.Equal("An error has occurred.", httpError["Message"]);
778775
Assert.Equal("The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.", httpError["ExceptionMessage"]);
779776
Assert.Equal(typeof(InvalidOperationException).FullName, httpError["ExceptionType"]);
@@ -820,8 +817,8 @@ public async Task CopyResponseAsync_IfHandlerIsDefault_Returns_Error_Response_Wh
820817
}
821818

822819
// Assert
823-
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
824-
Assert.True(responseBase.Headers["Content-Type"].StartsWith(JsonMediaTypeFormatter.DefaultMediaType.MediaType));
820+
Assert.Equal((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
821+
Assert.StartsWith(JsonMediaTypeFormatter.DefaultMediaType.MediaType, responseBase.Headers["Content-Type"]);
825822
Assert.Equal("An error has occurred.", httpError["Message"]);
826823
Assert.Equal("The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.", httpError["ExceptionMessage"]);
827824
Assert.Equal(typeof(InvalidOperationException).FullName, httpError["ExceptionType"]);
@@ -861,7 +858,7 @@ public async Task CopyResponseAsync_Returns_User_Response_When_Formatter_Write_T
861858
memoryStream.Seek(0L, SeekOrigin.Begin);
862859

863860
// Assert
864-
Assert.Equal<int>((int)errorResponse.StatusCode, responseBase.StatusCode);
861+
Assert.Equal((int)errorResponse.StatusCode, responseBase.StatusCode);
865862
Assert.Equal(0, memoryStream.Length);
866863
Assert.Equal("myValue", responseBase.Headers["myHeader"]);
867864
Assert.Null(responseBase.Headers["Content-Type"]);
@@ -903,8 +900,8 @@ public async Task CopyResponseAsync_Returns_User_Response_When_Formatter_Write_T
903900
}
904901

905902
// Assert
906-
Assert.Equal<int>((int)errorResponse.StatusCode, responseBase.StatusCode);
907-
Assert.True(responseBase.Headers["Content-Type"].StartsWith("application/fake"));
903+
Assert.Equal((int)errorResponse.StatusCode, responseBase.StatusCode);
904+
Assert.StartsWith("application/fake", responseBase.Headers["Content-Type"]);
908905
Assert.Equal("user message", responseContent);
909906
Assert.Equal("myValue", responseBase.Headers["myHeader"]);
910907
}
@@ -946,7 +943,7 @@ await HttpControllerHandler.CopyResponseAsync(contextMock.Object, request, respo
946943
exceptionHandler, CancellationToken.None);
947944

948945
// Assert
949-
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
946+
Assert.Equal((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
950947
Assert.Equal(0, memoryStream.Length);
951948
Assert.Null(responseBase.Headers["Content-Type"]);
952949
}
@@ -986,7 +983,7 @@ await HttpControllerHandler.CopyResponseAsync(contextMock.Object, request, respo
986983
exceptionHandler, CancellationToken.None);
987984

988985
// Assert
989-
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
986+
Assert.Equal((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
990987
Assert.Equal(0, memoryStream.Length);
991988
Assert.Null(responseBase.Headers["Content-Type"]);
992989
}
@@ -1038,7 +1035,7 @@ await HttpControllerHandler.CopyResponseAsync(contextMock.Object, request, respo
10381035
exceptionHandler, CancellationToken.None);
10391036

10401037
// Assert
1041-
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
1038+
Assert.Equal((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
10421039
Assert.Equal(0, memoryStream.Length);
10431040
Assert.Null(responseBase.Headers["Content-Type"]);
10441041
}
@@ -1079,7 +1076,7 @@ await HttpControllerHandler.CopyResponseAsync(contextMock.Object, request, respo
10791076
exceptionHandler, CancellationToken.None);
10801077

10811078
// Assert
1082-
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
1079+
Assert.Equal((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
10831080
Assert.Equal(0, memoryStream.Length);
10841081
Assert.Null(responseBase.Headers["Content-Type"]);
10851082
}
@@ -1097,7 +1094,7 @@ public async Task CopyResponseAsync_Returns_InternalServerError_And_No_Content_F
10971094
await CopyResponseAsync(contextMock.Object, request: new HttpRequestMessage(), response: null);
10981095

10991096
// Assert
1100-
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
1097+
Assert.Equal((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
11011098
Assert.Equal(0, memoryStream.Length);
11021099
Assert.Null(responseBase.Headers["Content-Type"]);
11031100
}
@@ -1390,7 +1387,7 @@ public async Task WriteBufferedResponseContentAsync_IfCopyToAsyncThrowsAndHandle
13901387

13911388
Assert.Same(expectedException, exception);
13921389
Assert.NotNull(exception.StackTrace);
1393-
Assert.True(exception.StackTrace.StartsWith(expectedStackTrace));
1390+
Assert.StartsWith(expectedStackTrace, exception.StackTrace);
13941391
}
13951392
}
13961393

test/System.Web.Http.WebHost.Test/RouteCollectionExtensionsTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void MapHttpRoute1CreatesRoute()
3434
// Assert
3535
Assert.NotNull(route);
3636
Assert.Equal("template", route.Url);
37-
Assert.Equal(1, route.Defaults.Count);
37+
Assert.Single(route.Defaults);
3838
Assert.Equal("D1", route.Defaults["d1"]);
3939
Assert.Same(route, routes["name"]);
4040
}
@@ -52,7 +52,7 @@ public void MapHttpRoute1WithDefaultsAsDictionaryCreatesRoute()
5252
// Assert
5353
Assert.NotNull(route);
5454
Assert.Equal("template", route.Url);
55-
Assert.Equal(1, route.Defaults.Count);
55+
Assert.Single(route.Defaults);
5656
Assert.Equal("D1", route.Defaults["d1"]);
5757
Assert.Same(route, routes["name"]);
5858
}
@@ -77,9 +77,9 @@ public void MapHttpRoute2CreatesRoute()
7777
// Assert
7878
Assert.NotNull(route);
7979
Assert.Equal("template", route.Url);
80-
Assert.Equal(1, route.Defaults.Count);
80+
Assert.Single(route.Defaults);
8181
Assert.Equal("D1", route.Defaults["d1"]);
82-
Assert.Equal(1, route.Defaults.Count);
82+
Assert.Single(route.Constraints);
8383
Assert.Equal("C1", route.Constraints["c1"]);
8484
Assert.Same(route, routes["name"]);
8585
}
@@ -98,9 +98,9 @@ public void MapHttpRoute2WithDefaultsAndConstraintsAsDictionaryCreatesRoute()
9898
// Assert
9999
Assert.NotNull(route);
100100
Assert.Equal("template", route.Url);
101-
Assert.Equal(1, route.Defaults.Count);
101+
Assert.Single(route.Defaults);
102102
Assert.Equal("D1", route.Defaults["d1"]);
103-
Assert.Equal(1, route.Defaults.Count);
103+
Assert.Single(route.Constraints);
104104
Assert.Equal("C1", route.Constraints["c1"]);
105105
Assert.Same(route, routes["name"]);
106106
}

test/System.Web.Http.WebHost.Test/Routing/HostedHttpRouteCollectionTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public void IgnoreRoute_GetVirtualPathReturnsNull()
307307

308308
// Assert
309309
// Altough it contains the ignore route, GetVirtualPath from the ignored route will always return null.
310-
Assert.Equal(collection.Count, 1);
310+
Assert.Equal(1, collection.Count);
311311
Assert.Null(httpvPathData);
312312
}
313313

@@ -322,7 +322,7 @@ public async Task IgnoreRoute_GetSoft404IfRouteIgnored(string requestPath)
322322

323323
var response = await SubmitRequestAsync(request);
324324

325-
Assert.Equal(response.StatusCode, Net.HttpStatusCode.NotFound);
325+
Assert.Equal(Net.HttpStatusCode.NotFound, response.StatusCode);
326326
Assert.True(response.RequestMessage.Properties.ContainsKey(HttpPropertyKeys.NoRouteMatched));
327327
}
328328

@@ -411,7 +411,7 @@ public async Task IgnoreRoute_WithConstraints_GetSoft404IfRouteIgnored(string re
411411

412412
var response = await SubmitRequestAsync(request);
413413

414-
Assert.Equal(response.StatusCode, Net.HttpStatusCode.NotFound);
414+
Assert.Equal(Net.HttpStatusCode.NotFound, response.StatusCode);
415415
Assert.True(response.RequestMessage.Properties.ContainsKey(HttpPropertyKeys.NoRouteMatched));
416416
}
417417

@@ -427,7 +427,7 @@ public async Task IgnoreRoute_WithConstraints_GetValueIfNotIgnored(string reques
427427

428428
var response = await SubmitRequestAsync(request);
429429

430-
Assert.Equal(response.StatusCode, Net.HttpStatusCode.OK);
430+
Assert.Equal(Net.HttpStatusCode.OK, response.StatusCode);
431431
Assert.Equal(String.Concat("values/", await response.Content.ReadAsStringAsync()), requestPath);
432432
}
433433

test/System.Web.Http.WebHost.Test/Routing/HttpContextBaseExtensionsTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using System.Collections.Specialized;
66
using System.IO;
7-
using System.Linq;
87
using System.Net.Http;
98
using Microsoft.TestCommon;
109
using Moq;
@@ -27,7 +26,7 @@ public void GetOrCreateHttpRequestMessageFromHttpContextCopiesHeaders()
2726
col.Add("customHeader", "customHeaderValue");
2827
requestMock.Setup(r => r.Headers).Returns(col);
2928
contextMock.Setup(o => o.Request).Returns(requestMock.Object);
30-
29+
3130
// Act
3231
contextMock.Object.GetOrCreateHttpRequestMessage();
3332

@@ -37,8 +36,8 @@ public void GetOrCreateHttpRequestMessageFromHttpContextCopiesHeaders()
3736
Assert.Equal(HttpMethod.Get, request.Method);
3837
IEnumerable<string> headerValues;
3938
Assert.True(request.Headers.TryGetValues("customHeader", out headerValues));
40-
Assert.Equal(1, headerValues.Count());
41-
Assert.Equal("customHeaderValue", headerValues.First());
39+
string headerValue = Assert.Single(headerValues);
40+
Assert.Equal("customHeaderValue", headerValue);
4241
}
4342
}
4443
}

test/System.Web.Http.WebHost.Test/Routing/HttpRouteExceptionHandlerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public async Task ProcessRequestAsync_IfHandlerDoesNotHandle_ReturnsTaskPropagat
243243

244244
Assert.Same(expectedException, exception);
245245
Assert.NotNull(exception.StackTrace);
246-
Assert.True(exception.StackTrace.StartsWith(expectedStackTrace));
246+
Assert.StartsWith(expectedStackTrace, exception.StackTrace);
247247
}
248248

249249
}

test/System.Web.Http.WebHost.Test/Routing/HttpRouteExceptionRouteHandlerTests.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
74
using System.Runtime.ExceptionServices;
8-
using System.Text;
9-
using System.Threading.Tasks;
105
using System.Web.Routing;
116
using Microsoft.TestCommon;
127

@@ -40,8 +35,7 @@ public void GetHttpHandler_ReturnsExceptionHandlerWithExceptionInfo()
4035
IHttpHandler handler = product.GetHttpHandler(requestContext);
4136

4237
// Assert
43-
Assert.IsType<HttpRouteExceptionHandler>(handler);
44-
HttpRouteExceptionHandler typedHandler = (HttpRouteExceptionHandler)handler;
38+
HttpRouteExceptionHandler typedHandler = Assert.IsType<HttpRouteExceptionHandler>(handler);
4539
Assert.Same(expectedExceptionInfo, typedHandler.ExceptionInfo);
4640
}
4741

test/System.Web.Http.WebHost.Test/Routing/HttpWebRouteTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public void GetRouteData_IfHttpRouteGetRouteDataThrows_RoutesToExceptionHandler(
3636
// Assert
3737
Assert.NotNull(routeData);
3838
Assert.Same(product, routeData.Route);
39-
Assert.IsType<HttpRouteExceptionRouteHandler>(routeData.RouteHandler);
40-
HttpRouteExceptionRouteHandler typedHandler = (HttpRouteExceptionRouteHandler)routeData.RouteHandler;
39+
HttpRouteExceptionRouteHandler typedHandler = Assert.IsType<HttpRouteExceptionRouteHandler>(routeData.RouteHandler);
4140
ExceptionDispatchInfo exceptionInfo = typedHandler.ExceptionInfo;
4241
Assert.NotNull(exceptionInfo); // Guard
4342
Assert.Same(expectedException, exceptionInfo.SourceException);

0 commit comments

Comments
 (0)