Skip to content

Commit 76844ba

Browse files
Add comprehensive tuple tests for reflection and improve test assertions
Co-authored-by: eiriktsarpalis <[email protected]>
1 parent b325fcc commit 76844ba

File tree

2 files changed

+117
-7
lines changed

2 files changed

+117
-7
lines changed

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,20 @@ public virtual void RoundTripValueTuple()
171171
[Fact]
172172
public virtual void LongValueTupleSerializes()
173173
{
174-
// Test that long tuples (> 7 elements) flatten the Rest field properly
175-
var longTuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
176-
string json = JsonSerializer.Serialize(longTuple);
177-
// Should serialize as Item1 through Item10, not nested Rest objects
178-
Assert.Contains("\"Item1\":1", json);
179-
Assert.Contains("\"Item10\":10", json);
180-
Assert.DoesNotContain("\"Rest\"", json);
174+
// Test 8-element tuple (boundary case)
175+
var tuple8 = (1, 2, 3, 4, 5, 6, 7, 8);
176+
string json8 = JsonSerializer.Serialize(tuple8);
177+
Assert.Equal("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Item8\":8}", json8);
178+
179+
// Test 10-element tuple
180+
var tuple10 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
181+
string json10 = JsonSerializer.Serialize(tuple10);
182+
Assert.Equal("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Item8\":8,\"Item9\":9,\"Item10\":10}", json10);
183+
184+
// Test 16-element tuple (nested Rest)
185+
var tuple16 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
186+
string json16 = JsonSerializer.Serialize(tuple16);
187+
Assert.Equal("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Item8\":8,\"Item9\":9,\"Item10\":10,\"Item11\":11,\"Item12\":12,\"Item13\":13,\"Item14\":14,\"Item15\":15,\"Item16\":16}", json16);
181188
}
182189

183190
[Fact]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace System.Text.Json.Serialization.Tests
7+
{
8+
public static class TupleTests
9+
{
10+
[Fact]
11+
public static void ValueTupleSerializesWithoutIncludeFields()
12+
{
13+
// Test that ValueTuple serializes without requiring IncludeFields = true
14+
var tuple = (Label1: "string", Label2: 42, true);
15+
string json = JsonSerializer.Serialize(tuple);
16+
Assert.Equal("{\"Item1\":\"string\",\"Item2\":42,\"Item3\":true}", json);
17+
}
18+
19+
[Fact]
20+
public static void ValueTupleDiscardsLabels()
21+
{
22+
// Test that C# labels are discarded and standard Item naming is used
23+
var tuple = (Name: "John", Age: 30, Active: true);
24+
string json = JsonSerializer.Serialize(tuple);
25+
Assert.Equal("{\"Item1\":\"John\",\"Item2\":30,\"Item3\":true}", json);
26+
Assert.DoesNotContain("Name", json);
27+
Assert.DoesNotContain("Age", json);
28+
Assert.DoesNotContain("Active", json);
29+
}
30+
31+
[Fact]
32+
public static void ValueTupleLongTupleFlattensRest()
33+
{
34+
// Test 8-element tuple (boundary case)
35+
var tuple8 = (1, 2, 3, 4, 5, 6, 7, 8);
36+
string json8 = JsonSerializer.Serialize(tuple8);
37+
Assert.Equal("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Item8\":8}", json8);
38+
39+
// Test 10-element tuple
40+
var tuple10 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
41+
string json10 = JsonSerializer.Serialize(tuple10);
42+
Assert.Equal("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Item8\":8,\"Item9\":9,\"Item10\":10}", json10);
43+
44+
// Test 16-element tuple (nested Rest)
45+
var tuple16 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
46+
string json16 = JsonSerializer.Serialize(tuple16);
47+
Assert.Equal("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Item8\":8,\"Item9\":9,\"Item10\":10,\"Item11\":11,\"Item12\":12,\"Item13\":13,\"Item14\":14,\"Item15\":15,\"Item16\":16}", json16);
48+
}
49+
50+
[Fact]
51+
public static void SystemTupleSerializesWithoutIncludeFields()
52+
{
53+
// Test that System.Tuple serializes (it already has properties, so this should work)
54+
var tuple = Tuple.Create("string", 42, true);
55+
string json = JsonSerializer.Serialize(tuple);
56+
Assert.Equal("{\"Item1\":\"string\",\"Item2\":42,\"Item3\":true}", json);
57+
}
58+
59+
[Fact]
60+
public static void SystemTupleLongTupleFlattensRest()
61+
{
62+
// Test 8-element System.Tuple (boundary case)
63+
var tuple8 = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
64+
string json8 = JsonSerializer.Serialize(tuple8);
65+
Assert.Equal("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Item8\":8}", json8);
66+
67+
// Test 10-element System.Tuple
68+
var tuple10 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(
69+
1, 2, 3, 4, 5, 6, 7, new Tuple<int, int, int>(8, 9, 10));
70+
string json10 = JsonSerializer.Serialize(tuple10);
71+
Assert.Equal("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Item8\":8,\"Item9\":9,\"Item10\":10}", json10);
72+
}
73+
74+
[Fact]
75+
public static void MixedTypeTuple()
76+
{
77+
// Test tuple with mixed types
78+
var tuple = ("text", 123, true, 45.67, 'c');
79+
string json = JsonSerializer.Serialize(tuple);
80+
Assert.Equal("{\"Item1\":\"text\",\"Item2\":123,\"Item3\":true,\"Item4\":45.67,\"Item5\":\"c\"}", json);
81+
}
82+
83+
[Fact]
84+
public static void NestedTuple()
85+
{
86+
// Test tuple containing another tuple
87+
var innerTuple = (1, 2);
88+
var outerTuple = ("outer", innerTuple);
89+
string json = JsonSerializer.Serialize(outerTuple);
90+
Assert.Equal("{\"Item1\":\"outer\",\"Item2\":{\"Item1\":1,\"Item2\":2}}", json);
91+
}
92+
93+
[Fact]
94+
public static void TupleWithObject()
95+
{
96+
// Test tuple containing a complex object
97+
var obj = new { Name = "Test", Value = 42 };
98+
var tuple = ("prefix", obj, "suffix");
99+
string json = JsonSerializer.Serialize(tuple);
100+
Assert.Equal("{\"Item1\":\"prefix\",\"Item2\":{\"Name\":\"Test\",\"Value\":42},\"Item3\":\"suffix\"}", json);
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)