Skip to content

Commit

Permalink
IsSimpleType added to PropertiesExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidFarahmandian committed Mar 9, 2025
1 parent f01f2d3 commit 59badfe
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ private static IEnumerable<PropertyInfo> GetDeclaredProperties(Type t) =>
t.GetTypeInfo().DeclaredProperties;
#endif

private static bool IsSimpleType(Type? t)
/// <summary>
/// Determines whether a given type is considered a simple type.
/// Simple types include primitive types, decimal, string, Guid, DateTime, enums, byte arrays, and their nullable counterparts.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>True if the type is a simple type; otherwise, false.</returns>
public static bool IsSimpleType(Type? t)
{
while (true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,56 @@
[TestClass]
public class PropertiesExtensionsTests
{
enum MyEnum { Value1, Value2 }
class MyClass { }
struct MyStruct { public int Value; }

[TestMethod]
public void IsSimpleType_PrimitiveTypes_ReturnsTrue()
{
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(int)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(bool)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(char)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(double)));
}

[TestMethod]
public void IsSimpleType_DecimalStringGuidDateTimeByteArrayEnum_ReturnsTrue()
{
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(decimal)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(string)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(Guid)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(DateTime)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(byte[])));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(MyEnum)));
}

[TestMethod]
public void IsSimpleType_NullableTypes_ReturnsTrue()
{
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(int?)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(decimal?)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(Guid?)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(DateTime?)));
Assert.IsTrue(PropertiesExtensions.IsSimpleType(typeof(MyEnum?)));
}

[TestMethod]
public void IsSimpleType_NonSimpleTypes_ReturnsFalse()
{
Assert.IsFalse(PropertiesExtensions.IsSimpleType(typeof(object)));
Assert.IsFalse(PropertiesExtensions.IsSimpleType(typeof(MyClass)));
Assert.IsFalse(PropertiesExtensions.IsSimpleType(typeof(MyStruct)));
Assert.IsFalse(PropertiesExtensions.IsSimpleType(typeof(int[,]))); // multidimensional array
Assert.IsFalse(PropertiesExtensions.IsSimpleType(typeof(int[]))); // one dimensional array that is not byte[]
}

[TestMethod]
public void IsSimpleType_NullType_ReturnsFalse()
{
Assert.IsFalse(PropertiesExtensions.IsSimpleType(null));
}

[TestMethod]
public void Should_return_true_for_nullable_type()
{
Expand Down

0 comments on commit 59badfe

Please sign in to comment.