Merge pull request #6150 from Bond-009/proptest
This commit is contained in:
commit
1c6bf16d15
|
@ -17,7 +17,8 @@ namespace MediaBrowser.Model.Extensions
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (char.IsUpper(str[0]))
|
// We check IsLower instead of IsUpper because both return false for non-letters
|
||||||
|
if (!char.IsLower(str[0]))
|
||||||
{
|
{
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
|
||||||
<PackageReference Include="coverlet.collector" Version="3.0.3" />
|
<PackageReference Include="coverlet.collector" Version="3.0.3" />
|
||||||
|
<PackageReference Include="FsCheck.Xunit" Version="2.15.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Code Analyzers -->
|
<!-- Code Analyzers -->
|
||||||
|
|
|
@ -1,34 +1,45 @@
|
||||||
using System.Text.Json;
|
using System.Globalization;
|
||||||
|
using System.Text.Json;
|
||||||
|
using FsCheck;
|
||||||
|
using FsCheck.Xunit;
|
||||||
using MediaBrowser.Common.Json.Converters;
|
using MediaBrowser.Common.Json.Converters;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Jellyfin.Common.Tests.Json
|
namespace Jellyfin.Common.Tests.Json
|
||||||
{
|
{
|
||||||
public static class JsonBoolNumberTests
|
public class JsonBoolNumberTests
|
||||||
{
|
{
|
||||||
|
private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions()
|
||||||
|
{
|
||||||
|
Converters =
|
||||||
|
{
|
||||||
|
new JsonBoolNumberConverter()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("1", true)]
|
[InlineData("1", true)]
|
||||||
[InlineData("0", false)]
|
[InlineData("0", false)]
|
||||||
[InlineData("2", true)]
|
[InlineData("2", true)]
|
||||||
[InlineData("true", true)]
|
[InlineData("true", true)]
|
||||||
[InlineData("false", false)]
|
[InlineData("false", false)]
|
||||||
public static void Deserialize_Number_Valid_Success(string input, bool? output)
|
public void Deserialize_Number_Valid_Success(string input, bool? output)
|
||||||
{
|
{
|
||||||
var options = new JsonSerializerOptions();
|
var value = JsonSerializer.Deserialize<bool>(input, _jsonOptions);
|
||||||
options.Converters.Add(new JsonBoolNumberConverter());
|
|
||||||
var value = JsonSerializer.Deserialize<bool>(input, options);
|
|
||||||
Assert.Equal(value, output);
|
Assert.Equal(value, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(true, "true")]
|
[InlineData(true, "true")]
|
||||||
[InlineData(false, "false")]
|
[InlineData(false, "false")]
|
||||||
public static void Serialize_Bool_Success(bool input, string output)
|
public void Serialize_Bool_Success(bool input, string output)
|
||||||
{
|
{
|
||||||
var options = new JsonSerializerOptions();
|
var value = JsonSerializer.Serialize(input, _jsonOptions);
|
||||||
options.Converters.Add(new JsonBoolNumberConverter());
|
|
||||||
var value = JsonSerializer.Serialize(input, options);
|
|
||||||
Assert.Equal(value, output);
|
Assert.Equal(value, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Property]
|
||||||
|
public Property Deserialize_NonZeroInt_True(NonZeroInt input)
|
||||||
|
=> JsonSerializer.Deserialize<bool>(input.ToString(), _jsonOptions).ToProperty();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,14 +6,13 @@ namespace Jellyfin.Common.Tests.Json
|
||||||
{
|
{
|
||||||
public class JsonStringConverterTests
|
public class JsonStringConverterTests
|
||||||
{
|
{
|
||||||
private readonly JsonSerializerOptions _jsonSerializerOptions
|
private readonly JsonSerializerOptions _jsonSerializerOptions = new ()
|
||||||
= new ()
|
{
|
||||||
|
Converters =
|
||||||
{
|
{
|
||||||
Converters =
|
new JsonStringConverter()
|
||||||
{
|
}
|
||||||
new JsonStringConverter()
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("\"test\"", "test")]
|
[InlineData("\"test\"", "test")]
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
using System;
|
||||||
|
using FsCheck;
|
||||||
|
using FsCheck.Xunit;
|
||||||
using MediaBrowser.Model.Extensions;
|
using MediaBrowser.Model.Extensions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
@ -10,9 +13,20 @@ namespace Jellyfin.Model.Tests.Extensions
|
||||||
[InlineData("banana", "Banana")]
|
[InlineData("banana", "Banana")]
|
||||||
[InlineData("Banana", "Banana")]
|
[InlineData("Banana", "Banana")]
|
||||||
[InlineData("ä", "Ä")]
|
[InlineData("ä", "Ä")]
|
||||||
|
[InlineData("\027", "\027")]
|
||||||
public void StringHelper_ValidArgs_Success(string input, string expectedResult)
|
public void StringHelper_ValidArgs_Success(string input, string expectedResult)
|
||||||
{
|
{
|
||||||
Assert.Equal(expectedResult, StringHelper.FirstToUpper(input));
|
Assert.Equal(expectedResult, StringHelper.FirstToUpper(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Property]
|
||||||
|
public Property FirstToUpper_RandomArg_Correct(NonEmptyString input)
|
||||||
|
{
|
||||||
|
var result = StringHelper.FirstToUpper(input.Item);
|
||||||
|
|
||||||
|
// We check IsLower instead of IsUpper because both return false for non-letters
|
||||||
|
return (!char.IsLower(result[0])).Label("First char is uppercase")
|
||||||
|
.And(input.Item.Length == 1 || result[1..].Equals(input.Item[1..], StringComparison.Ordinal)).Label("Remaining chars are unmodified");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||||
<PackageReference Include="coverlet.collector" Version="3.0.3" />
|
<PackageReference Include="coverlet.collector" Version="3.0.3" />
|
||||||
|
<PackageReference Include="FsCheck.Xunit" Version="2.15.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Code Analyzers -->
|
<!-- Code Analyzers -->
|
||||||
|
|
53
tests/Jellyfin.Networking.Tests/IPHostTests.cs
Normal file
53
tests/Jellyfin.Networking.Tests/IPHostTests.cs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
using FsCheck;
|
||||||
|
using FsCheck.Xunit;
|
||||||
|
using MediaBrowser.Common.Net;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Jellyfin.Networking.Tests
|
||||||
|
{
|
||||||
|
public static class IPHostTests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Checks IP address formats.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">IP Address.</param>
|
||||||
|
[Theory]
|
||||||
|
[InlineData("127.0.0.1")]
|
||||||
|
[InlineData("127.0.0.1:123")]
|
||||||
|
[InlineData("localhost")]
|
||||||
|
[InlineData("localhost:1345")]
|
||||||
|
[InlineData("www.google.co.uk")]
|
||||||
|
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
|
||||||
|
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
|
||||||
|
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]:124")]
|
||||||
|
[InlineData("fe80::7add:12ff:febb:c67b%16")]
|
||||||
|
[InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
|
||||||
|
[InlineData("fe80::7add:12ff:febb:c67b%16:123")]
|
||||||
|
[InlineData("[fe80::7add:12ff:febb:c67b%16]")]
|
||||||
|
[InlineData("192.168.1.2/255.255.255.0")]
|
||||||
|
[InlineData("192.168.1.2/24")]
|
||||||
|
public static void TryParse_ValidHostStrings_True(string address)
|
||||||
|
=> Assert.True(IPHost.TryParse(address, out _));
|
||||||
|
|
||||||
|
[Property]
|
||||||
|
public static Property TryParse_IPv4Address_True(IPv4Address address)
|
||||||
|
=> IPHost.TryParse(address.Item.ToString(), out _).ToProperty();
|
||||||
|
|
||||||
|
[Property]
|
||||||
|
public static Property TryParse_IPv6Address_True(IPv6Address address)
|
||||||
|
=> IPHost.TryParse(address.Item.ToString(), out _).ToProperty();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// All should be invalid address strings.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">Invalid address strings.</param>
|
||||||
|
[Theory]
|
||||||
|
[InlineData("256.128.0.0.0.1")]
|
||||||
|
[InlineData("127.0.0.1#")]
|
||||||
|
[InlineData("localhost!")]
|
||||||
|
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
|
||||||
|
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231]")]
|
||||||
|
public static void TryParse_InvalidAddressString_False(string address)
|
||||||
|
=> Assert.False(IPHost.TryParse(address, out _));
|
||||||
|
}
|
||||||
|
}
|
49
tests/Jellyfin.Networking.Tests/IPNetAddressTests.cs
Normal file
49
tests/Jellyfin.Networking.Tests/IPNetAddressTests.cs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
using FsCheck;
|
||||||
|
using FsCheck.Xunit;
|
||||||
|
using MediaBrowser.Common.Net;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Jellyfin.Networking.Tests
|
||||||
|
{
|
||||||
|
public static class IPNetAddressTests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Checks IP address formats.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">IP Address.</param>
|
||||||
|
[Theory]
|
||||||
|
[InlineData("127.0.0.1")]
|
||||||
|
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
|
||||||
|
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
|
||||||
|
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]")]
|
||||||
|
[InlineData("fe80::7add:12ff:febb:c67b%16")]
|
||||||
|
[InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
|
||||||
|
[InlineData("fe80::7add:12ff:febb:c67b%16:123")]
|
||||||
|
[InlineData("[fe80::7add:12ff:febb:c67b%16]")]
|
||||||
|
[InlineData("192.168.1.2/255.255.255.0")]
|
||||||
|
[InlineData("192.168.1.2/24")]
|
||||||
|
public static void TryParse_ValidIPStrings_True(string address)
|
||||||
|
=> Assert.True(IPNetAddress.TryParse(address, out _));
|
||||||
|
|
||||||
|
[Property]
|
||||||
|
public static Property TryParse_IPv4Address_True(IPv4Address address)
|
||||||
|
=> IPNetAddress.TryParse(address.Item.ToString(), out _).ToProperty();
|
||||||
|
|
||||||
|
[Property]
|
||||||
|
public static Property TryParse_IPv6Address_True(IPv6Address address)
|
||||||
|
=> IPNetAddress.TryParse(address.Item.ToString(), out _).ToProperty();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// All should be invalid address strings.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">Invalid address strings.</param>
|
||||||
|
[Theory]
|
||||||
|
[InlineData("256.128.0.0.0.1")]
|
||||||
|
[InlineData("127.0.0.1#")]
|
||||||
|
[InlineData("localhost!")]
|
||||||
|
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
|
||||||
|
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231]")]
|
||||||
|
public static void TryParse_InvalidAddressString_False(string address)
|
||||||
|
=> Assert.False(IPNetAddress.TryParse(address, out _));
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,7 @@
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||||
<PackageReference Include="coverlet.collector" Version="3.0.3" />
|
<PackageReference Include="coverlet.collector" Version="3.0.3" />
|
||||||
|
<PackageReference Include="FsCheck.Xunit" Version="2.15.3" />
|
||||||
<PackageReference Include="Moq" Version="4.16.1" />
|
<PackageReference Include="Moq" Version="4.16.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -56,66 +56,6 @@ namespace Jellyfin.Networking.Tests
|
||||||
Assert.Equal(nm.GetInternalBindAddresses().AsString(), value);
|
Assert.Equal(nm.GetInternalBindAddresses().AsString(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks IP address formats.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="address">IP Address.</param>
|
|
||||||
[Theory]
|
|
||||||
[InlineData("127.0.0.1")]
|
|
||||||
[InlineData("127.0.0.1:123")]
|
|
||||||
[InlineData("localhost")]
|
|
||||||
[InlineData("localhost:1345")]
|
|
||||||
[InlineData("www.google.co.uk")]
|
|
||||||
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
|
|
||||||
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
|
|
||||||
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]:124")]
|
|
||||||
[InlineData("fe80::7add:12ff:febb:c67b%16")]
|
|
||||||
[InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
|
|
||||||
[InlineData("fe80::7add:12ff:febb:c67b%16:123")]
|
|
||||||
[InlineData("[fe80::7add:12ff:febb:c67b%16]")]
|
|
||||||
[InlineData("192.168.1.2/255.255.255.0")]
|
|
||||||
[InlineData("192.168.1.2/24")]
|
|
||||||
public void ValidHostStrings(string address)
|
|
||||||
{
|
|
||||||
Assert.True(IPHost.TryParse(address, out _));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks IP address formats.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="address">IP Address.</param>
|
|
||||||
[Theory]
|
|
||||||
[InlineData("127.0.0.1")]
|
|
||||||
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
|
|
||||||
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
|
|
||||||
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]")]
|
|
||||||
[InlineData("fe80::7add:12ff:febb:c67b%16")]
|
|
||||||
[InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
|
|
||||||
[InlineData("fe80::7add:12ff:febb:c67b%16:123")]
|
|
||||||
[InlineData("[fe80::7add:12ff:febb:c67b%16]")]
|
|
||||||
[InlineData("192.168.1.2/255.255.255.0")]
|
|
||||||
[InlineData("192.168.1.2/24")]
|
|
||||||
public void ValidIPStrings(string address)
|
|
||||||
{
|
|
||||||
Assert.True(IPNetAddress.TryParse(address, out _));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// All should be invalid address strings.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="address">Invalid address strings.</param>
|
|
||||||
[Theory]
|
|
||||||
[InlineData("256.128.0.0.0.1")]
|
|
||||||
[InlineData("127.0.0.1#")]
|
|
||||||
[InlineData("localhost!")]
|
|
||||||
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
|
|
||||||
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231]")]
|
|
||||||
public void InvalidAddressString(string address)
|
|
||||||
{
|
|
||||||
Assert.False(IPNetAddress.TryParse(address, out _));
|
|
||||||
Assert.False(IPHost.TryParse(address, out _));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test collection parsing.
|
/// Test collection parsing.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user