Skip to content

Commit 8395a47

Browse files
committed
Ensure OpenApiWriterAnyExtensionsTests cover terse output
1 parent 83fc148 commit 8395a47

7 files changed

+165
-68
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
false,
3+
{
4+
"stringProp": "stringValue1",
5+
"objProp": { },
6+
"arrayProp": [
7+
false
8+
]
9+
},
10+
"stringValue2"
11+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[false,{"stringProp":"stringValue1","objProp":{},"arrayProp":[false]},"stringValue2"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
false,
3+
{
4+
"stringProp": "stringValue1",
5+
"objProp": { },
6+
"arrayProp": [
7+
false
8+
]
9+
},
10+
"stringValue2"
11+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[false,{"stringProp":"stringValue1","objProp":{},"arrayProp":[false]},"stringValue2"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"stringProp": "stringValue1",
3+
"objProp": { },
4+
"arrayProp": [
5+
false
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"stringProp":"stringValue1","objProp":{},"arrayProp":[false]}

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

Lines changed: 133 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,124 +2,210 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Globalization;
67
using System.IO;
8+
using System.Linq;
9+
using System.Threading.Tasks;
710
using FluentAssertions;
811
using Microsoft.OpenApi.Any;
912
using Microsoft.OpenApi.Writers;
13+
using VerifyXunit;
1014
using Xunit;
1115

1216
namespace Microsoft.OpenApi.Tests.Writers
1317
{
1418
[Collection("DefaultSettings")]
19+
[UsesVerify]
1520
public class OpenApiWriterAnyExtensionsTests
1621
{
17-
[Fact]
18-
public void WriteOpenApiNullAsJsonWorks()
22+
static bool[] shouldProduceTerseOutputValues = new[] { true, false };
23+
24+
[Theory]
25+
[InlineData(true)]
26+
[InlineData(false)]
27+
public void WriteOpenApiNullAsJsonWorks(bool produceTerseOutput)
1928
{
2029
// Arrange
2130
var nullValue = new OpenApiNull();
2231

23-
var json = WriteAsJson(nullValue);
32+
var json = WriteAsJson(nullValue, produceTerseOutput);
2433

2534
// Assert
2635
json.Should().Be("null");
2736
}
2837

38+
public static IEnumerable<object[]> IntInputs
39+
{
40+
get
41+
{
42+
return
43+
from input in new int[] {
44+
int.MinValue,
45+
42,
46+
int.MaxValue,
47+
}
48+
from shouldBeTerse in shouldProduceTerseOutputValues
49+
select new object[] { input, shouldBeTerse };
50+
}
51+
}
52+
2953
[Theory]
30-
[InlineData(int.MinValue)]
31-
[InlineData(42)]
32-
[InlineData(int.MaxValue)]
33-
public void WriteOpenApiIntegerAsJsonWorks(int input)
54+
[MemberData(nameof(IntInputs))]
55+
public void WriteOpenApiIntegerAsJsonWorks(int input, bool produceTerseOutput)
3456
{
3557
// Arrange
3658
var intValue = new OpenApiInteger(input);
3759

38-
var json = WriteAsJson(intValue);
60+
var json = WriteAsJson(intValue, produceTerseOutput);
3961

4062
// Assert
4163
json.Should().Be(input.ToString());
4264
}
4365

66+
public static IEnumerable<object[]> LongInputs
67+
{
68+
get
69+
{
70+
return
71+
from input in new long[] {
72+
long.MinValue,
73+
42,
74+
long.MaxValue,
75+
}
76+
from shouldBeTerse in shouldProduceTerseOutputValues
77+
select new object[] { input, shouldBeTerse };
78+
}
79+
}
80+
4481
[Theory]
45-
[InlineData(long.MinValue)]
46-
[InlineData(42)]
47-
[InlineData(long.MaxValue)]
48-
public void WriteOpenApiLongAsJsonWorks(long input)
82+
[MemberData(nameof(LongInputs))]
83+
public void WriteOpenApiLongAsJsonWorks(long input, bool produceTerseOutput)
4984
{
5085
// Arrange
5186
var longValue = new OpenApiLong(input);
5287

53-
var json = WriteAsJson(longValue);
88+
var json = WriteAsJson(longValue, produceTerseOutput);
5489

5590
// Assert
5691
json.Should().Be(input.ToString());
5792
}
5893

94+
public static IEnumerable<object[]> FloatInputs
95+
{
96+
get
97+
{
98+
return
99+
from input in new float[] {
100+
float.MinValue,
101+
42.42f,
102+
float.MaxValue,
103+
}
104+
from shouldBeTerse in shouldProduceTerseOutputValues
105+
select new object[] { input, shouldBeTerse };
106+
}
107+
}
108+
59109
[Theory]
60-
[InlineData(float.MinValue)]
61-
[InlineData(42.42)]
62-
[InlineData(float.MaxValue)]
63-
public void WriteOpenApiFloatAsJsonWorks(float input)
110+
[MemberData(nameof(FloatInputs))]
111+
public void WriteOpenApiFloatAsJsonWorks(float input, bool produceTerseOutput)
64112
{
65113
// Arrange
66114
var floatValue = new OpenApiFloat(input);
67115

68-
var json = WriteAsJson(floatValue);
116+
var json = WriteAsJson(floatValue, produceTerseOutput);
69117

70118
// Assert
71119
json.Should().Be(input.ToString());
72120
}
73121

122+
public static IEnumerable<object[]> DoubleInputs
123+
{
124+
get
125+
{
126+
return
127+
from input in new double[] {
128+
double.MinValue,
129+
42.42d,
130+
double.MaxValue,
131+
}
132+
from shouldBeTerse in shouldProduceTerseOutputValues
133+
select new object[] { input, shouldBeTerse };
134+
}
135+
}
136+
74137
[Theory]
75-
[InlineData(double.MinValue)]
76-
[InlineData(42.42)]
77-
[InlineData(double.MaxValue)]
78-
public void WriteOpenApiDoubleAsJsonWorks(double input)
138+
[MemberData(nameof(DoubleInputs))]
139+
public void WriteOpenApiDoubleAsJsonWorks(double input, bool produceTerseOutput)
79140
{
80141
// Arrange
81142
var doubleValue = new OpenApiDouble(input);
82143

83-
var json = WriteAsJson(doubleValue);
144+
var json = WriteAsJson(doubleValue, produceTerseOutput);
84145

85146
// Assert
86147
json.Should().Be(input.ToString());
87148
}
88149

150+
public static IEnumerable<object[]> StringifiedDateTimes
151+
{
152+
get
153+
{
154+
return
155+
from input in new [] {
156+
"2017-1-2",
157+
"1999-01-02T12:10:22",
158+
"1999-01-03",
159+
"10:30:12"
160+
}
161+
from shouldBeTerse in shouldProduceTerseOutputValues
162+
select new object[] { input, shouldBeTerse };
163+
}
164+
}
165+
89166
[Theory]
90-
[InlineData("2017-1-2")]
91-
[InlineData("1999-01-02T12:10:22")]
92-
[InlineData("1999-01-03")]
93-
[InlineData("10:30:12")]
94-
public void WriteOpenApiDateTimeAsJsonWorks(string inputString)
167+
[MemberData(nameof(StringifiedDateTimes))]
168+
public void WriteOpenApiDateTimeAsJsonWorks(string inputString, bool produceTerseOutput)
95169
{
96170
// Arrange
97171
var input = DateTimeOffset.Parse(inputString, CultureInfo.InvariantCulture);
98172
var dateTimeValue = new OpenApiDateTime(input);
99173

100-
var json = WriteAsJson(dateTimeValue);
174+
var json = WriteAsJson(dateTimeValue, produceTerseOutput);
101175
var expectedJson = "\"" + input.ToString("o") + "\"";
102176

103177
// Assert
104178
json.Should().Be(expectedJson);
105179
}
106180

181+
public static IEnumerable<object[]> BooleanInputs
182+
{
183+
get
184+
{
185+
return
186+
from input in new [] { true, false }
187+
from shouldBeTerse in shouldProduceTerseOutputValues
188+
select new object[] { input, shouldBeTerse };
189+
}
190+
}
191+
107192
[Theory]
108-
[InlineData(true)]
109-
[InlineData(false)]
110-
public void WriteOpenApiBooleanAsJsonWorks(bool input)
193+
[MemberData(nameof(BooleanInputs))]
194+
public void WriteOpenApiBooleanAsJsonWorks(bool input, bool produceTerseOutput)
111195
{
112196
// Arrange
113197
var boolValue = new OpenApiBoolean(input);
114198

115-
var json = WriteAsJson(boolValue);
199+
var json = WriteAsJson(boolValue, produceTerseOutput);
116200

117201
// Assert
118202
json.Should().Be(input.ToString().ToLower());
119203
}
120204

121-
[Fact]
122-
public void WriteOpenApiObjectAsJsonWorks()
205+
[Theory]
206+
[InlineData(true)]
207+
[InlineData(false)]
208+
public async Task WriteOpenApiObjectAsJsonWorks(bool produceTerseOutput)
123209
{
124210
// Arrange
125211
var openApiObject = new OpenApiObject
@@ -135,24 +221,16 @@ public void WriteOpenApiObjectAsJsonWorks()
135221
}
136222
};
137223

138-
var actualJson = WriteAsJson(openApiObject);
224+
var actualJson = WriteAsJson(openApiObject, produceTerseOutput);
139225

140226
// Assert
141-
142-
var expectedJson = @"{
143-
""stringProp"": ""stringValue1"",
144-
""objProp"": { },
145-
""arrayProp"": [
146-
false
147-
]
148-
}";
149-
expectedJson = expectedJson.MakeLineBreaksEnvironmentNeutral();
150-
151-
actualJson.Should().Be(expectedJson);
227+
await Verifier.Verify(actualJson).UseParameters(produceTerseOutput);
152228
}
153229

154-
[Fact]
155-
public void WriteOpenApiArrayAsJsonWorks()
230+
[Theory]
231+
[InlineData(true)]
232+
[InlineData(false)]
233+
public async Task WriteOpenApiArrayAsJsonWorks(bool produceTerseOutput)
156234
{
157235
// Arrange
158236
var openApiObject = new OpenApiObject
@@ -175,32 +253,19 @@ public void WriteOpenApiArrayAsJsonWorks()
175253
new OpenApiString("stringValue2")
176254
};
177255

178-
var actualJson = WriteAsJson(array);
256+
var actualJson = WriteAsJson(array, produceTerseOutput);
179257

180258
// Assert
181-
182-
var expectedJson = @"[
183-
false,
184-
{
185-
""stringProp"": ""stringValue1"",
186-
""objProp"": { },
187-
""arrayProp"": [
188-
false
189-
]
190-
},
191-
""stringValue2""
192-
]";
193-
194-
expectedJson = expectedJson.MakeLineBreaksEnvironmentNeutral();
195-
196-
actualJson.Should().Be(expectedJson);
259+
await Verifier.Verify(actualJson).UseParameters(produceTerseOutput);
197260
}
198261

199-
private static string WriteAsJson(IOpenApiAny any)
262+
private static string WriteAsJson(IOpenApiAny any, bool produceTerseOutput = false)
200263
{
201264
// Arrange (continued)
202265
var stream = new MemoryStream();
203-
IOpenApiWriter writer = new OpenApiJsonWriter(new StreamWriter(stream));
266+
IOpenApiWriter writer = new OpenApiJsonWriter(
267+
new StreamWriter(stream),
268+
new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
204269

205270
writer.WriteAny(any);
206271
writer.Flush();

0 commit comments

Comments
 (0)