Skip to content

Commit d3ac37e

Browse files
author
Sébastien Geiser
committed
More tests about struct
1 parent d1cfea9 commit d3ac37e

File tree

7 files changed

+92
-7
lines changed

7 files changed

+92
-7
lines changed

CodingSeb.ExpressionEvaluator.Tests/CodingSeb.ExpressionEvaluator.Tests.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@
1313
<PackageReference Include="Shouldly" Version="3.0.2" />
1414
</ItemGroup>
1515
<ItemGroup>
16+
<Compile Update="OthersTests.cs">
17+
<SubType>Code</SubType>
18+
</Compile>
1619
<Compile Update="Resources.Designer.cs">
1720
<AutoGen>True</AutoGen>
1821
<DesignTime>True</DesignTime>
1922
<DependentUpon>Resources.resx</DependentUpon>
2023
</Compile>
24+
<Compile Update="TestsUtils\StructForTest3.cs">
25+
<SubType>Code</SubType>
26+
</Compile>
2127
<Compile Update="TestsUtils\StructForTest1.cs">
2228
<SubType>Code</SubType>
2329
</Compile>
30+
<Compile Update="TestsUtils\StructForTest4.cs">
31+
<SubType>Code</SubType>
32+
</Compile>
2433
<Compile Update="TestsUtils\StructForTest2.cs">
2534
<SubType>Code</SubType>
2635
</Compile>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using NUnit.Framework;
2+
using Shouldly;
3+
4+
namespace CodingSeb.ExpressionEvaluator.Tests
5+
{
6+
public class OthersTests
7+
{
8+
[Test]
9+
public void RealNestedStructAssignationToSeeHowItWorksScript0055()
10+
{
11+
// The real version of Script0055
12+
StructForTest2 otherStruct;
13+
14+
otherStruct.AnOtherIntValue = 5;
15+
otherStruct.nestedStruct = new StructForTest1()
16+
{
17+
myIntvalue = 8,
18+
myStringValue = "Hey"
19+
};
20+
21+
otherStruct.nestedStruct.myIntvalue = 9;
22+
23+
$"Result {otherStruct.nestedStruct.myStringValue} {otherStruct.nestedStruct.myIntvalue}, {otherStruct.AnOtherIntValue}".ShouldBe("Result Hey 9, 5");
24+
}
25+
26+
[Test]
27+
public void RealNestedStructByPropertyAssignationToSeeHowItWorks()
28+
{
29+
// Same sa Script0055 with properties
30+
StructForTest4 otherStruct = new StructForTest4();
31+
32+
otherStruct.AnOtherIntValue = 5;
33+
otherStruct.NestedStruct = new StructForTest3()
34+
{
35+
MyIntvalue = 8,
36+
MyStringValue = "Hey"
37+
};
38+
39+
// Do not compile
40+
//otherStruct.NestedStruct.MyIntvalue = 9;
41+
42+
$"Result {otherStruct.NestedStruct.MyStringValue} {otherStruct.NestedStruct.MyIntvalue}, {otherStruct.AnOtherIntValue}".ShouldBe("Result Hey 8, 5");
43+
}
44+
}
45+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* Script0055 */
22
otherStruct.AnOtherIntValue = 5;
3-
otherStruct.struct1 = new StructForTest1()
3+
4+
otherStruct.nestedStruct = new StructForTest1()
45
{
56
myIntvalue = 8,
67
myStringValue = "Hey"
78
};
89

9-
otherStruct.struct1.myIntvalue = 9;
10+
otherStruct.nestedStruct.myIntvalue = 9;
1011

11-
return $"Result {otherStruct.struct1.myStringValue} {otherStruct.struct1.myIntvalue}, {otherStruct.AnOtherIntValue}";
12+
return $"Result {otherStruct.nestedStruct.myStringValue} {otherStruct.nestedStruct.myIntvalue}, {otherStruct.AnOtherIntValue}";

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/StructForTest2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
public struct StructForTest2
44
{
55
public int AnOtherIntValue;
6-
public StructForTest1 struct1;
6+
public StructForTest1 nestedStruct;
77
}
88
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CodingSeb.ExpressionEvaluator.Tests
2+
{
3+
public struct StructForTest3
4+
{
5+
public int MyIntvalue { get; set; }
6+
public string MyStringValue { get; set; }
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CodingSeb.ExpressionEvaluator.Tests
2+
{
3+
public struct StructForTest4
4+
{
5+
public int AnOtherIntValue { get; set; }
6+
public StructForTest3 NestedStruct { get; set; }
7+
}
8+
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Globalization;
1616
using System.Linq;
1717
using System.Reflection;
18+
using System.Reflection.Emit;
1819
using System.Runtime.InteropServices;
1920
using System.Text;
2021
using System.Text.RegularExpressions;
@@ -1958,7 +1959,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
19581959
bool isDynamic = (flag & BindingFlags.Instance) != 0 && obj is IDynamicMetaObjectProvider && obj is IDictionary<string, object>;
19591960
IDictionary<string, object> dictionaryObject = obj as IDictionary<string, object>;
19601961

1961-
dynamic member = isDynamic ? null : objType?.GetProperty(varFuncName, flag);
1962+
MemberInfo member = isDynamic ? null : objType?.GetProperty(varFuncName, flag);
19621963
dynamic varValue = null;
19631964
bool assign = true;
19641965

@@ -1976,7 +1977,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
19761977
}
19771978
else
19781979
{
1979-
varValue = member.GetValue(obj);
1980+
varValue = ((dynamic)member).GetValue(obj);
19801981
}
19811982

19821983
if (pushVarValue)
@@ -2021,9 +2022,22 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
20212022
if (assign)
20222023
{
20232024
if (isDynamic)
2025+
{
20242026
dictionaryObject[varFuncName] = varValue;
2027+
}
20252028
else
2026-
member.SetValue(obj, varValue);
2029+
{
2030+
//if (objType.IsValueType && member is FieldInfo fieldInfo)
2031+
//{
2032+
// Action<object, object> setter = GetDelegateForStruct(objType, fieldInfo);
2033+
// ValueType valueType = obj as ValueType;
2034+
// setter(valueType, varValue);
2035+
//}
2036+
//else
2037+
//{
2038+
((dynamic)member).SetValue(obj, varValue);
2039+
//}
2040+
}
20272041
}
20282042
}
20292043
else if (varFuncMatch.Groups["assignationOperator"].Success)

0 commit comments

Comments
 (0)