Skip to content

Commit

Permalink
feat(type-extensions): add support for converting additional numeric …
Browse files Browse the repository at this point in the history
…types

This commit adds support for converting additional numeric types:
- `sbyte`
- `ushort`
- `float`
- `double`
- `decimal`
  • Loading branch information
dmitrii-kiselev committed Sep 28, 2024
1 parent 8aecbf6 commit de776cd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 19 deletions.
15 changes: 14 additions & 1 deletion src/EFCoreSecondLevelCacheInterceptor/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public static class TypeExtensions
/// </summary>
public static readonly Type ByteType = typeof(byte);

/// <summary>
/// Cached version of typeof(sbyte)
/// </summary>
public static readonly Type SByteType = typeof(sbyte);

/// <summary>
/// Cached version of typeof(string)
/// </summary>
Expand Down Expand Up @@ -63,6 +68,11 @@ public static class TypeExtensions
/// </summary>
public static readonly Type ShortType = typeof(short);

/// <summary>
/// Cached version of typeof(ushort)
/// </summary>
public static readonly Type UShortType = typeof(ushort);

/// <summary>
/// Cached version of typeof(int)
/// </summary>
Expand Down Expand Up @@ -112,7 +122,10 @@ public static bool IsArrayOrGenericList(Type? expectedValueType) =>
public static bool IsNumber(Type type) =>
type == UintType || type == IntType ||
type == UlongTYpe || type == LongType ||
type == ShortType || type == ByteType || type == CharType;
type == UShortType || type == ShortType ||
type == SByteType || type == ByteType ||
type == FloatType || type == DoubleType || type == DecimalType ||
type == CharType;

#if NET8_0 || NET7_0 || NET6_0
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1386,17 +1386,22 @@ public void GetFieldValue_ShouldReturnExpectedTimeSpanValueFromNumber()
public static IEnumerable<object[]> ValidNumberData =>
new List<object[]>
{
new object[] { (sbyte)1 },
new object[] { (byte)1 },
new object[] { (short)1 },
new object[] { (ushort)1 },
new object[] { 1 },
new object[] { (uint)1 },
new object[] { 1U },
new object[] { 1L },
new object[] { 1UL },
new object[] { (short)1 }
new object[] { 1F },
new object[] { 1D },
new object[] { 1M }
};

[Theory]
[MemberData(nameof(ValidNumberData))]
public void GetFieldValue_ShouldReturnExpectedNumber(object value)
public void GetFieldValue_ShouldReturnExpectedDecimalNumber(object value)
{
// Arrange
var values = new List<object> { value };
Expand All @@ -1414,23 +1419,69 @@ public void GetFieldValue_ShouldReturnExpectedNumber(object value)
dataReader.Read();

// Act
var actual = dataReader.GetFieldValue<long>(0);
var actual = dataReader.GetFieldValue<decimal>(0);

// Assert
Assert.Equal(1L, actual);
Assert.Equal(1M, actual);
}

[Fact]
public void GetFieldValue_ShouldReturnExpectedByteNumberFromChar()
{
// Arrange
var values = new List<object> { '1' };
var tableRow = new EFTableRow(values);
var tableRows = new EFTableRows
{
Rows = new List<EFTableRow> { tableRow },
ColumnsInfo = new Dictionary<int, EFTableColumnInfo>
{
{ 0, new EFTableColumnInfo { DbTypeName = nameof(Char), Ordinal = 0 } }
}
};
var dataReader = new EFTableRowsDataReader(tableRows);

dataReader.Read();

// Act
var actual = dataReader.GetFieldValue<byte>(0);

// Assert
Assert.Equal(49, actual);
}

[Theory]
[MemberData(nameof(ValidNumberData))]
public void GetFieldValue_ShouldNotThrowInvalidCastExceptionWhenConvertToDecimal(object value)
{
// Arrange
var values = new List<object> { value };
var tableRow = new EFTableRow(values);
var tableRows = new EFTableRows
{
Rows = new List<EFTableRow> { tableRow },
ColumnsInfo = new Dictionary<int, EFTableColumnInfo>
{
{ 0, new EFTableColumnInfo { DbTypeName = value.GetType().Name, Ordinal = 0 } }
}
};
var dataReader = new EFTableRowsDataReader(tableRows);

dataReader.Read();

// Act
var exception = Record.Exception(() => dataReader.GetFieldValue<decimal>(0));

// Assert
Assert.Null(exception);
}

public static IEnumerable<object[]> InvalidNumberData =>
new List<object[]>
{
new object[] { (sbyte)1 },
new object[] { "1" },
new object[] { 1M },
new object[] { 1.0 },
new object[] { 1.0F },
new object[] { (nint)1 },
new object[] { (nuint)1 },
new object[] { (ushort)1 }
new object[] { "1" }
};

[Theory]
Expand All @@ -1453,7 +1504,7 @@ public void GetFieldValue_ThrowsInvalidCastException_WhenValueIsNotNumber(object
dataReader.Read();

// Act
void Act() => dataReader.GetFieldValue<long>(0);
void Act() => dataReader.GetFieldValue<decimal>(0);

// Assert
Assert.Throws<InvalidCastException>(Act);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ public void IsArrayOrGenericList_ShouldReturnExpectedResult(Type type, bool expe
[InlineData(null, false)]
[InlineData(typeof(bool), false)]
[InlineData(typeof(byte), true)]
[InlineData(typeof(sbyte), false)]
[InlineData(typeof(sbyte), true)]
[InlineData(typeof(char), true)]
[InlineData(typeof(decimal), false)]
[InlineData(typeof(double), false)]
[InlineData(typeof(float), false)]
[InlineData(typeof(decimal), true)]
[InlineData(typeof(double), true)]
[InlineData(typeof(float), true)]
[InlineData(typeof(int), true)]
[InlineData(typeof(uint), true)]
[InlineData(typeof(nint), false)]
[InlineData(typeof(nuint), false)]
[InlineData(typeof(long), true)]
[InlineData(typeof(ulong), true)]
[InlineData(typeof(short), true)]
[InlineData(typeof(ushort), false)]
[InlineData(typeof(ushort), true)]
[InlineData(typeof(DateOnly), false)]
[InlineData(typeof(DateTime), false)]
[InlineData(typeof(DateTimeOffset), false)]
Expand Down

0 comments on commit de776cd

Please sign in to comment.