Skip to content

Commit

Permalink
IsSimpleType si now extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidFarahmandian committed Mar 9, 2025
1 parent 59badfe commit 7c062bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static IEnumerable<PropertyInfo> GetDeclaredProperties(Type t) =>
/// </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)
public static bool IsSimpleType(this Type? t)
{
while (true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,41 @@ 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)));
Assert.IsTrue(typeof(int).IsSimpleType());
Assert.IsTrue(typeof(bool).IsSimpleType());
Assert.IsTrue(typeof(char).IsSimpleType());
Assert.IsTrue(typeof(double).IsSimpleType());
}

[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)));
Assert.IsTrue(typeof(decimal).IsSimpleType());
Assert.IsTrue(typeof(string).IsSimpleType());
Assert.IsTrue(typeof(Guid).IsSimpleType());
Assert.IsTrue(typeof(DateTime).IsSimpleType());
Assert.IsTrue(typeof(byte[]).IsSimpleType());
Assert.IsTrue(typeof(MyEnum).IsSimpleType());
}

[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?)));
Assert.IsTrue(typeof(int?).IsSimpleType());
Assert.IsTrue(typeof(decimal?).IsSimpleType());
Assert.IsTrue(typeof(Guid?).IsSimpleType());
Assert.IsTrue(typeof(DateTime?).IsSimpleType());
Assert.IsTrue(typeof(MyEnum?).IsSimpleType());
}

[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[]
Assert.IsFalse(typeof(object).IsSimpleType());
Assert.IsFalse(typeof(MyClass).IsSimpleType());
Assert.IsFalse(typeof(MyStruct).IsSimpleType());
Assert.IsFalse(typeof(int[,]).IsSimpleType()); // multidimensional array
Assert.IsFalse(typeof(int[]).IsSimpleType()); // one dimensional array that is not byte[]
}

[TestMethod]
Expand Down Expand Up @@ -76,7 +76,7 @@ public void Should_return_all_writable_primitive_properties()
var result = typeof(TestClass).GetWritableProperties();

Assert.IsNotNull(result);
Assert.IsTrue(result.Any());
Assert.IsTrue(result.Count != 0);
Assert.AreEqual(expectedResult.First(), result.First());
}
}

0 comments on commit 7c062bc

Please sign in to comment.