Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support types without object equal operator ( extend test for DateTime, TimeSpan ) #3152

Merged
merged 3 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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