-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathAssignmentExpressionTests.cs
More file actions
53 lines (44 loc) · 2.15 KB
/
AssignmentExpressionTests.cs
File metadata and controls
53 lines (44 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Linq.Expressions;
using NUnit.Framework;
namespace DelegateDecompiler.Tests
{
[TestFixture]
public class AssignmentExpressionTests : DecompilerTestsBase
{
class MyClass
{
public string MyProperty { get; set; } = "";
}
[Test]
public void ShouldDecompilePropertyAssignmentAsAssignmentExpression()
{
// Create a function that assigns to a property
string TestAssignment(MyClass v) => v.MyProperty = "test value";
Func<MyClass, string> compiled = TestAssignment;
// Decompile it
LambdaExpression decompiled = DecompileExtensions.Decompile(compiled);
// The body should be a single assignment expression, not a block
Assert.That(decompiled.Body.NodeType, Is.EqualTo(ExpressionType.Assign));
Assert.That(decompiled.Body.Type, Is.EqualTo(typeof(string)));
// Verify it's a proper assignment expression
var assignment = (BinaryExpression)decompiled.Body;
Assert.That(assignment.Left.NodeType, Is.EqualTo(ExpressionType.MemberAccess));
Assert.That(assignment.Right.NodeType, Is.EqualTo(ExpressionType.Constant));
var constant = (ConstantExpression)assignment.Right;
Assert.That(constant.Value, Is.EqualTo("test value"));
}
[Test]
public void ShouldDecompileMultiplePropertyAssignmentAsAssignmentExpression()
{
// Test with another property to make sure the fix is general
DateTime TestPropertyAssignment(TestClass v) => v.StartDate = new DateTime(2023, 1, 1);
Func<TestClass, DateTime> compiled = TestPropertyAssignment;
// Decompile it
LambdaExpression decompiled = DecompileExtensions.Decompile(compiled);
// The body should be a single assignment expression, not a block
Assert.That(decompiled.Body.NodeType, Is.EqualTo(ExpressionType.Assign));
Assert.That(decompiled.Body.Type, Is.EqualTo(typeof(DateTime)));
}
}
}