Skip to content

Commit d40f66e

Browse files
committed
Added a unit test for binding ValueTuples with more than 8 members
1 parent 900233b commit d40f66e

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

ModelBinderTests/ModelBinderTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,54 @@ public void TestTupleModelBinder()
6666
Assert.True(result.BooleanCheck);
6767
}
6868

69+
[Fact]
70+
public void TestNestedTupleModelBinder()
71+
{
72+
var type = typeof(NestedTupleMemberTestData);
73+
var prop = type.GetProperty("Value");
74+
var tupleType = prop.PropertyType;
75+
string body = @"
76+
{
77+
""User"" : {
78+
""String"":""Test"",
79+
""Integer"":444,
80+
""Double"": 1.44,
81+
""Decimal"": 1.44
82+
},
83+
""SomeData"" : ""Test String Root"",
84+
""NullCheck"": null,
85+
""BooleanCheck"": true,
86+
""ComplexNullCheck"":null,
87+
""IntParam6"": 6,
88+
""IntParam7"": 7,
89+
""IntParam8"": 8,
90+
""IntParam9"": 9,
91+
}";
92+
93+
int parameterIndex = 0;
94+
var jobj = JObject.Parse(body);
95+
var tupleElementNames = (TupleElementNamesAttribute)prop.GetCustomAttributes(typeof(TupleElementNamesAttribute), true)[0];
96+
97+
var result =
98+
((TestUserClass User, string SomeData, string NullCheck, bool BooleanCheck, TestUserClass ComplexNullCheck, int IntParam6, int IntParam7, int IntParam8, int IntParam9))
99+
TupleModelBinder.ParseTupleFromModelAttributes(jobj, tupleElementNames.TransformNames, tupleType, ref parameterIndex);
100+
101+
Assert.NotNull(result.User);
102+
Assert.Equal("Test", result.User.String);
103+
Assert.Equal(444, result.User.Integer);
104+
Assert.Equal(1.44d, result.User.Double);
105+
Assert.Equal(1.44m, result.User.Decimal);
106+
107+
Assert.Equal("Test String Root", result.SomeData);
108+
Assert.Null(result.NullCheck);
109+
Assert.Null(result.ComplexNullCheck);
110+
Assert.True(result.BooleanCheck);
111+
Assert.Equal(6, result.IntParam6);
112+
Assert.Equal(7, result.IntParam7);
113+
Assert.Equal(8, result.IntParam8);
114+
Assert.Equal(9, result.IntParam9);
115+
}
116+
69117
[Fact]
70118
public void TestNullHandling()
71119
{
@@ -146,6 +194,11 @@ public class TupleMemberTestData
146194
public (TestUserClass User, string SomeData, string NullCheck, bool BooleanCheck, TestUserClass ComplexNullCheck) Value { get; set; }
147195
}
148196

197+
public class NestedTupleMemberTestData
198+
{
199+
public (TestUserClass User, string SomeData, string NullCheck, bool BooleanCheck, TestUserClass ComplexNullCheck, int IntParam6, int IntParam7, int IntParam8, int IntParam9) Value { get; set; }
200+
}
201+
149202
public class NullMemberTestData
150203
{
151204
public (string SomeData, string SomeNullData, string NullCheck, bool BooleanNullCheck, TestUserClass ComplexNullCheck) Value { get; set; }

0 commit comments

Comments
 (0)