2
2
// Licensed under the MIT license.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . Globalization ;
6
7
using System . IO ;
8
+ using System . Linq ;
9
+ using System . Threading . Tasks ;
7
10
using FluentAssertions ;
8
11
using Microsoft . OpenApi . Any ;
9
12
using Microsoft . OpenApi . Writers ;
13
+ using VerifyXunit ;
10
14
using Xunit ;
11
15
12
16
namespace Microsoft . OpenApi . Tests . Writers
13
17
{
14
18
[ Collection ( "DefaultSettings" ) ]
19
+ [ UsesVerify ]
15
20
public class OpenApiWriterAnyExtensionsTests
16
21
{
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 )
19
28
{
20
29
// Arrange
21
30
var nullValue = new OpenApiNull ( ) ;
22
31
23
- var json = WriteAsJson ( nullValue ) ;
32
+ var json = WriteAsJson ( nullValue , produceTerseOutput ) ;
24
33
25
34
// Assert
26
35
json . Should ( ) . Be ( "null" ) ;
27
36
}
28
37
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
+
29
53
[ 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 )
34
56
{
35
57
// Arrange
36
58
var intValue = new OpenApiInteger ( input ) ;
37
59
38
- var json = WriteAsJson ( intValue ) ;
60
+ var json = WriteAsJson ( intValue , produceTerseOutput ) ;
39
61
40
62
// Assert
41
63
json . Should ( ) . Be ( input . ToString ( ) ) ;
42
64
}
43
65
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
+
44
81
[ 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 )
49
84
{
50
85
// Arrange
51
86
var longValue = new OpenApiLong ( input ) ;
52
87
53
- var json = WriteAsJson ( longValue ) ;
88
+ var json = WriteAsJson ( longValue , produceTerseOutput ) ;
54
89
55
90
// Assert
56
91
json . Should ( ) . Be ( input . ToString ( ) ) ;
57
92
}
58
93
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
+
59
109
[ 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 )
64
112
{
65
113
// Arrange
66
114
var floatValue = new OpenApiFloat ( input ) ;
67
115
68
- var json = WriteAsJson ( floatValue ) ;
116
+ var json = WriteAsJson ( floatValue , produceTerseOutput ) ;
69
117
70
118
// Assert
71
119
json . Should ( ) . Be ( input . ToString ( ) ) ;
72
120
}
73
121
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
+
74
137
[ 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 )
79
140
{
80
141
// Arrange
81
142
var doubleValue = new OpenApiDouble ( input ) ;
82
143
83
- var json = WriteAsJson ( doubleValue ) ;
144
+ var json = WriteAsJson ( doubleValue , produceTerseOutput ) ;
84
145
85
146
// Assert
86
147
json . Should ( ) . Be ( input . ToString ( ) ) ;
87
148
}
88
149
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
+
89
166
[ 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 )
95
169
{
96
170
// Arrange
97
171
var input = DateTimeOffset . Parse ( inputString , CultureInfo . InvariantCulture ) ;
98
172
var dateTimeValue = new OpenApiDateTime ( input ) ;
99
173
100
- var json = WriteAsJson ( dateTimeValue ) ;
174
+ var json = WriteAsJson ( dateTimeValue , produceTerseOutput ) ;
101
175
var expectedJson = "\" " + input . ToString ( "o" ) + "\" " ;
102
176
103
177
// Assert
104
178
json . Should ( ) . Be ( expectedJson ) ;
105
179
}
106
180
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
+
107
192
[ 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 )
111
195
{
112
196
// Arrange
113
197
var boolValue = new OpenApiBoolean ( input ) ;
114
198
115
- var json = WriteAsJson ( boolValue ) ;
199
+ var json = WriteAsJson ( boolValue , produceTerseOutput ) ;
116
200
117
201
// Assert
118
202
json . Should ( ) . Be ( input . ToString ( ) . ToLower ( ) ) ;
119
203
}
120
204
121
- [ Fact ]
122
- public void WriteOpenApiObjectAsJsonWorks ( )
205
+ [ Theory ]
206
+ [ InlineData ( true ) ]
207
+ [ InlineData ( false ) ]
208
+ public async Task WriteOpenApiObjectAsJsonWorks ( bool produceTerseOutput )
123
209
{
124
210
// Arrange
125
211
var openApiObject = new OpenApiObject
@@ -135,24 +221,16 @@ public void WriteOpenApiObjectAsJsonWorks()
135
221
}
136
222
} ;
137
223
138
- var actualJson = WriteAsJson ( openApiObject ) ;
224
+ var actualJson = WriteAsJson ( openApiObject , produceTerseOutput ) ;
139
225
140
226
// 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 ) ;
152
228
}
153
229
154
- [ Fact ]
155
- public void WriteOpenApiArrayAsJsonWorks ( )
230
+ [ Theory ]
231
+ [ InlineData ( true ) ]
232
+ [ InlineData ( false ) ]
233
+ public async Task WriteOpenApiArrayAsJsonWorks ( bool produceTerseOutput )
156
234
{
157
235
// Arrange
158
236
var openApiObject = new OpenApiObject
@@ -175,32 +253,19 @@ public void WriteOpenApiArrayAsJsonWorks()
175
253
new OpenApiString ( "stringValue2" )
176
254
} ;
177
255
178
- var actualJson = WriteAsJson ( array ) ;
256
+ var actualJson = WriteAsJson ( array , produceTerseOutput ) ;
179
257
180
258
// 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 ) ;
197
260
}
198
261
199
- private static string WriteAsJson ( IOpenApiAny any )
262
+ private static string WriteAsJson ( IOpenApiAny any , bool produceTerseOutput = false )
200
263
{
201
264
// Arrange (continued)
202
265
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 } ) ;
204
269
205
270
writer . WriteAny ( any ) ;
206
271
writer . Flush ( ) ;
0 commit comments