Skip to content

Commit

Permalink
Merge pull request #188 from AdrianJSClark/replace-webutilities-package
Browse files Browse the repository at this point in the history
Replace Web Utilities Package
  • Loading branch information
AdrianJSClark authored Aug 16, 2023
2 parents df33e5e + b029815 commit 535cd4f
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 206 deletions.
28 changes: 19 additions & 9 deletions src/Aydsko.iRacingData.UnitTests/DictionaryExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,32 @@
public class DictionaryExtensionTests
{
[Test, TestCaseSource(nameof(GetTestCases))]
public KeyValuePair<string, string> CheckAddParameterIfNotNull<T>(T value)
public KeyValuePair<string, object?> CheckAddParameterIfNotNull<T>(ExampleData<T> example)
{
var parameters = new Dictionary<string, string>();
parameters.AddParameterIfNotNull("value", value);
var parameters = new Dictionary<string, object?>();
parameters.AddParameterIfNotNull(() => example.Value);
return parameters.ElementAt(0);
}

#pragma warning disable CA1861 // Avoid constant arrays as arguments - not worth it for these unit test cases.
public static IEnumerable<TestCaseData> GetTestCases()
{
yield return new TestCaseData("foo").Returns(new KeyValuePair<string, string>("value", "foo"));
yield return new TestCaseData(new DateTime(2023, 4, 22, 11, 12, 13)).Returns(new KeyValuePair<string, string>("value", "2023-04-22T11:12Z"));
yield return new TestCaseData(new[] { 1, 2, 3 }).Returns(new KeyValuePair<string, string>("value", "1,2,3"));
yield return new TestCaseData(new string[] { "a", "b", "c" }.AsEnumerable()).Returns(new KeyValuePair<string, string>("value", "a,b,c"));
yield return new TestCaseData(true).Returns(new KeyValuePair<string, string>("value", "true"));
yield return new TestCaseData(Common.EventType.Practice).Returns(new KeyValuePair<string, string>("value", "2"));
yield return new TestCaseData(new ExampleData<string>("foo")).Returns(new KeyValuePair<string, object?>("Value", "foo"));
yield return new TestCaseData(new ExampleData<DateTime>(new DateTime(2023, 4, 22, 11, 12, 13))).Returns(new KeyValuePair<string, object?>("Value", new DateTime(2023, 4, 22, 11, 12, 13)));
yield return new TestCaseData(new ExampleData<int[]>(new[] { 1, 2, 3 })).Returns(new KeyValuePair<string, object?>("Value", new[] { 1, 2, 3 }));
yield return new TestCaseData(new ExampleData<IEnumerable<string>>(new string[] { "a", "b", "c" }.AsEnumerable())).Returns(new KeyValuePair<string, object?>("Value", new string[] { "a", "b", "c" }.AsEnumerable()));
yield return new TestCaseData(new ExampleData<bool>(true)).Returns(new KeyValuePair<string, object?>("Value", true));
yield return new TestCaseData(new ExampleData<Common.EventType>(Common.EventType.Practice)).Returns(new KeyValuePair<string, object?>("Value", Common.EventType.Practice));
}
#pragma warning restore CA1861 // Avoid constant arrays as arguments
}

public class ExampleData<T>
{
public T? Value { get; }

public ExampleData(T value)
{
Value = value;
}
}
22 changes: 22 additions & 0 deletions src/Aydsko.iRacingData.UnitTests/UrlExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Aydsko.iRacingData.UnitTests;

internal sealed class UrlExtensionsTests
{
[Test, TestCaseSource(nameof(ValidateToUrlWithQueryMethodTestCases))]
public Uri ValidateToUrlWithQueryMethod(string url, IEnumerable<KeyValuePair<string, object?>> parameters)
{
return url.ToUrlWithQuery(parameters);
}

private static IEnumerable<TestCaseData> ValidateToUrlWithQueryMethodTestCases()
{
yield return new TestCaseData("https://example.com", Array.Empty<KeyValuePair<string, object?>>()) { ExpectedResult = new Uri("https://example.com") };
yield return new TestCaseData("https://example.com", new KeyValuePair<string, object?>[] { new("foo", "bar" ) }) { ExpectedResult = new Uri("https://example.com?foo=bar") };
#pragma warning disable CA1861 // Avoid constant arrays as arguments
yield return new TestCaseData("https://example.com", new KeyValuePair<string, object?>[] { new("foo", new[] { "bar", "baz" } ) }) { ExpectedResult = new Uri("https://example.com?foo=bar,baz") };
#pragma warning restore CA1861 // Avoid constant arrays as arguments
yield return new TestCaseData("https://example.com", new KeyValuePair<string, object?>[] { new("=&?", "=?&=?&" ) }) { ExpectedResult = new Uri("https://example.com?%3D%26%3F=%3D%3F%26%3D%3F%26") };
yield return new TestCaseData("https://example.com", new KeyValuePair<string, object?>[] { new("foo", "bar" ), new("baz", "bat" ) }) { ExpectedResult = new Uri("https://example.com?foo=bar&baz=bat") };
yield return new TestCaseData("https://example.com?a=b", new KeyValuePair<string, object?>[] { new("foo", "bar" ), new("baz", "bat" ) }) { ExpectedResult = new Uri("https://example.com?a=b&foo=bar&baz=bat") };
}
}
1 change: 0 additions & 1 deletion src/Aydsko.iRacingData/Aydsko.iRacingData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
Expand Down
Loading

0 comments on commit 535cd4f

Please sign in to comment.