From f9b890803d926f030fa4714ac60cf1bb16a70af1 Mon Sep 17 00:00:00 2001 From: gintsk <3540004+gintsk@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:13:18 +0200 Subject: [PATCH] test: Code cleanup in test project (#82) * Minor code-style cleanups * Migrate test to System.Text.Json * Add supported dotnet versions to test project * Make tests async and simplify TestMimeDbMimeTypesAsync * Use https for svn.apache.org * Remove redundant usings * Update Test/Test.csproj Drop net7.0 as it will be out-of-support soon Co-authored-by: Michael Kriese * PR comments: revert changes and ValueTask fix (net48) * fix spacing * Update Test/Test.csproj Co-authored-by: Michael Kriese --------- Co-authored-by: Michael Kriese --- Test/BasicTest.cs | 5 ++--- Test/Properties/AssemblyInfo.cs | 2 -- Test/TemplateSourceTests.cs | 29 +++++++++++++++-------------- Test/Test.csproj | 6 +++--- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/Test/BasicTest.cs b/Test/BasicTest.cs index d6010f1..78929e1 100644 --- a/Test/BasicTest.cs +++ b/Test/BasicTest.cs @@ -10,7 +10,7 @@ namespace Test [TestClass] public class BasicTest { - private readonly Dictionary _expectedTypes = new Dictionary + private readonly Dictionary _expectedTypes = new() { {"PNG", "image/png"}, {"png", "image/png"}, @@ -94,7 +94,7 @@ public void TestMimeTypeLookupToGetExtensions() { var expected = new[] { KnownMimeTypes.FileExtensions.Json, KnownMimeTypes.FileExtensions.Map }; var actual = MimeUtility.GetExtensions(KnownMimeTypes.Json); - Assert.IsTrue(actual.All(x => expected.Contains(x))); + Assert.IsTrue(Array.TrueForAll(actual, x => expected.Contains(x))); var @null = MimeUtility.GetExtensions("invalid"); Assert.IsNull(@null); @@ -122,6 +122,5 @@ public void TestMimeTypeToExtenstionsLookup() Assert.IsTrue(kv.Value.Length > 0, $"{kv.Key} cannot have zero extensions."); } } - } } diff --git a/Test/Properties/AssemblyInfo.cs b/Test/Properties/AssemblyInfo.cs index 1e5f619..cd0d254 100644 --- a/Test/Properties/AssemblyInfo.cs +++ b/Test/Properties/AssemblyInfo.cs @@ -1,5 +1,3 @@ -using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // Setting ComVisible to false makes the types in this assembly not visible diff --git a/Test/TemplateSourceTests.cs b/Test/TemplateSourceTests.cs index b0b02ed..c994ec1 100644 --- a/Test/TemplateSourceTests.cs +++ b/Test/TemplateSourceTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json.Linq; @@ -10,14 +11,14 @@ namespace Test [TestClass] public class TemplateSourceTests { - private const string APACHE_URL = "http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types"; + private const string APACHE_URL = "https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types"; private const string NGINX_URL = "https://raw.githubusercontent.com/h5bp/server-configs-nginx/master/mime.types"; private const string MIMEDB_URL = "https://raw.githubusercontent.com/jshttp/mime-db/master/src/custom-types.json"; [TestMethod] - public void TestApacheMimeTypes() + public async Task TestApacheMimeTypesAsync() { - var keyPairs = GetMimeTypesFromText(APACHE_URL, line => + var keyPairs = await GetMimeTypesFromTextAsync(APACHE_URL, line => { if (line[0] == '#') return null; var parts = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); @@ -28,9 +29,9 @@ public void TestApacheMimeTypes() } [TestMethod] - public void TestNginxMimeTypes() + public async Task TestNginxMimeTypesAsync() { - var keyPairs = GetMimeTypesFromText(NGINX_URL, line => + var keyPairs = await GetMimeTypesFromTextAsync(NGINX_URL, line => { line = line.Trim().TrimEnd(';'); if (line[0] == '#' || line[0] == '}' || line.StartsWith("types {")) return null; @@ -42,12 +43,12 @@ public void TestNginxMimeTypes() } [TestMethod] - public void TestMimeDbMimeTypes() + public async Task TestMimeDbMimeTypesAsync() { - var keyPairs = GetMimeTypesFromJson(MIMEDB_URL, resource => + var keyPairs = await GetMimeTypesFromJsonAsync(MIMEDB_URL, resource => { return from mimeTypes in resource.Children() - let mimeType = ((JProperty) mimeTypes).Name + let mimeType = ((JProperty)mimeTypes).Name from mimeTypeProperties in mimeTypes.Children() from mimeTypeProperty in mimeTypeProperties.Children() from mimeTypeValue in mimeTypeProperty.Values() @@ -62,23 +63,23 @@ select new[] Assert.IsTrue(keyPairs.Any()); } - private static string GetPageContent(string url) + private static async Task GetPageContentAsync(string url) { using var client = new HttpClient(); - return client.GetStringAsync(url).GetAwaiter().GetResult(); + return await client.GetStringAsync(url); } - private static IEnumerable GetMimeTypesFromText(string url, Func processLine) + private static async Task> GetMimeTypesFromTextAsync(string url, Func processLine) { - var content = GetPageContent(url); + var content = await GetPageContentAsync(url); if (string.IsNullOrEmpty(content)) return Enumerable.Empty(); var lines = content.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); return lines.Select(processLine).Where(newKeyPairs => newKeyPairs != null).ToList(); } - private static IEnumerable GetMimeTypesFromJson(string url, Func> processObject) + private static async Task> GetMimeTypesFromJsonAsync(string url, Func> processObject) { - var content = GetPageContent(url); + var content = await GetPageContentAsync(url); if (string.IsNullOrEmpty(content)) return Enumerable.Empty(); var resource = JObject.Parse(content); return processObject(resource); diff --git a/Test/Test.csproj b/Test/Test.csproj index 88906f7..0b995b6 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -1,9 +1,9 @@ - net6.0;net48 - net6.0 + net8.0;net6.0;net48 + net8.0;net6.0 $(MSBuildThisFileDirectory)/test.runsettings - latest + latest