Skip to content

Commit 86b4cee

Browse files
committed
Ensure OpenApiExampleTests cover terse output
1 parent d0cc1b6 commit 86b4cee

7 files changed

+81
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"value": {
3+
"versions": [
4+
{
5+
"status": "Status1",
6+
"id": "v1",
7+
"links": [
8+
{
9+
"href": "http://example.com/1",
10+
"rel": "sampleRel1",
11+
"bytes": "AQID",
12+
"binary": "Ñ😻😑♮Í☛oƞ♑😲☇éNjžŁ♻😟¥a´Ī♃ƠąøƩ"
13+
}
14+
]
15+
},
16+
{
17+
"status": "Status2",
18+
"id": "v2",
19+
"links": [
20+
{
21+
"href": "http://example.com/2",
22+
"rel": "sampleRel2"
23+
}
24+
]
25+
}
26+
]
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"value":{"versions":[{"status":"Status1","id":"v1","links":[{"href":"http://example.com/1","rel":"sampleRel1","bytes":"AQID","binary":"Ñ😻😑♮Í☛oƞ♑😲☇éNjžŁ♻😟¥a´Ī♃ƠąøƩ"}]},{"status":"Status2","id":"v2","links":[{"href":"http://example.com/2","rel":"sampleRel2"}]}]}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"value": {
3+
"versions": [
4+
{
5+
"status": "Status1",
6+
"id": "v1",
7+
"links": [
8+
{
9+
"href": "http://example.com/1",
10+
"rel": "sampleRel1"
11+
}
12+
]
13+
},
14+
{
15+
"status": "Status2",
16+
"id": "v2",
17+
"links": [
18+
{
19+
"href": "http://example.com/2",
20+
"rel": "sampleRel2"
21+
}
22+
]
23+
}
24+
]
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"value":{"versions":[{"status":"Status1","id":"v1","links":[{"href":"http://example.com/1","rel":"sampleRel1"}]},{"status":"Status2","id":"v2","links":[{"href":"http://example.com/2","rel":"sampleRel2"}]}]}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$ref": "#/components/examples/example1"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"$ref":"#/components/examples/example1"}

test/Microsoft.OpenApi.Tests/Models/OpenApiExampleTests.cs

Lines changed: 21 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
using System.Globalization;
55
using System.IO;
66
using System.Text;
7-
using FluentAssertions;
7+
using System.Threading.Tasks;
88
using Microsoft.OpenApi.Any;
99
using Microsoft.OpenApi.Models;
1010
using Microsoft.OpenApi.Writers;
11+
using VerifyXunit;
1112
using Xunit;
1213
using Xunit.Abstractions;
1314

1415
namespace Microsoft.OpenApi.Tests.Models
1516
{
1617
[Collection("DefaultSettings")]
18+
[UsesVerify]
1719
public class OpenApiExampleTests
1820
{
1921
public static OpenApiExample AdvancedExample = new OpenApiExample
@@ -104,118 +106,58 @@ public OpenApiExampleTests(ITestOutputHelper output)
104106
_output = output;
105107
}
106108

107-
[Fact]
108-
public void SerializeAdvancedExampleAsV3JsonWorks()
109+
[Theory]
110+
[InlineData(true)]
111+
[InlineData(false)]
112+
public async Task SerializeAdvancedExampleAsV3JsonWorks(bool produceTerseOutput)
109113
{
110114
// Arrange
111115
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
112-
var writer = new OpenApiJsonWriter(outputStringWriter);
113-
var expected =
114-
@"{
115-
""value"": {
116-
""versions"": [
117-
{
118-
""status"": ""Status1"",
119-
""id"": ""v1"",
120-
""links"": [
121-
{
122-
""href"": ""http://example.com/1"",
123-
""rel"": ""sampleRel1"",
124-
""bytes"": ""AQID"",
125-
""binary"": ""Ñ😻😑♮Í☛oƞ♑😲☇éNjžŁ♻😟¥a´Ī♃ƠąøƩ""
126-
}
127-
]
128-
},
129-
{
130-
""status"": ""Status2"",
131-
""id"": ""v2"",
132-
""links"": [
133-
{
134-
""href"": ""http://example.com/2"",
135-
""rel"": ""sampleRel2""
136-
}
137-
]
138-
}
139-
]
140-
}
141-
}";
116+
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
142117

143118
// Act
144119
AdvancedExample.SerializeAsV3(writer);
145120
writer.Flush();
146121
var actual = outputStringWriter.GetStringBuilder().ToString();
147122

148123
// Assert
149-
actual = actual.MakeLineBreaksEnvironmentNeutral();
150-
expected = expected.MakeLineBreaksEnvironmentNeutral();
151-
actual.Should().Be(expected);
124+
await Verifier.Verify(actual).UseParameters(produceTerseOutput);
152125
}
153126

154-
[Fact]
155-
public void SerializeReferencedExampleAsV3JsonWorks()
127+
[Theory]
128+
[InlineData(true)]
129+
[InlineData(false)]
130+
public async Task SerializeReferencedExampleAsV3JsonWorks(bool produceTerseOutput)
156131
{
157132
// Arrange
158133
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
159-
var writer = new OpenApiJsonWriter(outputStringWriter);
160-
var expected =
161-
@"{
162-
""$ref"": ""#/components/examples/example1""
163-
}";
134+
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
164135

165136
// Act
166137
ReferencedExample.SerializeAsV3(writer);
167138
writer.Flush();
168139
var actual = outputStringWriter.GetStringBuilder().ToString();
169140

170141
// Assert
171-
actual = actual.MakeLineBreaksEnvironmentNeutral();
172-
expected = expected.MakeLineBreaksEnvironmentNeutral();
173-
actual.Should().Be(expected);
142+
await Verifier.Verify(actual).UseParameters(produceTerseOutput);
174143
}
175144

176-
[Fact]
177-
public void SerializeReferencedExampleAsV3JsonWithoutReferenceWorks()
145+
[Theory]
146+
[InlineData(true)]
147+
[InlineData(false)]
148+
public async Task SerializeReferencedExampleAsV3JsonWithoutReferenceWorks(bool produceTerseOutput)
178149
{
179150
// Arrange
180151
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
181-
var writer = new OpenApiJsonWriter(outputStringWriter);
182-
var expected =
183-
@"{
184-
""value"": {
185-
""versions"": [
186-
{
187-
""status"": ""Status1"",
188-
""id"": ""v1"",
189-
""links"": [
190-
{
191-
""href"": ""http://example.com/1"",
192-
""rel"": ""sampleRel1""
193-
}
194-
]
195-
},
196-
{
197-
""status"": ""Status2"",
198-
""id"": ""v2"",
199-
""links"": [
200-
{
201-
""href"": ""http://example.com/2"",
202-
""rel"": ""sampleRel2""
203-
}
204-
]
205-
}
206-
]
207-
}
208-
}";
152+
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
209153

210154
// Act
211155
ReferencedExample.SerializeAsV3WithoutReference(writer);
212156
writer.Flush();
213157
var actual = outputStringWriter.GetStringBuilder().ToString();
214158

215159
// Assert
216-
actual = actual.MakeLineBreaksEnvironmentNeutral();
217-
expected = expected.MakeLineBreaksEnvironmentNeutral();
218-
actual.Should().Be(expected);
160+
await Verifier.Verify(actual).UseParameters(produceTerseOutput);
219161
}
220162
}
221163
}

0 commit comments

Comments
 (0)