Skip to content

Commit

Permalink
Merge branch 'improvement/C#-10' into develop
Browse files Browse the repository at this point in the history
Upgraded the project to use both C# 10 as well as the associated .NET 6 code analyzers.

Modified code to take advantage of natural types for generics; this was especially useful for XUNit's `Assert.Equal<>()` overloads, which no longer need the type parameter to be set in order to identify the correct overload.

Updated type checking statements to prefer `is` and `is not` where practice. This replaced a handful of cases where we were previously using `Equals(typeof())` or even `IsAssignableFrom()`.

Implemented new C# 10 constant string interpolation for `[Obsolete()]` attributes, which allows (most) class and member references to use `nameof()` instead of being hardcoded as strings.

Fixed a handful of new issues identified by the new code analyzers while I was at it.

This resolves Issue #96.
  • Loading branch information
JeremyCaney committed Dec 9, 2021
2 parents a82f7e6 + 427959b commit 5eaa417
Show file tree
Hide file tree
Showing 76 changed files with 361 additions and 318 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<LangVersion>9.0</LangVersion>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public class TopicViewResultExecutorTest: IClassFixture<WebApplicationFactory<St
var uri = new Uri("/Web/MissingView/", UriKind.Relative);
var response = await client.GetAsync(uri).ConfigureAwait(false);

Assert.Equal<HttpStatusCode?>(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions OnTopic.AspNetCore.Mvc.Tests/TopicRepositoryExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class TopicRepositoryExtensionsTest: IClassFixture<StubTopicRepository> {
var currentTopic = _topicRepository.Load(routes);

Assert.NotNull(currentTopic);
Assert.Equal<Topic?>(topic, currentTopic);
Assert.Equal(topic, currentTopic);
Assert.Equal("Web_0_1_1", currentTopic?.Key);

}
Expand All @@ -84,7 +84,7 @@ public class TopicRepositoryExtensionsTest: IClassFixture<StubTopicRepository> {
var currentTopic = _topicRepository.Load(routes);

Assert.NotNull(currentTopic);
Assert.Equal<Topic?>(topic, currentTopic);
Assert.Equal(topic, currentTopic);
Assert.Equal("Root", currentTopic?.Key);

}
Expand Down
4 changes: 2 additions & 2 deletions OnTopic.AspNetCore.Mvc.Tests/TopicViewComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public class TopicViewComponentTest: IClassFixture<StubTopicRepository> {
Assert.NotNull(model);
Assert.Equal(_topic.GetWebPath(), model?.CurrentWebPath);
Assert.Equal("/Web/", model?.NavigationRoot?.WebPath);
Assert.Equal<int?>(3, model?.NavigationRoot?.Children.Count);
Assert.Equal(3, model?.NavigationRoot?.Children.Count);

}

Expand Down Expand Up @@ -204,7 +204,7 @@ public class TopicViewComponentTest: IClassFixture<StubTopicRepository> {
Assert.NotNull(model);
Assert.Equal(_topic.GetWebPath(), model?.CurrentWebPath);
Assert.Equal("/Web/Web_3/", model?.NavigationRoot?.WebPath);
Assert.Equal<int?>(2, model?.NavigationRoot?.Children.Count);
Assert.Equal(2, model?.NavigationRoot?.Children.Count);
Assert.True(model?.NavigationRoot?.IsSelected(_topic.GetWebPath())?? false);

}
Expand Down
8 changes: 4 additions & 4 deletions OnTopic.AspNetCore.Mvc.Tests/ValidateTopicAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public class ValidateTopicAttributeTest {
var result = context.Result as StatusCodeResult;

Assert.NotNull(result);
Assert.Equal<int?>(403, result?.StatusCode);
Assert.Equal(403, result?.StatusCode);

}

Expand All @@ -231,7 +231,7 @@ public class ValidateTopicAttributeTest {
var result = context.Result as StatusCodeResult;

Assert.NotNull(result);
Assert.Equal<int?>(403, result?.StatusCode);
Assert.Equal(403, result?.StatusCode);

}

Expand All @@ -257,7 +257,7 @@ public class ValidateTopicAttributeTest {
var result = context.Result as StatusCodeResult;

Assert.NotNull(result);
Assert.Equal<int?>(403, result?.StatusCode);
Assert.Equal(403, result?.StatusCode);

}

Expand Down Expand Up @@ -309,7 +309,7 @@ public class ValidateTopicAttributeTest {
var result = context.Result as StatusCodeResult;

Assert.NotNull(result);
Assert.Equal<int?>(403, result?.StatusCode);
Assert.Equal(403, result?.StatusCode);

}

Expand Down
2 changes: 1 addition & 1 deletion OnTopic.AspNetCore.Mvc/Models/NavigationViewModel{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class NavigationViewModel<T> where T : class, IHierarchicalTopicViewModel
/// </summary>
/// <inheritdoc cref="CurrentWebPath"/>
[ExcludeFromCodeCoverage]
[Obsolete("The CurrentKey property has been replaced in favor of CurrentWebPath.", true)]
[Obsolete($"The {nameof(CurrentKey)} property has been replaced in favor of {nameof(CurrentWebPath)}.", true)]
public string CurrentKey { get; set; } = default!;

} //Class
Expand Down
2 changes: 1 addition & 1 deletion OnTopic.AspNetCore.Mvc/OnTopic.AspNetCore.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions OnTopic.AspNetCore.Mvc/TopicViewResultExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ IModelMetadataProvider modelMetadataProvider
if (!(view?.Success ?? false) && requestContext.Headers.ContainsKey("Accept")) {
foreach (var header in requestContext.Headers["Accept"]) {
var value = header.Replace("+", "-", StringComparison.Ordinal);
if (value.Contains("/", StringComparison.Ordinal)) {
if (value.Contains('/', StringComparison.Ordinal)) {
value = value[(value.IndexOf("/", StringComparison.Ordinal)+1)..];
}
if (value.Contains(";", StringComparison.Ordinal)) {
if (value.Contains(';', StringComparison.Ordinal)) {
value = value[..(value.IndexOf(";", StringComparison.Ordinal))];
}
if (value is not null) {
Expand Down
2 changes: 1 addition & 1 deletion OnTopic.Data.Caching/OnTopic.Data.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion OnTopic.Data.Sql/OnTopic.Data.Sql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions OnTopic.Data.Sql/SqlDataReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ internal static class SqlDataReaderExtensions {
/*------------------------------------------------------------------------------------------------------------------------
| Assign parent
\-----------------------------------------------------------------------------------------------------------------------*/
if (parentId >= 0 && current.Parent?.Id != parentId && topics.Keys.Contains(parentId)) {
if (parentId >= 0 && current.Parent?.Id != parentId && topics.ContainsKey(parentId)) {
current.Parent = topics[parentId];
}

Expand Down Expand Up @@ -359,7 +359,7 @@ internal static class SqlDataReaderExtensions {
var related = (Topic?)null;

// Fetch the related topic
if (topics.Keys.Contains(targetTopicId)) {
if (topics.ContainsKey(targetTopicId)) {
related = topics[targetTopicId];
}

Expand Down Expand Up @@ -417,7 +417,7 @@ internal static class SqlDataReaderExtensions {
// Fetch the related topic
if (targetTopicId is null) {
}
else if (topics.Keys.Contains(targetTopicId.Value)) {
else if (topics.ContainsKey(targetTopicId.Value)) {
referenced = topics[targetTopicId.Value];
}
else {
Expand Down
2 changes: 1 addition & 1 deletion OnTopic.TestDoubles/OnTopic.TestDoubles.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
5 changes: 4 additions & 1 deletion OnTopic.TestDoubles/StubTopicRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ public class StubTopicRepository : TopicRepository, ITopicRepository {
\-------------------------------------------------------------------------------------------------------------------------*/
/// <inheritdoc cref="TopicRepository.GetContentTypeDescriptors(ContentTypeDescriptor)" />
[ExcludeFromCodeCoverage]
[Obsolete("Deprecated. Instead, use the new SetContentTypeDescriptorsProxy(), which provides the same function.", true)]
[Obsolete(
$"Deprecated. Instead, use the new {nameof(SetContentTypeDescriptorsProxy)}, which provides the same function.",
true
)]
public ContentTypeDescriptorCollection GetContentTypeDescriptorsProxy(ContentTypeDescriptor topicGraph) =>
base.SetContentTypeDescriptors(topicGraph);

Expand Down
58 changes: 29 additions & 29 deletions OnTopic.Tests/AttributeCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class AttributeCollectionTest {

topic.Attributes.SetInteger("Number1", 1);

Assert.Equal<int>(1, topic.Attributes.GetInteger("Number1", 5));
Assert.Equal(1, topic.Attributes.GetInteger("Number1", 5));

}

Expand All @@ -131,9 +131,9 @@ public class AttributeCollectionTest {

baseTopic.Attributes.SetInteger("Number1", 1);

Assert.Equal<int>(1, topic.Attributes.GetInteger("Number1", 5));
Assert.Equal<int>(1, childTopic.Attributes.GetInteger("Number1", 5, true));
Assert.Equal<int>(0, topic.Attributes.GetInteger("Number1", inheritFromBase: false));
Assert.Equal(1, topic.Attributes.GetInteger("Number1", 5));
Assert.Equal(1, childTopic.Attributes.GetInteger("Number1", 5, true));
Assert.Equal(0, topic.Attributes.GetInteger("Number1", inheritFromBase: false));

}

Expand All @@ -150,8 +150,8 @@ public class AttributeCollectionTest {

topic.Attributes.SetValue("Number3", "Invalid");

Assert.Equal<int>(0, topic.Attributes.GetInteger("Number3"));
Assert.Equal<int>(5, topic.Attributes.GetInteger("Number3", 5));
Assert.Equal(0, topic.Attributes.GetInteger("Number3"));
Assert.Equal(5, topic.Attributes.GetInteger("Number3", 5));

}

Expand All @@ -166,8 +166,8 @@ public class AttributeCollectionTest {

var topic = new Topic("Test", "Container");

Assert.Equal<int>(0, topic.Attributes.GetInteger("InvalidKey"));
Assert.Equal<int>(5, topic.Attributes.GetInteger("InvalidKey", 5));
Assert.Equal(0, topic.Attributes.GetInteger("InvalidKey"));
Assert.Equal(5, topic.Attributes.GetInteger("InvalidKey", 5));

}

Expand All @@ -184,7 +184,7 @@ public class AttributeCollectionTest {

topic.Attributes.SetDouble("Number1", 1);

Assert.Equal<double>(1.0, topic.Attributes.GetDouble("Number1", 5.0));
Assert.Equal(1.0, topic.Attributes.GetDouble("Number1", 5.0));

}

Expand All @@ -206,9 +206,9 @@ public class AttributeCollectionTest {

baseTopic.Attributes.SetDouble("Number1", 1);

Assert.Equal<double>(1.0, topic.Attributes.GetDouble("Number1", 5.0));
Assert.Equal<double>(1.0, childTopic.Attributes.GetDouble("Number1", 5.0, true));
Assert.Equal<double>(0.0, topic.Attributes.GetInteger("Number1", inheritFromBase: false));
Assert.Equal(1.0, topic.Attributes.GetDouble("Number1", 5.0));
Assert.Equal(1.0, childTopic.Attributes.GetDouble("Number1", 5.0, true));
Assert.Equal(0.0, topic.Attributes.GetInteger("Number1", inheritFromBase: false));

}

Expand All @@ -225,8 +225,8 @@ public class AttributeCollectionTest {

topic.Attributes.SetValue("Number3", "Invalid");

Assert.Equal<double>(0, topic.Attributes.GetDouble("Number3"));
Assert.Equal<double>(5.0, topic.Attributes.GetDouble("Number3", 5.0));
Assert.Equal(0.0, topic.Attributes.GetDouble("Number3"));
Assert.Equal(5.0, topic.Attributes.GetDouble("Number3", 5.0));

}

Expand All @@ -241,8 +241,8 @@ public class AttributeCollectionTest {

var topic = new Topic("Test", "Container");

Assert.Equal<double>(0, topic.Attributes.GetDouble("InvalidKey"));
Assert.Equal<double>(5.0, topic.Attributes.GetDouble("InvalidKey", 5.0));
Assert.Equal(0.0, topic.Attributes.GetDouble("InvalidKey"));
Assert.Equal(5.0, topic.Attributes.GetDouble("InvalidKey", 5.0));

}

Expand All @@ -260,7 +260,7 @@ public class AttributeCollectionTest {

topic.Attributes.SetDateTime("DateTime1", dateTime1);

Assert.Equal<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime1", DateTime.MinValue));
Assert.Equal(dateTime1, topic.Attributes.GetDateTime("DateTime1", DateTime.MinValue));

}

Expand All @@ -283,9 +283,9 @@ public class AttributeCollectionTest {

baseTopic.Attributes.SetDateTime("DateTime1", dateTime1);

Assert.Equal<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime1", DateTime.Now));
Assert.Equal<DateTime>(dateTime1, childTopic.Attributes.GetDateTime("DateTime1", DateTime.Now, true));
Assert.Equal<DateTime>(new DateTime(), topic.Attributes.GetDateTime("DateTime1", inheritFromBase: false));
Assert.Equal(dateTime1, topic.Attributes.GetDateTime("DateTime1", DateTime.Now));
Assert.Equal(dateTime1, childTopic.Attributes.GetDateTime("DateTime1", DateTime.Now, true));
Assert.Equal(new DateTime(), topic.Attributes.GetDateTime("DateTime1", inheritFromBase: false));

}

Expand All @@ -303,8 +303,8 @@ public class AttributeCollectionTest {

topic.Attributes.SetValue("DateTime2", "IncorrectValue");

Assert.Equal<DateTime>(new DateTime(), topic.Attributes.GetDateTime("DateTime2"));
Assert.Equal<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime2", dateTime1));
Assert.Equal(new DateTime(), topic.Attributes.GetDateTime("DateTime2"));
Assert.Equal(dateTime1, topic.Attributes.GetDateTime("DateTime2", dateTime1));

}

Expand All @@ -323,8 +323,8 @@ public class AttributeCollectionTest {

topic.Attributes.SetDateTime("DateTime2", dateTime2);

Assert.Equal<DateTime>(new DateTime(), topic.Attributes.GetDateTime("DateTime3"));
Assert.Equal<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime3", dateTime1));
Assert.Equal(new DateTime(), topic.Attributes.GetDateTime("DateTime3"));
Assert.Equal(dateTime1, topic.Attributes.GetDateTime("DateTime3", dateTime1));

}

Expand Down Expand Up @@ -686,9 +686,9 @@ public class AttributeCollectionTest {
topic.Attributes.TryGetValue("Bar", out var cleanedAttribute2);
topic.Attributes.TryGetValue("Baz", out var cleanedAttribute3);

Assert.Equal<DateTime?>(firstVersion, cleanedAttribute1?.LastModified);
Assert.Equal<DateTime?>(secondVersion, cleanedAttribute2?.LastModified);
Assert.Equal<DateTime?>(thirdVersion, cleanedAttribute3?.LastModified);
Assert.Equal(firstVersion, cleanedAttribute1?.LastModified);
Assert.Equal(secondVersion, cleanedAttribute2?.LastModified);
Assert.Equal(thirdVersion, cleanedAttribute3?.LastModified);

}

Expand Down Expand Up @@ -874,7 +874,7 @@ public class AttributeCollectionTest {

topic.Attributes.SetInteger("NumericAttribute", 1);

Assert.Equal<int>(1, topic.NumericAttribute);
Assert.Equal(1, topic.NumericAttribute);

}

Expand Down Expand Up @@ -928,7 +928,7 @@ public class AttributeCollectionTest {

topic.Attributes.SetDateTime("DateTimeAttribute", dateTime);

Assert.Equal<DateTime>(dateTime, topic.DateTimeAttribute);
Assert.Equal(dateTime, topic.DateTimeAttribute);

}

Expand Down
Loading

0 comments on commit 5eaa417

Please sign in to comment.