mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:25:43 +00:00
parent
b7514fdead
commit
f7664ba8a8
@ -1,58 +0,0 @@
|
|||||||
namespace X10D.Tests.Core
|
|
||||||
{
|
|
||||||
using System;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tests for <see cref="ConvertibleExtensions" />.
|
|
||||||
/// </summary>
|
|
||||||
[TestClass]
|
|
||||||
public class ConvertibleTests
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Tests for <see cref="ConvertibleExtensions.To{T}" />.
|
|
||||||
/// </summary>
|
|
||||||
[TestMethod]
|
|
||||||
public void To()
|
|
||||||
{
|
|
||||||
Assert.AreEqual(2, "2".To<int>());
|
|
||||||
Assert.AreEqual("12.5", 12.50.To<string>());
|
|
||||||
Assert.IsTrue("True".To<bool>());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tests for <see cref="ConvertibleExtensions.ToOrDefault{T}(System.IConvertible, System.IFormatProvider)" />.
|
|
||||||
/// </summary>
|
|
||||||
[TestMethod]
|
|
||||||
public void ToOrDefault()
|
|
||||||
{
|
|
||||||
Assert.AreEqual(2, "2".ToOrDefault<int>());
|
|
||||||
Assert.AreEqual("12.5", 12.50.ToOrDefault<string>());
|
|
||||||
Assert.IsTrue("True".ToOrDefault<bool>());
|
|
||||||
Assert.ThrowsException<FormatException>(() => "Foo".ToOrDefault<double>());
|
|
||||||
Assert.IsTrue("1.5".ToOrDefault(out float f));
|
|
||||||
Assert.AreEqual(1.5f, f);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tests for <see cref="ConvertibleExtensions.ToOrNull{T}(System.IConvertible, System.IFormatProvider)" />.
|
|
||||||
/// </summary>
|
|
||||||
[TestMethod]
|
|
||||||
public void ToOrNull()
|
|
||||||
{
|
|
||||||
Assert.IsFalse("foo".ToOrNull(out ConvertibleTests t));
|
|
||||||
Assert.IsNull(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tests for <see cref="ConvertibleExtensions.ToOrOther{T}(System.IConvertible, T, System.IFormatProvider)" />.
|
|
||||||
/// </summary>
|
|
||||||
[TestMethod]
|
|
||||||
public void ToOrOther()
|
|
||||||
{
|
|
||||||
Assert.AreEqual(2.0, "Foo".ToOrOther(2.0));
|
|
||||||
Assert.IsFalse("Foo".ToOrOther(out var d, 2.0));
|
|
||||||
Assert.AreEqual(2.0, d);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,161 +0,0 @@
|
|||||||
namespace X10D
|
|
||||||
{
|
|
||||||
using System;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Extension methods for <see cref="IConvertible" />.
|
|
||||||
/// </summary>
|
|
||||||
public static class ConvertibleExtensions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the object to another type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
|
||||||
/// <param name="value">The object to convert.</param>
|
|
||||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
|
||||||
/// <returns>Returns the value converted to <see cref="T" />.</returns>
|
|
||||||
/// <exception cref="InvalidCastException">
|
|
||||||
/// This conversion is not supported.
|
|
||||||
/// -or-
|
|
||||||
/// <paramref name="value" /> is <see langword="null" /> and <typeparamref name="T" /> is a value type.
|
|
||||||
/// </exception>
|
|
||||||
[CLSCompliant(false)]
|
|
||||||
public static T To<T>(this IConvertible value, IFormatProvider provider = null)
|
|
||||||
{
|
|
||||||
if (value is null)
|
|
||||||
{
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (T)Convert.ChangeType(value, typeof(T), provider);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the object to another type, returning the default value on failure.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
|
||||||
/// <param name="value">The object to convert.</param>
|
|
||||||
/// <param name="provider">The format provider.</param>
|
|
||||||
/// <returns>Returns the value converted to <see cref="T" />.</returns>
|
|
||||||
/// <exception cref="InvalidCastException">This conversion is not supported.</exception>
|
|
||||||
[CLSCompliant(false)]
|
|
||||||
public static T ToOrDefault<T>(this IConvertible value, IFormatProvider provider = null)
|
|
||||||
{
|
|
||||||
return value is null ? default : To<T>(value, provider);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the object to another type, returning the default value on failure.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
|
||||||
/// <param name="value">The object to convert.</param>
|
|
||||||
/// <param name="newObj">The parameter where the result should be sent.</param>
|
|
||||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
|
||||||
/// <returns>Returns <see langword="true" /> on success, <see langword="true" /> on failure.</returns>
|
|
||||||
[CLSCompliant(false)]
|
|
||||||
public static bool ToOrDefault<T>(this IConvertible value, out T newObj, IFormatProvider provider = null)
|
|
||||||
{
|
|
||||||
if (value is null)
|
|
||||||
{
|
|
||||||
newObj = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
newObj = To<T>(value, provider);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (InvalidCastException)
|
|
||||||
{
|
|
||||||
newObj = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the object to another type, returning <see langword="null" /> on failure.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
|
||||||
/// <param name="value">The object to convert.</param>
|
|
||||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
|
||||||
/// <returns>Returns a <see cref="T" /> or <see langword="null" />.</returns>
|
|
||||||
[CLSCompliant(false)]
|
|
||||||
public static T ToOrNull<T>(this IConvertible value, IFormatProvider provider = null)
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
return value.ToOrNull(out T v, provider) ? v : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the object to another type, returning <see langword="null" /> on failure.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
|
||||||
/// <param name="value">The object to convert.</param>
|
|
||||||
/// <param name="newObj">The parameter where the result should be sent.</param>
|
|
||||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
|
||||||
/// <returns>Returns a <see cref="T" /> or <see langword="null" />.</returns>
|
|
||||||
[CLSCompliant(false)]
|
|
||||||
public static bool ToOrNull<T>(this IConvertible value, out T newObj, IFormatProvider provider = null)
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
return ToOrOther(value, out newObj, null, provider);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the object to another type, returning a different value on failure.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
|
||||||
/// <param name="value">The object to convert.</param>
|
|
||||||
/// <param name="other">The backup value.</param>
|
|
||||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
|
||||||
/// <returns>Returns the value converted to <see cref="T" />.</returns>
|
|
||||||
[CLSCompliant(false)]
|
|
||||||
public static T ToOrOther<T>(this IConvertible value, T other, IFormatProvider provider = null)
|
|
||||||
{
|
|
||||||
if (value is null)
|
|
||||||
{
|
|
||||||
return other;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return To<T>(value, provider);
|
|
||||||
}
|
|
||||||
catch (Exception ex) when (ex is InvalidCastException || ex is FormatException)
|
|
||||||
{
|
|
||||||
return other;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the object to another type, returning a different value on failure.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
|
||||||
/// <param name="value">The object to convert.</param>
|
|
||||||
/// <param name="newObj">The parameter where the result should be sent.</param>
|
|
||||||
/// <param name="other">The backup value.</param>
|
|
||||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
|
||||||
/// <returns>Returns <see langword="true" /> on success, <see langword="true" /> on failure.</returns>
|
|
||||||
[CLSCompliant(false)]
|
|
||||||
public static bool ToOrOther<T>(this IConvertible value, out T newObj, T other, IFormatProvider provider = null)
|
|
||||||
{
|
|
||||||
if (value is null)
|
|
||||||
{
|
|
||||||
newObj = other;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
newObj = To<T>(value, provider);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (Exception ex) when (ex is InvalidCastException || ex is FormatException)
|
|
||||||
{
|
|
||||||
newObj = other;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user