parent
92f50054b2
commit
c717425549
@ -1,45 +0,0 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Jellyfin.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Provides extensions methods for <see cref="Type" />.
|
||||
/// </summary>
|
||||
public static class TypeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if the supplied value is the default or null value for that type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value to compare.</typeparam>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <param name="value">The value to check.</param>
|
||||
/// <returns><see langword="true"/> if the value is the default for the type. Otherwise, <see langword="false"/>.</returns>
|
||||
public static bool IsNullOrDefault<T>(this Type type, T value)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
object? tmp = value;
|
||||
object? defaultValue = type.IsValueType ? Activator.CreateInstance(type) : null;
|
||||
if (type.IsAssignableTo(typeof(IConvertible)))
|
||||
{
|
||||
tmp = Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return Equals(tmp, defaultValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the object is currently a default or null value. Boxed types will be unboxed prior to comparison.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to check.</param>
|
||||
/// <returns><see langword="true"/> if the value is the default for the type. Otherwise, <see langword="false"/>.</returns>
|
||||
public static bool IsNullOrDefault(this object? obj)
|
||||
{
|
||||
// Unbox the type and check.
|
||||
return obj?.GetType().IsNullOrDefault(obj) ?? true;
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Extensions.Tests
|
||||
{
|
||||
public class TypeExtensionsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(typeof(byte), byte.MaxValue, false)]
|
||||
[InlineData(typeof(short), short.MinValue, false)]
|
||||
[InlineData(typeof(ushort), ushort.MaxValue, false)]
|
||||
[InlineData(typeof(int), int.MinValue, false)]
|
||||
[InlineData(typeof(uint), uint.MaxValue, false)]
|
||||
[InlineData(typeof(long), long.MinValue, false)]
|
||||
[InlineData(typeof(ulong), ulong.MaxValue, false)]
|
||||
[InlineData(typeof(decimal), -1.0, false)]
|
||||
[InlineData(typeof(bool), true, false)]
|
||||
[InlineData(typeof(char), 'a', false)]
|
||||
[InlineData(typeof(string), "", false)]
|
||||
[InlineData(typeof(object), 1, false)]
|
||||
[InlineData(typeof(byte), 0, true)]
|
||||
[InlineData(typeof(short), 0, true)]
|
||||
[InlineData(typeof(ushort), 0, true)]
|
||||
[InlineData(typeof(int), 0, true)]
|
||||
[InlineData(typeof(uint), 0, true)]
|
||||
[InlineData(typeof(long), 0, true)]
|
||||
[InlineData(typeof(ulong), 0, true)]
|
||||
[InlineData(typeof(decimal), 0, true)]
|
||||
[InlineData(typeof(bool), false, true)]
|
||||
[InlineData(typeof(char), '\x0000', true)]
|
||||
[InlineData(typeof(string), null, true)]
|
||||
[InlineData(typeof(object), null, true)]
|
||||
[InlineData(typeof(PhonyClass), null, true)]
|
||||
[InlineData(typeof(DateTime), null, true)] // Special case handled within the test.
|
||||
[InlineData(typeof(DateTime), null, false)] // Special case handled within the test.
|
||||
[InlineData(typeof(byte?), null, true)]
|
||||
[InlineData(typeof(short?), null, true)]
|
||||
[InlineData(typeof(ushort?), null, true)]
|
||||
[InlineData(typeof(int?), null, true)]
|
||||
[InlineData(typeof(uint?), null, true)]
|
||||
[InlineData(typeof(long?), null, true)]
|
||||
[InlineData(typeof(ulong?), null, true)]
|
||||
[InlineData(typeof(decimal?), null, true)]
|
||||
[InlineData(typeof(bool?), null, true)]
|
||||
[InlineData(typeof(char?), null, true)]
|
||||
public void IsNullOrDefault_Matches_Expected(Type type, object? value, bool expectedResult)
|
||||
{
|
||||
if (type == typeof(DateTime))
|
||||
{
|
||||
if (expectedResult)
|
||||
{
|
||||
value = default(DateTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal(expectedResult, type.IsNullOrDefault(value));
|
||||
Assert.Equal(expectedResult, value.IsNullOrDefault());
|
||||
}
|
||||
|
||||
private class PhonyClass
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue