Skip to content

Commit

Permalink
Support types without object equal operator ( extend test for DateTim…
Browse files Browse the repository at this point in the history
…e, TimeSpan ) (#3152)

* Add test for DateTime and TimeSpan

~ set CultureInfo on test start
+ add test for DateTime
+ add test for TimeSpan

* Value type comparison

+ implement value type handling for expression compare
~ fix timespan test to use correct expecting values

* add more test & simplify

+ add more corner test
~ replace activator with expression default
~ replace member.reflectedtype with item.type
  • Loading branch information
puschie286 authored Nov 24, 2021
1 parent 5fb6697 commit ea5e6d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
31 changes: 27 additions & 4 deletions Tests/Blazorise.Tests/DataGrid/Utils/FunctionCompilerTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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" )]
Expand All @@ -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" )]
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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; }
}

Expand Down

0 comments on commit ea5e6d4

Please sign in to comment.