Selaa lähdekoodia

Added ToObject_DateTimeAsString

Piotr Czajkowski 1 viikko sitten
vanhempi
commit
5ef833a4b3

+ 3 - 0
ExcelORM/ExcelORM/TypeExtensions.cs

@@ -35,6 +35,9 @@ public static class TypeExtensions
         if (property.PropertyType == typeof(Guid) || property.PropertyType == typeof(Guid?))
             return HandleGuid(value, property);
 
+        if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
+            return DateTime.TryParse(value.GetText(), out var dateTime) ? dateTime : default;
+
         var nullableUnderlyingType = Nullable.GetUnderlyingType(property.PropertyType);
         if (property.PropertyType.IsEnum || (nullableUnderlyingType is { IsEnum: true }))
             return HandleEnum(value, property, nullableUnderlyingType);

+ 0 - 8
ExcelORM/ExcelORMTests/ReaderTests.cs

@@ -112,14 +112,6 @@ public class ReaderTests
         Assert.NotEmpty(results);
     }
     
-    [Fact]
-    public void BadDateThrows()
-    {
-        using var reader = new ExcelReader(BadDate);
-        var exception = Assert.Throws<ArgumentException>(() => reader.Read<TestTypes>().ToArray());
-        Assert.Contains("Location", exception.Message);
-    }
-    
     [Fact]
     public void ReadAdditionalTypes()
     {

+ 19 - 0
ExcelORM/ExcelORMTests/TypeExtensionsTests.cs

@@ -0,0 +1,19 @@
+using ClosedXML.Excel;
+using ExcelORM;
+
+namespace ExcelORMTests;
+
+public class TypeExtensionsTests
+{
+    public DateTime? DateTimeProperty { get; set; }
+    
+    [Fact]
+    public void ToObject_DateTimeAsString()
+    {
+        XLCellValue value = "7/27/2025";
+        
+        var propertyInfo = typeof(TypeExtensionsTests).GetProperty("DateTimeProperty");
+        var readValue = value.ToObject(propertyInfo);
+        Assert.IsType<DateTime>(readValue);
+    }
+}