Skip to content

Commit

Permalink
Support ExpressionType.Add in CSharpHelper
Browse files Browse the repository at this point in the history
For NodaTime code literal generation
  • Loading branch information
roji committed Mar 4, 2020
1 parent 8a2e2e4 commit b1cf561
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,23 @@ private bool HandleExpression(Expression expression, StringBuilder builder, bool
.Append('.')
.Append(memberExpression.Member.Name);

return true;
}
case ExpressionType.Add:
{
var binaryExpression = (BinaryExpression)expression;
if (!HandleExpression(binaryExpression.Left, builder))
{
return false;
}

builder.Append(" + ");

if (!HandleExpression(binaryExpression.Right, builder))
{
return false;
}

return true;
}
}
Expand Down
17 changes: 15 additions & 2 deletions test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,16 +572,29 @@ public void Literal_with_instance_property()
}

[ConditionalFact]
public void Literal_with_unsupported_node_throws()
public void Literal_with_add()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Add(
Expression.Constant(10),
Expression.Constant(10)));

Assert.Equal(
"10 + 10",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType()));
}

[ConditionalFact]
public void Literal_with_unsupported_node_throws()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Multiply(
Expression.Constant(10),
Expression.Constant(10)));

Assert.Equal(
DesignStrings.LiteralExpressionNotSupported(
"(10 + 10)",
"(10 * 10)",
nameof(SimpleTestType)),
Assert.Throws<NotSupportedException>(
() => new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType())).Message);
Expand Down

0 comments on commit b1cf561

Please sign in to comment.