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

Fix for parsing Guid and string in the same condition #200

Merged
merged 23 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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 @@ -13,9 +13,13 @@ internal interface IEqualitySignatures : IRelationalSignatures

void F(Guid x, Guid y);
void F(Guid? x, Guid? y);
void F(Guid x, string y);
void F(Guid? x, string y);
void F(string x, Guid y);
void F(string x, Guid? y);

// Disabled 2 lines below according to unit test ParseLambda_With_Guid_Equals_Null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change comment to:

// Disabled 4 lines below because of : https://github.com/StefH/System.Linq.Dynamic.Core/pull/200
//void F(Guid x, string y);
//void F(Guid? x, string y);
//void F(string x, Guid y);
//void F(string x, Guid? y);

//void F(Guid x, string y);
//void F(Guid? x, string y);

// Disabled 2 lines below according to unit test ParseLambda_With_Null_Equals_Guid
//void F(string x, Guid y);
//void F(string x, Guid? y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace System.Linq.Dynamic.Core.Tests
{
public class DynamicExpressionParserTests
{


private class MyClass
{
public int Foo()
Expand Down Expand Up @@ -452,5 +454,56 @@ public void ParseLambda_With_InnerStringLiteral()
object result = del.DynamicInvoke(String.Empty);
Check.That(result).IsEqualTo(originalTrueValue);
}

[Fact]
public void ParseLambda_With_Guid_Equals_Null()
{
var user = new User();
Guid guidEmpty = Guid.Empty;
Guid someId = Guid.NewGuid();
string expressionText = $"iif(@0.Id == null, @0.Id == Guid.Parse(\"{someId}\"), Id == Id)";

var lambda = DynamicExpressionParser.ParseLambda(typeof(User), null, expressionText, user);
var boolLambda = lambda as Expression<Func<User, bool>>;
Assert.NotNull(boolLambda);

var del = lambda.Compile();
bool result = (bool) del.DynamicInvoke(user);
Assert.True(result);
}

[Fact]
public void ParseLambda_With_Null_Equals_Guid()
{
var user = new User();
Guid guidEmpty = Guid.Empty;
Guid someId = Guid.NewGuid();
string expressionText = $"iif(null == @0.Id, @0.Id == Guid.Parse(\"{someId}\"), Id == Id)";

var lambda = DynamicExpressionParser.ParseLambda(typeof(User), null, expressionText, user);
var boolLambda = lambda as Expression<Func<User, bool>>;
Assert.NotNull(boolLambda);

var del = lambda.Compile();
bool result = (bool) del.DynamicInvoke(user);
Assert.True(result);
}

[Fact]
public void ParseLambda_With_Guid_Equals_String()
{
Guid someId = Guid.NewGuid();
Guid anotherId = Guid.NewGuid();
var user = new User();
user.Id = someId;
Guid guidEmpty = Guid.Empty;
string expressionText = $"iif(@0.Id == \"{someId}\", Guid.Parse(\"{guidEmpty}\"), Guid.Parse(\"{anotherId}\"))";

var lambda = DynamicExpressionParser.ParseLambda(typeof(User), null, expressionText, user);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not also check here on:

var boolLambda = lambda as Expression<Func<User, bool>>;
Assert.NotNull(boolLambda);

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I've added it:

var guidLambda = lambda as Expression<Func<User, Guid>>;
Assert.NotNull(guidLambda);

var del = lambda.Compile();
Guid result = (Guid) del.DynamicInvoke(user);
Assert.Equal(guidEmpty, result);
}
}
}