Skip to content

Commit 589ad48

Browse files
committed
Review feedback: NullabilityTests
1 parent 945a803 commit 589ad48

File tree

3 files changed

+27
-43
lines changed

3 files changed

+27
-43
lines changed

test/OpenApiClientTests/PropertyInfoAssertionsExtension.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,14 @@ namespace OpenApiClientTests;
77
internal static class PropertyInfoAssertionsExtensions
88
{
99
[CustomAssertion]
10-
public static void BeNullable(this PropertyInfoAssertions source, string because = "", params object[] becauseArgs)
10+
public static void HaveNullabilityState(this PropertyInfoAssertions source, NullabilityState expected, string because = "", params object[] becauseArgs)
1111
{
1212
PropertyInfo propertyInfo = source.Subject;
1313

1414
NullabilityInfoContext nullabilityContext = new();
1515
NullabilityInfo nullabilityInfo = nullabilityContext.Create(propertyInfo);
1616

17-
nullabilityInfo.ReadState.Should().NotBe(NullabilityState.NotNull, because, becauseArgs);
18-
}
19-
20-
[CustomAssertion]
21-
public static void BeNonNullable(this PropertyInfoAssertions source, string because = "", params object[] becauseArgs)
22-
{
23-
PropertyInfo propertyInfo = source.Subject;
24-
25-
NullabilityInfoContext nullabilityContext = new();
26-
NullabilityInfo nullabilityInfo = nullabilityContext.Create(propertyInfo);
27-
28-
nullabilityInfo.ReadState.Should().Be(NullabilityState.NotNull, because, becauseArgs);
17+
nullabilityInfo.ReadState.Should().Be(expected, because, becauseArgs);
18+
nullabilityInfo.WriteState.Should().Be(expected, because, becauseArgs);
2919
}
3020
}

test/OpenApiClientTests/SchemaProperties/NullableReferenceTypesDisabled/NullabilityTests.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@ namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled;
77

88
public sealed class NullabilityTests
99
{
10-
[Fact]
11-
public void Nullability_of_generated_types_is_as_expected()
10+
[Theory]
11+
[InlineData(nameof(ChickenAttributesInResponse.Name), NullabilityState.Unknown)]
12+
[InlineData(nameof(ChickenAttributesInResponse.NameOfCurrentFarm), NullabilityState.Unknown)]
13+
[InlineData(nameof(ChickenAttributesInResponse.Age), NullabilityState.NotNull)]
14+
[InlineData(nameof(ChickenAttributesInResponse.Weight), NullabilityState.NotNull)]
15+
[InlineData(nameof(ChickenAttributesInResponse.TimeAtCurrentFarmInDays), NullabilityState.Nullable)]
16+
[InlineData(nameof(ChickenAttributesInResponse.HasProducedEggs), NullabilityState.NotNull)]
17+
public void Nullability_of_generated_property_is_as_expected(string propertyName, NullabilityState expectedState)
1218
{
13-
PropertyInfo[] propertyInfos = typeof(ChickenAttributesInResponse).GetProperties();
14-
15-
PropertyInfo? propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.Name));
16-
propertyInfo.Should().BeNullable();
17-
18-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.NameOfCurrentFarm));
19-
propertyInfo.Should().BeNullable();
20-
21-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.Age));
22-
propertyInfo.Should().BeNonNullable();
23-
24-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.TimeAtCurrentFarmInDays));
25-
propertyInfo.Should().BeNullable();
19+
PropertyInfo[] properties = typeof(ChickenAttributesInResponse).GetProperties();
20+
PropertyInfo property = properties.Single(property => property.Name == propertyName);
21+
property.Should().HaveNullabilityState(expectedState);
2622
}
2723
}

test/OpenApiClientTests/SchemaProperties/NullableReferenceTypesEnabled/NullabilityTests.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesEnabled;
77

88
public sealed class NullabilityTests
99
{
10-
[Fact]
11-
public void Nullability_of_generated_types_is_as_expected()
10+
[Theory]
11+
[InlineData(nameof(CowAttributesInResponse.Name), NullabilityState.NotNull)]
12+
[InlineData(nameof(CowAttributesInResponse.NameOfCurrentFarm), NullabilityState.NotNull)]
13+
[InlineData(nameof(CowAttributesInResponse.NameOfPreviousFarm), NullabilityState.Nullable)]
14+
[InlineData(nameof(CowAttributesInResponse.Nickname), NullabilityState.NotNull)]
15+
[InlineData(nameof(CowAttributesInResponse.Age), NullabilityState.NotNull)]
16+
[InlineData(nameof(CowAttributesInResponse.Weight), NullabilityState.NotNull)]
17+
[InlineData(nameof(CowAttributesInResponse.TimeAtCurrentFarmInDays), NullabilityState.Nullable)]
18+
[InlineData(nameof(CowAttributesInResponse.HasProducedMilk), NullabilityState.NotNull)]
19+
public void Nullability_of_generated_property_is_as_expected(string propertyName, NullabilityState expectedState)
1220
{
13-
PropertyInfo[] propertyInfos = typeof(CowAttributesInResponse).GetProperties();
14-
15-
PropertyInfo? propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(CowAttributesInResponse.Name));
16-
propertyInfo.Should().BeNonNullable();
17-
18-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(CowAttributesInResponse.NameOfPreviousFarm));
19-
propertyInfo.Should().BeNullable();
20-
21-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(CowAttributesInResponse.Age));
22-
propertyInfo.Should().BeNonNullable();
23-
24-
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(CowAttributesInResponse.TimeAtCurrentFarmInDays));
25-
propertyInfo.Should().BeNullable();
21+
PropertyInfo[] properties = typeof(CowAttributesInResponse).GetProperties();
22+
PropertyInfo property = properties.Single(property => property.Name == propertyName);
23+
property.Should().HaveNullabilityState(expectedState);
2624
}
2725
}

0 commit comments

Comments
 (0)