diff --git a/Source/Extensions/Blazorise.DataGrid/Utils/FunctionCompiler.cs b/Source/Extensions/Blazorise.DataGrid/Utils/FunctionCompiler.cs index 0fe67a501c..6aa686a44d 100644 --- a/Source/Extensions/Blazorise.DataGrid/Utils/FunctionCompiler.cs +++ b/Source/Extensions/Blazorise.DataGrid/Utils/FunctionCompiler.cs @@ -41,7 +41,7 @@ private static Expression GetSafePropertyOrField( Expression item, string proper if ( field == null ) throw new ArgumentException( $"Cannot detect the member of {item.Type}", propertyOrFieldName ); - field = Expression.Condition( Expression.Equal( item, Expression.Constant( null ) ), + field = Expression.Condition( Expression.Equal( item, Expression.Default( item.Type ) ), IsNullable( field.Type ) ? Expression.Constant( null, field.Type ) : Expression.Default( field.Type ), field ); diff --git a/Tests/Blazorise.Tests/DataGrid/Utils/FunctionCompilerTests.cs b/Tests/Blazorise.Tests/DataGrid/Utils/FunctionCompilerTests.cs index 31c96139b8..7bf3ff9128 100644 --- a/Tests/Blazorise.Tests/DataGrid/Utils/FunctionCompilerTests.cs +++ b/Tests/Blazorise.Tests/DataGrid/Utils/FunctionCompilerTests.cs @@ -1,6 +1,5 @@ -using System.Collections; -using System.Collections.Generic; -using Blazorise.DataGrid; +using System; +using System.Globalization; using Blazorise.DataGrid.Utils; using Xunit; @@ -9,10 +8,15 @@ namespace Blazorise.Tests.DataGrid.Utils public class FunctionCompilerTests { [Theory] - [InlineData("Id", "0")] + [InlineData( "Id", "0" )] [InlineData( "Name", null )] [InlineData( "Value", null )] [InlineData( "Boolean", "False" )] + [InlineData( "DateTime", "1/1/0001 12:00:00 AM" )] + [InlineData( "DateTime.TimeOfDay", "00:00:00" )] + [InlineData( "DateTimeNull", null )] + [InlineData( "TimeSpan", "00:00:00" )] + [InlineData( "TimeSpanNull", null )] [InlineData( "Information.Id", "0" )] [InlineData( "Information.Message", null )] [InlineData( "Information.Detail.Id", "0" )] @@ -30,6 +34,11 @@ public void ValueGetter_ReturnsCorrectPropertyOrField_DefaultValue(string field, [InlineData( "Name", "John" )] [InlineData( "Value", "200" )] [InlineData( "Boolean", "True" )] + [InlineData( "DateTime", "12/31/9999 11:59:59 PM" )] + [InlineData( "DateTime.TimeOfDay", "23:59:59.9999999" )] + [InlineData( "DateTimeNull", "8/18/2018 12:00:00 AM" )] + [InlineData( "TimeSpan", "10675199.02:48:05.4775807" )] + [InlineData( "TimeSpanNull", "20:00:00" )] [InlineData( "Information.Id", "1000" )] [InlineData( "Information.Message", "This is a message!" )] [InlineData( "Information.Detail.Id", "2000" )] @@ -42,6 +51,12 @@ public void ValueGetter_ReturnsCorrectPropertyOrField_ActualValue( string field, Assert.Equal( expected, valueGetter( test )?.ToString() ); } + public FunctionCompilerTests() + { + // force to use us culture info + CultureInfo.CurrentCulture = new CultureInfo( "en-US" ); + } + private Test GetTest() { return new() @@ -50,6 +65,10 @@ private Test GetTest() Name = "John", Value = 200, Boolean = true, + DateTime = DateTime.MaxValue, + DateTimeNull = DateTime.Parse( "08/18/2018" ), + TimeSpan = TimeSpan.MaxValue, + TimeSpanNull = TimeSpan.FromHours( 20.0 ), Information = new() { Id = 1000, @@ -71,6 +90,10 @@ private class Test public string Name { get; set; } public int? Value { get; set; } public bool Boolean { get; set; } + public DateTime DateTime { get; set; } + public DateTime? DateTimeNull { get; set; } + public TimeSpan TimeSpan { get; set; } + public TimeSpan? TimeSpanNull { get; set; } public Information Information { get; set; } }