-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Changes from 22 commits
eca21da
4b7d15a
cd4b420
c7d29f4
e6ddce6
9a5e5e3
02bda33
e77c116
d7d432b
2c840f8
8590347
e13c9d1
6213fd1
4d6a4d5
43765a3
10bb5bd
a9c93f2
cc90915
ea0f3bd
1290d20
53c73ac
d143a0b
dc9e24d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,57 @@ public class CustomClassWithStaticMethod | |
public static int GetAge(int x) => x; | ||
} | ||
|
||
public class CustomTextClass | ||
{ | ||
public CustomTextClass(string origin) | ||
{ | ||
Origin = origin; | ||
} | ||
|
||
public string Origin { get; } | ||
|
||
public static implicit operator string(CustomTextClass customTextValue) | ||
{ | ||
return customTextValue?.Origin; | ||
} | ||
|
||
public static implicit operator CustomTextClass(string origin) | ||
{ | ||
return new CustomTextClass(origin); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return Origin; | ||
} | ||
} | ||
|
||
public class TextHolder | ||
{ | ||
public TextHolder(string name, CustomTextClass note) | ||
{ | ||
Name = name; | ||
Note = note; | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public CustomTextClass Note { get; } | ||
|
||
public override string ToString() | ||
{ | ||
return Name + " (" + Note + ")"; | ||
} | ||
} | ||
|
||
public static class StaticHelper | ||
{ | ||
public static Guid? GetGuid(string name) | ||
{ | ||
return Guid.NewGuid(); | ||
} | ||
} | ||
|
||
private class TestCustomTypeProvider : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider | ||
{ | ||
private HashSet<Type> _customTypes; | ||
|
@@ -50,6 +101,7 @@ public virtual HashSet<Type> GetCustomTypes() | |
|
||
_customTypes = new HashSet<Type>(FindTypesMarkedWithDynamicLinqTypeAttribute(new[] { GetType().GetTypeInfo().Assembly })); | ||
_customTypes.Add(typeof(CustomClassWithStaticMethod)); | ||
_customTypes.Add(typeof(StaticHelper)); | ||
return _customTypes; | ||
} | ||
} | ||
|
@@ -452,5 +504,146 @@ public void ParseLambda_With_InnerStringLiteral() | |
object result = del.DynamicInvoke(String.Empty); | ||
Check.That(result).IsEqualTo(originalTrueValue); | ||
} | ||
|
||
[Fact] | ||
public void ParseLambda_With_Guid_Equals_Null() | ||
{ | ||
// Arrange | ||
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)"; | ||
|
||
// Act | ||
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 | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void ParseLambda_With_Null_Equals_Guid() | ||
{ | ||
// Arrange | ||
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)"; | ||
|
||
// Act | ||
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 | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void ParseLambda_With_Guid_Equals_String() | ||
{ | ||
// Arrange | ||
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}\"))"; | ||
|
||
// Act | ||
var lambda = DynamicExpressionParser.ParseLambda(typeof(User), null, expressionText, user); | ||
var guidLambda = lambda as Expression<Func<User, Guid>>; | ||
Assert.NotNull(guidLambda); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I've added it:
|
||
var del = lambda.Compile(); | ||
Guid result = (Guid) del.DynamicInvoke(user); | ||
|
||
// Assert | ||
Assert.Equal(guidEmpty, result); | ||
} | ||
|
||
[Fact] | ||
public void ParseLambda_With_Concat_String_CustomType() | ||
{ | ||
// Arrange | ||
string name = "name1"; | ||
string note = "note1"; | ||
var textHolder = new TextHolder(name, note); | ||
string expressionText = $"Name + \" (\" + Note + \")\""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to use $ here I think ? It's just a normal string. |
||
|
||
// Act 1 | ||
var lambda = DynamicExpressionParser.ParseLambda(typeof(TextHolder), null, expressionText, textHolder); | ||
var stringLambda = lambda as Expression<Func<TextHolder, string>>; | ||
|
||
// Assert 1 | ||
Assert.NotNull(stringLambda); | ||
|
||
// Act 2 | ||
var del = lambda.Compile(); | ||
string result = (string) del.DynamicInvoke(textHolder); | ||
|
||
// Assert 2 | ||
Assert.Equal("name1 (note1)", result); | ||
} | ||
|
||
[Fact] | ||
public void ParseLambda_With_Concat_CustomType_String() | ||
{ | ||
// Arrange | ||
string name = "name1"; | ||
string note = "note1"; | ||
var textHolder = new TextHolder(name, note); | ||
string expressionText = $"Note + \" (\" + Name + \")\""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to use $ here I think ? It's just a normal string. |
||
|
||
// Act 1 | ||
var lambda = DynamicExpressionParser.ParseLambda(typeof(TextHolder), null, expressionText, textHolder); | ||
var stringLambda = lambda as Expression<Func<TextHolder, string>>; | ||
|
||
// Assert 1 | ||
Assert.NotNull(stringLambda); | ||
|
||
// Act 2 | ||
var del = lambda.Compile(); | ||
string result = (string) del.DynamicInvoke(textHolder); | ||
|
||
// Assert 2 | ||
Assert.Equal("note1 (name1)", result); | ||
} | ||
|
||
[Fact] | ||
public void ParseLambda_With_Less_Greater() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to |
||
{ | ||
var config = new ParsingConfig | ||
{ | ||
CustomTypeProvider = new TestCustomTypeProvider() | ||
}; | ||
|
||
// Arrange | ||
Guid someId = Guid.NewGuid(); | ||
Guid anotherId = Guid.NewGuid(); | ||
var user = new User(); | ||
user.Id = someId; | ||
Guid guidEmpty = Guid.Empty; | ||
string expressionText = $"iif(@0.Id == StaticHelper.GetGuid(\"name\"), Guid.Parse(\"{guidEmpty}\"), Guid.Parse(\"{anotherId}\"))"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why introduce the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This expression with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that the <> was not working at all. But I understand from your comment that this was only not working in specific flows? In that case your test looks good. Maybe you could add some more comments to explain this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Therefore if
And before my changes the variable Moreover for the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, thanks for the detailed explanation. |
||
|
||
// Act | ||
var lambda = DynamicExpressionParser.ParseLambda(config, typeof(User), null, expressionText, user); | ||
var guidLambda = lambda as Expression<Func<User, Guid>>; | ||
Assert.NotNull(guidLambda); | ||
|
||
var del = lambda.Compile(); | ||
Guid result = (Guid) del.DynamicInvoke(user); | ||
|
||
// Assert | ||
Assert.Equal(anotherId, result); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change comment to: