Skip to content

Commit b800ae0

Browse files
committed
Ensure OpenApiJsonWriterTests cover terse output
1 parent c932f6f commit b800ae0

File tree

1 file changed

+117
-137
lines changed

1 file changed

+117
-137
lines changed

test/Microsoft.OpenApi.Tests/Writers/OpenApiJsonWriterTests.cs

Lines changed: 117 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Globalization;
88
using System.IO;
9+
using System.Linq;
910
using FluentAssertions;
1011
using Microsoft.OpenApi.Writers;
1112
using Newtonsoft.Json;
@@ -24,36 +25,36 @@ public OpenApiJsonWriterTests(ITestOutputHelper output)
2425
_output = output;
2526
}
2627

28+
static bool[] shouldProduceTerseOutputValues = new[] { true, false };
29+
2730
public static IEnumerable<object[]> WriteStringListAsJsonShouldMatchExpectedTestCases()
2831
{
29-
yield return new object[]
30-
{
31-
new[]
32-
{
33-
"string1",
34-
"string2",
35-
"string3",
36-
"string4",
37-
"string5",
38-
"string6",
39-
"string7",
40-
"string8"
32+
return
33+
from input in new string[][] {
34+
new[]
35+
{
36+
"string1",
37+
"string2",
38+
"string3",
39+
"string4",
40+
"string5",
41+
"string6",
42+
"string7",
43+
"string8"
44+
},
45+
new[] {"string1", "string1", "string1", "string1"}
4146
}
42-
};
43-
44-
yield return new object[]
45-
{
46-
new[] {"string1", "string1", "string1", "string1"}
47-
};
47+
from shouldBeTerse in shouldProduceTerseOutputValues
48+
select new object[] { input, shouldBeTerse };
4849
}
4950

5051
[Theory]
5152
[MemberData(nameof(WriteStringListAsJsonShouldMatchExpectedTestCases))]
52-
public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues)
53+
public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues, bool produceTerseOutput)
5354
{
5455
// Arrange
5556
var outputString = new StringWriter(CultureInfo.InvariantCulture);
56-
var writer = new OpenApiJsonWriter(outputString);
57+
var writer = new OpenApiJsonWriter(outputString, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
5758

5859
// Act
5960
writer.WriteStartArray();
@@ -75,123 +76,112 @@ public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues)
7576

7677
public static IEnumerable<object[]> WriteMapAsJsonShouldMatchExpectedTestCasesSimple()
7778
{
78-
// Simple map
79-
yield return new object[]
80-
{
81-
new Dictionary<string, object>
82-
{
83-
["property1"] = "value1",
84-
["property2"] = "value2",
85-
["property3"] = "value3",
86-
["property4"] = "value4"
87-
}
88-
};
79+
return
80+
from input in new IDictionary<string, object>[] {
81+
// Simple map
82+
new Dictionary<string, object>
83+
{
84+
["property1"] = "value1",
85+
["property2"] = "value2",
86+
["property3"] = "value3",
87+
["property4"] = "value4"
88+
},
8989

90-
// Simple map with duplicate values
91-
yield return new object[]
92-
{
93-
new Dictionary<string, object>
94-
{
95-
["property1"] = "value1",
96-
["property2"] = "value1",
97-
["property3"] = "value1",
98-
["property4"] = "value1"
90+
// Simple map with duplicate values
91+
new Dictionary<string, object>
92+
{
93+
["property1"] = "value1",
94+
["property2"] = "value1",
95+
["property3"] = "value1",
96+
["property4"] = "value1"
97+
},
9998
}
100-
};
99+
from shouldBeTerse in shouldProduceTerseOutputValues
100+
select new object[] { input, shouldBeTerse };
101101
}
102102

103103
public static IEnumerable<object[]> WriteMapAsJsonShouldMatchExpectedTestCasesComplex()
104104
{
105-
// Empty map and empty list
106-
yield return new object[]
107-
{
108-
new Dictionary<string, object>
109-
{
110-
["property1"] = new Dictionary<string, object>(),
111-
["property2"] = new List<string>(),
112-
["property3"] = new List<object>
105+
return
106+
from input in new IDictionary<string, object>[] {
107+
// Empty map and empty list
108+
new Dictionary<string, object>
113109
{
114-
new Dictionary<string, object>(),
110+
["property1"] = new Dictionary<string, object>(),
111+
["property2"] = new List<string>(),
112+
["property3"] = new List<object>
113+
{
114+
new Dictionary<string, object>(),
115+
},
116+
["property4"] = "value4"
115117
},
116-
["property4"] = "value4"
117-
}
118-
};
119118

120-
// Number, boolean, and null handling
121-
yield return new object[]
122-
{
123-
new Dictionary<string, object>
124-
{
125-
["property1"] = "10.0",
126-
["property2"] = "10",
127-
["property3"] = "-5",
128-
["property4"] = 10.0M,
129-
["property5"] = 10,
130-
["property6"] = -5,
131-
["property7"] = true,
132-
["property8"] = "true",
133-
["property9"] = null,
134-
["property10"] = "null",
135-
["property11"] = "",
136-
}
137-
};
138-
139-
// DateTime
140-
yield return new object[]
141-
{
142-
new Dictionary<string, object>
143-
{
144-
["property1"] = new DateTime(1970, 01, 01),
145-
["property2"] = new DateTimeOffset(new DateTime(1970, 01, 01)),
146-
["property3"] = new DateTime(2018, 04, 03),
147-
}
148-
};
119+
// Number, boolean, and null handling
120+
new Dictionary<string, object>
121+
{
122+
["property1"] = "10.0",
123+
["property2"] = "10",
124+
["property3"] = "-5",
125+
["property4"] = 10.0M,
126+
["property5"] = 10,
127+
["property6"] = -5,
128+
["property7"] = true,
129+
["property8"] = "true",
130+
["property9"] = null,
131+
["property10"] = "null",
132+
["property11"] = "",
133+
},
149134

150-
// Nested map
151-
yield return new object[]
152-
{
153-
new Dictionary<string, object>
154-
{
155-
["property1"] = new Dictionary<string, object>
135+
// DateTime
136+
new Dictionary<string, object>
156137
{
157-
["innerProperty1"] = "innerValue1"
138+
["property1"] = new DateTime(1970, 01, 01),
139+
["property2"] = new DateTimeOffset(new DateTime(1970, 01, 01)),
140+
["property3"] = new DateTime(2018, 04, 03),
158141
},
159-
["property2"] = "value2",
160-
["property3"] = new Dictionary<string, object>
142+
143+
// Nested map
144+
new Dictionary<string, object>
161145
{
162-
["innerProperty3"] = "innerValue3"
146+
["property1"] = new Dictionary<string, object>
147+
{
148+
["innerProperty1"] = "innerValue1"
149+
},
150+
["property2"] = "value2",
151+
["property3"] = new Dictionary<string, object>
152+
{
153+
["innerProperty3"] = "innerValue3"
154+
},
155+
["property4"] = "value4"
163156
},
164-
["property4"] = "value4"
165-
}
166-
};
167157

168-
// Nested map and list
169-
yield return new object[]
170-
{
171-
new Dictionary<string, object>
172-
{
173-
["property1"] = new Dictionary<string, object>(),
174-
["property2"] = new List<string>(),
175-
["property3"] = new List<object>
158+
// Nested map and list
159+
new Dictionary<string, object>
176160
{
177-
new Dictionary<string, object>(),
178-
"string1",
179-
new Dictionary<string, object>
161+
["property1"] = new Dictionary<string, object>(),
162+
["property2"] = new List<string>(),
163+
["property3"] = new List<object>
180164
{
181-
["innerProperty1"] = new List<object>(),
182-
["innerProperty2"] = "string2",
183-
["innerProperty3"] = new List<object>
165+
new Dictionary<string, object>(),
166+
"string1",
167+
new Dictionary<string, object>
184168
{
185-
new List<string>
169+
["innerProperty1"] = new List<object>(),
170+
["innerProperty2"] = "string2",
171+
["innerProperty3"] = new List<object>
186172
{
187-
"string3"
173+
new List<string>
174+
{
175+
"string3"
176+
}
188177
}
189178
}
190-
}
179+
},
180+
["property4"] = "value4"
191181
},
192-
["property4"] = "value4"
193182
}
194-
};
183+
from shouldBeTerse in shouldProduceTerseOutputValues
184+
select new object[] { input, shouldBeTerse };
195185
}
196186

197187
private void WriteValueRecursive(OpenApiJsonWriter writer, object value)
@@ -233,11 +223,11 @@ private void WriteValueRecursive(OpenApiJsonWriter writer, object value)
233223
[Theory]
234224
[MemberData(nameof(WriteMapAsJsonShouldMatchExpectedTestCasesSimple))]
235225
[MemberData(nameof(WriteMapAsJsonShouldMatchExpectedTestCasesComplex))]
236-
public void WriteMapAsJsonShouldMatchExpected(IDictionary<string, object> inputMap)
226+
public void WriteMapAsJsonShouldMatchExpected(IDictionary<string, object> inputMap, bool produceTerseOutput)
237227
{
238228
// Arrange
239229
var outputString = new StringWriter(CultureInfo.InvariantCulture);
240-
var writer = new OpenApiJsonWriter(outputString);
230+
var writer = new OpenApiJsonWriter(outputString, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
241231

242232
// Act
243233
WriteValueRecursive(writer, inputMap);
@@ -251,34 +241,24 @@ public void WriteMapAsJsonShouldMatchExpected(IDictionary<string, object> inputM
251241

252242
public static IEnumerable<object[]> WriteDateTimeAsJsonTestCases()
253243
{
254-
yield return new object[]
255-
{
256-
new DateTimeOffset(2018, 1, 1, 10, 20, 30, TimeSpan.Zero),
257-
};
258-
259-
yield return new object[]
260-
{
261-
new DateTimeOffset(2018, 1, 1, 10, 20, 30, 100, TimeSpan.FromHours(14)),
262-
};
263-
264-
yield return new object[]
265-
{
266-
DateTimeOffset.UtcNow + TimeSpan.FromDays(4)
267-
};
268-
269-
yield return new object[]
270-
{
271-
DateTime.UtcNow + TimeSpan.FromDays(4)
272-
};
244+
return
245+
from input in new DateTimeOffset[] {
246+
new DateTimeOffset(2018, 1, 1, 10, 20, 30, TimeSpan.Zero),
247+
new DateTimeOffset(2018, 1, 1, 10, 20, 30, 100, TimeSpan.FromHours(14)),
248+
DateTimeOffset.UtcNow + TimeSpan.FromDays(4),
249+
DateTime.UtcNow + TimeSpan.FromDays(4),
250+
}
251+
from shouldBeTerse in shouldProduceTerseOutputValues
252+
select new object[] { input, shouldBeTerse };
273253
}
274254

275255
[Theory]
276256
[MemberData(nameof(WriteDateTimeAsJsonTestCases))]
277-
public void WriteDateTimeAsJsonShouldMatchExpected(DateTimeOffset dateTimeOffset)
257+
public void WriteDateTimeAsJsonShouldMatchExpected(DateTimeOffset dateTimeOffset, bool produceTerseOutput)
278258
{
279259
// Arrange
280260
var outputString = new StringWriter(CultureInfo.InvariantCulture);
281-
var writer = new OpenApiJsonWriter(outputString);
261+
var writer = new OpenApiJsonWriter(outputString, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
282262

283263
// Act
284264
writer.WriteValue(dateTimeOffset);

0 commit comments

Comments
 (0)