Skip to content

Commit 530cf62

Browse files
authored
Update all remaining Asserts to Fluent Assertions (#88)
- Update all Asserts to Fluent Assertions - Update JsonWriter test to use DeserializeObject. This works better with Fluent Assertions in comparison to JToken.Parse.
1 parent 342f79e commit 530cf62

20 files changed

+163
-151
lines changed

test/Microsoft.OpenApi.Readers.Tests/BasicTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
// ------------------------------------------------------------
55

66
using System.IO;
7-
using System.Linq;
87
using System.Text;
8+
using FluentAssertions;
99
using SharpYaml.Serialization;
1010
using Xunit;
11-
using FluentAssertions;
1211

1312
namespace Microsoft.OpenApi.Readers.Tests
1413
{
1514
public class BasicTests
1615
{
1716
[Fact]
18-
public void CheckOpenAPIVersion()
17+
public void CheckOpenApiVersion()
1918
{
2019
var stream = GetType().Assembly.GetManifestResourceStream(typeof(BasicTests), "Samples.petstore30.yaml");
2120
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
@@ -27,8 +26,8 @@ public void CheckOpenAPIVersion()
2726
public void InlineExample()
2827
{
2928
var openApiDoc = new OpenApiStringReader().Read(
30-
31-
@" openapi: 3.0.0
29+
@"
30+
openapi: 3.0.0
3231
info:
3332
title: A simple inline example
3433
version: 1.0.0
@@ -53,7 +52,7 @@ public void ParseBrokenSimplest()
5352
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
5453

5554
context.Errors.Should().HaveCount(1);
56-
context.Errors.Select(s => s.ToString()).Should().Contain("title is a required property of #/info");
55+
context.Errors[0].ToString().Should().Be("title is a required property of #/info");
5756
}
5857

5958
[Fact]

test/Microsoft.OpenApi.Readers.Tests/CallbackTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// ------------------------------------------------------------
55

66
using System.Linq;
7+
using FluentAssertions;
78
using Microsoft.OpenApi.Models;
89
using Xunit;
910

@@ -25,7 +26,7 @@ public void LoadSimpleCallback()
2526
var pathItem = callback.PathItems.First().Value;
2627
var operation = pathItem.Operations[OperationType.Post];
2728

28-
Assert.NotNull(operation);
29+
operation.Should().NotBeNull();
2930
}
3031

3132
[Fact]
@@ -40,19 +41,18 @@ public void LoadSimpleCallbackWithRefs()
4041
var operation = path.Operations.First().Value;
4142

4243
var callbackPair = operation.Callbacks.First();
43-
Assert.Equal("simplehook", callbackPair.Key);
44+
callbackPair.Key.Should().Be("simplehook");
4445

4546
var callback = callbackPair.Value;
4647
var pathItemPair = callback.PathItems.First();
47-
Assert.Equal("$request.body#/url", pathItemPair.Key.Expression);
48+
pathItemPair.Key.Expression.Should().Be("$request.body#/url");
4849

4950
var pathItem = pathItemPair.Value;
5051

5152
var operationPair = pathItem.Operations.First();
52-
var cboperation = operationPair.Value;
53-
Assert.Equal(OperationType.Post, operationPair.Key);
53+
operationPair.Key.Should().Be(OperationType.Post);
5454

55-
Assert.NotNull(callback);
55+
callback.Should().NotBeNull();
5656
}
5757
}
5858
}

test/Microsoft.OpenApi.Readers.Tests/FixtureTests.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System.IO;
77
using System.Linq;
8+
using FluentAssertions;
89
using Microsoft.OpenApi.Readers.ParseNodes;
910
using Microsoft.OpenApi.Readers.V3;
1011
using SharpYaml.Serialization;
@@ -49,11 +50,11 @@ public void TestBasicInfoObject()
4950
var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);
5051
var info = OpenApiV3Deserializer.LoadInfo(node);
5152

52-
Assert.NotNull(info);
53-
Assert.Equal("Swagger Sample App", info.Title);
54-
Assert.Equal("1.0.1", info.Version.ToString());
55-
Assert.Equal("[email protected]", info.Contact.Email);
56-
Assert.Empty(diagnostic.Errors);
53+
info.Should().NotBeNull();
54+
info.Title.Should().Be("Swagger Sample App");
55+
info.Version.ToString().Should().Be("1.0.1");
56+
info.Contact.Email.Should().Be("[email protected]");
57+
diagnostic.Errors.Should().BeEmpty();
5758
}
5859

5960
[Fact]
@@ -67,10 +68,10 @@ public void TestMinimalInfoObject()
6768
var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);
6869
var info = OpenApiV3Deserializer.LoadInfo(node);
6970

70-
Assert.NotNull(info);
71-
Assert.Equal("Swagger Sample App", info.Title);
72-
Assert.Equal("1.0.1", info.Version.ToString());
73-
Assert.Empty(diagnostic.Errors);
71+
info.Should().NotBeNull();
72+
info.Title.Should().Be("Swagger Sample App");
73+
info.Version.ToString().Should().Be("1.0.1");
74+
diagnostic.Errors.Should().BeEmpty();
7475
}
7576

7677
[Fact]
@@ -84,8 +85,8 @@ public void TestNegativeInfoObject()
8485
var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);
8586
var info = OpenApiV3Deserializer.LoadInfo(node);
8687

87-
Assert.NotNull(info);
88-
Assert.Equal(2, diagnostic.Errors.Count);
88+
info.Should().NotBeNull();
89+
diagnostic.Errors.Count.Should().Be(2);
8990
}
9091
}
9192
}

test/Microsoft.OpenApi.Readers.Tests/InfoTests.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6-
using Microsoft.OpenApi.Any;
76
using System.Linq;
7+
using FluentAssertions;
8+
using Microsoft.OpenApi.Any;
89
using Xunit;
910

1011
namespace Microsoft.OpenApi.Readers.Tests
@@ -19,11 +20,11 @@ public void CheckPetStoreApiInfo()
1920
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
2021

2122
var info = openApiDoc.Info;
22-
Assert.Equal("Swagger Petstore (Simple)", openApiDoc.Info.Title);
23-
Assert.Equal(
24-
"A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
25-
info.Description);
26-
Assert.Equal("1.0.0", info.Version.ToString());
23+
openApiDoc.Info.Title.Should().Be("Swagger Petstore (Simple)");
24+
info.Description.Should()
25+
.Be(
26+
"A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification");
27+
info.Version.ToString().Should().Be("1.0.0");
2728
}
2829

2930
[Fact]
@@ -36,33 +37,36 @@ public void ParseCompleteHeaderOpenApi()
3637
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
3738

3839
// Assert
39-
Assert.Equal("1.0.0", openApiDoc.SpecVersion.ToString());
40+
openApiDoc.SpecVersion.ToString().Should().Be("1.0.0");
4041

41-
Assert.Empty(openApiDoc.Paths);
42+
openApiDoc.Paths.Should().BeEmpty();
4243

4344
// verify info
4445
var info = openApiDoc.Info;
45-
Assert.NotNull(info);
46-
Assert.Equal("The Api", info.Title);
47-
Assert.Equal("0.9.1", info.Version.ToString());
48-
Assert.Equal("This is an api", info.Description);
49-
Assert.Equal("http://example.org/Dowhatyouwant", info.TermsOfService.OriginalString);
50-
Assert.Equal("Darrel Miller", info.Contact.Name);
46+
info.Should().NotBeNull();
47+
info.Title.Should().Be("The Api");
48+
info.Version.ToString().Should().Be("0.9.1");
49+
info.Description.Should().Be("This is an api");
50+
info.TermsOfService.OriginalString.Should().Be("http://example.org/Dowhatyouwant");
51+
info.Contact.Name.Should().Be("Darrel Miller");
5152

5253
// verify info's extensions
53-
Assert.NotNull(info.Extensions);
54-
Assert.Equal(3, info.Extensions.Count);
54+
info.Extensions.Should().NotBeNull();
55+
info.Extensions.Count.Should().Be(3);
5556

56-
OpenApiString stringValue = Assert.IsType<OpenApiString>(info.Extensions["x-something"]);
57-
Assert.Equal("Why does it start with x-, sigh", stringValue.Value);
57+
info.Extensions["x-something"].Should().BeOfType<OpenApiString>();
58+
var stringValue = (OpenApiString)(info.Extensions["x-something"]);
59+
stringValue.Value.Should().Be("Why does it start with x-, sigh");
5860

59-
OpenApiObject objValue = Assert.IsType<OpenApiObject>(info.Extensions["x-contact"]);
60-
Assert.Equal(3, objValue.Count);
61-
Assert.Equal(new[] { "name", "url", "email" }, objValue.Keys);
61+
info.Extensions["x-contact"].Should().BeOfType<OpenApiObject>();
62+
var objValue = (OpenApiObject)(info.Extensions["x-contact"]);
63+
objValue.Count.Should().Be(3);
64+
objValue.Keys.Should().Equal(new[] {"name", "url", "email"});
6265

63-
OpenApiArray arrayValue = Assert.IsType<OpenApiArray>(info.Extensions["x-list"]);
64-
Assert.Equal(2, arrayValue.Count);
65-
Assert.Equal(new[] { "1", "2" }, arrayValue.Select(e => ((OpenApiString)e).Value));
66+
info.Extensions["x-list"].Should().BeOfType<OpenApiArray>();
67+
var arrayValue = (OpenApiArray)(info.Extensions["x-list"]);
68+
arrayValue.Count.Should().Be(2);
69+
arrayValue.Select(e => ((OpenApiString)e).Value).Should().Equal(new[] {"1", "2"});
6670
}
6771
}
6872
}

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
22-
<PackageReference Include="FluentAssertions" Version="4.17.0">
22+
<PackageReference Include="FluentAssertions" Version="4.19.4">
2323
</PackageReference>
2424
<PackageReference Include="Newtonsoft.Json" Version="10.0.3">
2525
</PackageReference>

test/Microsoft.OpenApi.Readers.Tests/ModelToV2Tests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System.Collections.Generic;
77
using System.IO;
8+
using FluentAssertions;
89
using Microsoft.OpenApi.Models;
910
using Newtonsoft.Json.Linq;
1011
using Xunit;
@@ -24,8 +25,8 @@ public void EmptyTest()
2425

2526
var jObject = ExportV2ToJObject(openApiDoc);
2627

27-
Assert.Equal("2.0", jObject["swagger"].ToString());
28-
Assert.NotNull(jObject["info"]);
28+
jObject["swagger"].ToString().Should().Be("2.0");
29+
jObject["info"].Should().NotBeNull();
2930
}
3031

3132
private static JObject ExportV2ToJObject(OpenApiDocument openApiDoc)
@@ -47,9 +48,9 @@ public void HostTest()
4748

4849
var jObject = ExportV2ToJObject(openApiDoc);
4950

50-
Assert.Equal("example.org", (string)jObject["host"]);
51-
Assert.Equal("/api", (string)jObject["basePath"]);
52-
Assert.Equal(new List<string> {"http", "https"}, jObject["schemes"].ToObject<List<string>>());
51+
((string)jObject["host"]).Should().Be("example.org");
52+
((string)jObject["basePath"]).Should().Be("/api");
53+
jObject["schemes"].ToObject<List<string>>().Should().Equal(new List<string> {"http", "https"});
5354
}
5455

5556
[Fact]
@@ -94,9 +95,8 @@ public void TestConsumes()
9495

9596
var jObject = ExportV2ToJObject(openApiDoc);
9697

97-
Assert.Equal(
98-
"application/vnd.collection+json",
99-
(string)jObject["paths"]["/resource"]["get"]["consumes"][0]);
98+
((string)jObject["paths"]["/resource"]["get"]["consumes"][0]).Should()
99+
.Be("application/vnd.collection+json");
100100
}
101101

102102
[Fact]
@@ -133,7 +133,7 @@ public void TestParameter()
133133

134134
var jObject = ExportV2ToJObject(openApiDoc);
135135

136-
Assert.Equal("string", (string)jObject["paths"]["/resource"]["get"]["parameters"][0]["type"]);
136+
((string)jObject["paths"]["/resource"]["get"]["parameters"][0]["type"]).Should().Be("string");
137137
}
138138

139139
[Fact]
@@ -165,10 +165,10 @@ public void TestProduces()
165165

166166
var jObject = ExportV2ToJObject(openApiDoc);
167167

168-
Assert.Equal(
169-
"application/vnd.collection+json",
170-
(string)jObject["paths"]["/resource"]["get"]["produces"][0]);
171-
Assert.Equal("text/plain", (string)jObject["paths"]["/resource"]["get"]["produces"][1]);
168+
((string)jObject["paths"]["/resource"]["get"]["produces"][0])
169+
.Should()
170+
.Be("application/vnd.collection+json");
171+
((string)jObject["paths"]["/resource"]["get"]["produces"][1]).Should().Be("text/plain");
172172
}
173173

174174
[Fact]
@@ -212,9 +212,9 @@ public void TestRequestBody()
212212

213213
var bodyparam = jObject["paths"]["/resource"]["post"]["parameters"][0];
214214

215-
Assert.Equal("body", (string)bodyparam["in"]);
216-
Assert.Equal("string", (string)bodyparam["schema"]["type"]);
217-
Assert.Equal("100", (string)bodyparam["schema"]["maxLength"]);
215+
((string)bodyparam["in"]).Should().Be("body");
216+
((string)bodyparam["schema"]["type"]).Should().Be("string");
217+
((string)bodyparam["schema"]["maxLength"]).Should().Be("100");
218218
}
219219
}
220220
}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,34 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6-
using SharpYaml.Serialization;
7-
using System;
8-
using System.Threading.Tasks;
6+
using FluentAssertions;
97
using Xunit;
108

119
namespace Microsoft.OpenApi.Readers.Tests
1210
{
1311
public class OpenApiExampleTests
1412
{
15-
1613
[Fact()]
1714
public void ApiWithExamples()
1815
{
19-
using (var stream = GetType().Assembly.GetManifestResourceStream(GetType(), "Samples.api-with-examples.yaml"))
16+
using (var stream =
17+
GetType().Assembly.GetManifestResourceStream(GetType(), "Samples.api-with-examples.yaml"))
2018
{
2119
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
2220

23-
Assert.Empty(context.Errors);
21+
context.Errors.Should().BeEmpty();
2422
}
25-
2623
}
2724

2825
[Fact]
2926
public void PetStoreExpandedExample()
3027
{
31-
using (var stream = GetType().Assembly.GetManifestResourceStream(GetType(), "Samples.petstore-expanded.yaml"))
28+
using (var stream =
29+
GetType().Assembly.GetManifestResourceStream(GetType(), "Samples.petstore-expanded.yaml"))
3230
{
3331
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
3432

35-
Assert.Empty(context.Errors);
33+
context.Errors.Should().BeEmpty();
3634
}
3735
}
3836

@@ -43,7 +41,7 @@ public void SimplePetStore()
4341
{
4442
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
4543

46-
Assert.Empty(context.Errors);
44+
context.Errors.Should().BeEmpty();
4745
}
4846
}
4947

@@ -54,7 +52,7 @@ public void UberExample()
5452
{
5553
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
5654

57-
Assert.Empty(context.Errors);
55+
context.Errors.Should().BeEmpty();
5856
}
5957
}
6058
}

0 commit comments

Comments
 (0)