Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullable context in all test projects #329

Merged
merged 15 commits into from
Aug 15, 2024
2 changes: 1 addition & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFrameworks>net8.0;net462</TargetFrameworks>
<IsTestProject>true</IsTestProject>
<Nullable>disable</Nullable>
bm0niz marked this conversation as resolved.
Show resolved Hide resolved
<ImplicitUsings>true</ImplicitUsings>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public class ApiKeyAuthenticationProviderTests
[Fact]
public async Task DefensiveProgramming()
{
Assert.Throws<ArgumentNullException>(() => new ApiKeyAuthenticationProvider(null, "param", ApiKeyAuthenticationProvider.KeyLocation.Header));
Assert.Throws<ArgumentNullException>(() => new ApiKeyAuthenticationProvider("key", null, ApiKeyAuthenticationProvider.KeyLocation.Header));
Assert.Throws<ArgumentNullException>(() => new ApiKeyAuthenticationProvider(null!, "param", ApiKeyAuthenticationProvider.KeyLocation.Header));
Assert.Throws<ArgumentNullException>(() => new ApiKeyAuthenticationProvider("key", null!, ApiKeyAuthenticationProvider.KeyLocation.Header));

var value = new ApiKeyAuthenticationProvider("key", "param", ApiKeyAuthenticationProvider.KeyLocation.Header);
await Assert.ThrowsAsync<ArgumentNullException>(() => value.AuthenticateRequestAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>(() => value.AuthenticateRequestAsync(null!));
}

[Fact]
public async Task AddsInHeader()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/abstractions/IEnumerableExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class IEnumerableExtensionsTests
[Fact]
public void AsList_WithNullInput_ReturnsNull()
{
IEnumerable<int> nullEnumerable = null;
IEnumerable<int>? nullEnumerable = null;
var result = nullEnumerable.AsList();
Assert.Null(result);
}
Expand All @@ -33,7 +33,7 @@ public void AsList_WithEnumerableInput_ReturnsNewList()
[Fact]
public void AsArray_WithNullInput_ReturnsNull()
{
IEnumerable<int> nullEnumerable = null;
IEnumerable<int>? nullEnumerable = null;
var result = nullEnumerable.AsArray();
Assert.Null(result);
}
Expand Down
24 changes: 12 additions & 12 deletions tests/abstractions/Mocks/TestEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ public class TestEntity : IParsable, IAdditionalDataHolder, IBackedModel
/// <summary>Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.</summary>
public IDictionary<string, object> AdditionalData
{
get { return BackingStore?.Get<IDictionary<string, object>>("additionalData"); }
get { return BackingStore.Get<IDictionary<string, object>>("additionalData")!; }
set { BackingStore?.Set("additionalData", value); }
}
/// <summary>Stores model information.</summary>
public IBackingStore BackingStore { get; private set; }
/// <summary>The id property</summary>
public string Id
public string? Id
{
get { return BackingStore?.Get<string>("id"); }
get { return BackingStore.Get<string>("id"); }
set { BackingStore?.Set("id", value); }
}
/// <summary>The OdataType property</summary>
public string OdataType
public string? OdataType
{
get { return BackingStore?.Get<string>("@odata.type"); }
get { return BackingStore.Get<string>("@odata.type"); }
set { BackingStore?.Set("@odata.type", value); }
}
/// <summary>The telephone numbers for the user. NOTE: Although this is a string collection, only one number can be set for this property. Read-only for users synced from on-premises directory. Returned by default. Supports $filter (eq, not, ge, le, startsWith).</summary>
public List<string> BusinessPhones
public List<string>? BusinessPhones
{
get { return BackingStore?.Get<List<string>>("businessPhones"); }
get { return BackingStore.Get<List<string>>("businessPhones"); }
set { BackingStore?.Set("businessPhones", value); }
}
/// <summary>The user or contact that is this user&apos;s manager. Read-only. (HTTP Methods: GET, PUT, DELETE.). Supports $expand.</summary>
public TestEntity Manager
public TestEntity? Manager
{
get { return BackingStore?.Get<TestEntity>("manager"); }
get { return BackingStore.Get<TestEntity>("manager"); }
set { BackingStore?.Set("manager", value); }
}
/// <summary>The user or contact that is this user& works with.</summary>
public List<TestEntity> Colleagues
public List<TestEntity>? Colleagues
{
get { return BackingStore?.Get<List<TestEntity>>("colleagues"); }
get { return BackingStore.Get<List<TestEntity>>("colleagues"); }
set { BackingStore?.Set("colleagues", value); }
}
/// <summary>
Expand All @@ -61,7 +61,7 @@ public TestEntity()
public IDictionary<string, Action<IParseNode>> GetFieldDeserializers()
{
return new Dictionary<string, Action<IParseNode>> {
{"businessPhones", n => { BusinessPhones = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
{"businessPhones", n => { BusinessPhones = n.GetCollectionOfPrimitiveValues<string>().ToList(); } },
{"id", n => { Id = n.GetStringValue(); } },
{"manager", n => { Manager = n.GetObjectValue<TestEntity>(TestEntity.CreateFromDiscriminatorValue); } },
{"@odata.type", n => { OdataType = n.GetStringValue(); } },
Expand Down
16 changes: 8 additions & 8 deletions tests/abstractions/RequestHeadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public class RequestHeadersTests
public void Defensive()
{
var instance = new RequestHeaders();
Assert.Throws<ArgumentNullException>(() => instance.Add(null, "value"));
Assert.Throws<ArgumentNullException>(() => instance.Add("name", (string[])null));
Assert.Throws<ArgumentNullException>(() => instance.Add(null!, "value"));
Assert.Throws<ArgumentNullException>(() => instance.Add("name", (string[])null!));
instance.Add("name", Array.Empty<string>());
instance.Add("name", new List<string>());
instance.Add(new KeyValuePair<string, IEnumerable<string>>("name", Array.Empty<string>()));
Assert.Throws<ArgumentNullException>(() => instance[null]);
Assert.Throws<ArgumentNullException>(() => instance.Remove(null));
Assert.Throws<ArgumentNullException>(() => instance.Remove(null, "value"));
Assert.Throws<ArgumentNullException>(() => instance.Remove("name", null));
Assert.Throws<ArgumentNullException>(() => instance.AddAll(null));
instance.ContainsKey(null);
Assert.Throws<ArgumentNullException>(() => instance[null!]);
Assert.Throws<ArgumentNullException>(() => instance.Remove(null!));
Assert.Throws<ArgumentNullException>(() => instance.Remove(null!, "value"));
Assert.Throws<ArgumentNullException>(() => instance.Remove("name", null!));
Assert.Throws<ArgumentNullException>(() => instance.AddAll(null!));
instance.ContainsKey(null!);
}
[Fact]
public void AddsToNonExistent()
Expand Down
16 changes: 8 additions & 8 deletions tests/abstractions/RequestInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ internal class GetQueryParameters
{
/// <summary>Select properties to be returned</summary>\
[QueryParameter("%24select")]
public string[] Select { get; set; }
public string[]? Select { get; set; }
/// <summary>Unique id of the request</summary>
[QueryParameter("%24requestId")]
public Guid RequestId { get; set; }
Expand All @@ -812,25 +812,25 @@ internal class GetQueryParameters
public bool? Count { get; set; }
/// <summary>Expand related entities</summary>
[QueryParameter("%24filter")]
public string Filter { get; set; }
public string? Filter { get; set; }
/// <summary>Order items by property values</summary>
[QueryParameter("%24orderby")]
public string[] Orderby { get; set; }
public string[]? Orderby { get; set; }
/// <summary>Search items by search phrases</summary>
[QueryParameter("%24search")]
public string Search { get; set; }
public string? Search { get; set; }
/// <summary>Restrict to TenantId</summary>
public string TenantId { get; set; }
public string? TenantId { get; set; }
/// <summary>Which Dataset to use</summary>
[QueryParameter("dataset")]
public TestEnum DataSet { get; set; }
/// <summary>Which Dataset to use</summary>
[QueryParameter("datasets")]
public TestEnum[] DataSets { get; set; }
public TestEnum[]? DataSets { get; set; }

[QueryParameter("item")]
public object Item { get; set; }
public object? Item { get; set; }
[QueryParameter("items")]
public object[] Items { get; set; }
public object[]? Items { get; set; }
}
}
34 changes: 18 additions & 16 deletions tests/abstractions/Serialization/DeserializationHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ public class DeserializationHelpersTests
[Obsolete]
public void DefensiveObject()
{
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(null, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(null!, (Stream)null!, null!));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, (Stream)null!, null!));
using var stream = new MemoryStream();
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, stream, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, "", null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, stream, null!));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.Deserialize<TestEntity>(_jsonContentType, "", null!));
}
[Fact]
[Obsolete]
public void DefensiveObjectCollection()
{
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(null, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, (Stream)null, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(null!, (Stream)null!, null!));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, (Stream)null!, null!));
using var stream = new MemoryStream();
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, stream, null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, "", null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, stream, null!));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.DeserializeCollection<TestEntity>(_jsonContentType, "", null!));
}
[Fact]
[Obsolete]
Expand Down Expand Up @@ -97,21 +97,23 @@ public void DeserializesCollectionOfObject()
[Fact]
public async Task DefensiveObjectAsync()
{
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeAsync<TestEntity>(null, (Stream)null, null));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeAsync<TestEntity>(_jsonContentType, (Stream)null, null));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeAsync<TestEntity>(null!, (Stream)null!, null!));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeAsync<TestEntity>(_jsonContentType, (Stream)null!, null!));
using var stream = new MemoryStream();
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeAsync<TestEntity>(_jsonContentType, stream, null));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeAsync<TestEntity>(_jsonContentType, "", null));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeAsync<TestEntity>(_jsonContentType, stream, null!));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeAsync<TestEntity>(_jsonContentType, "", null!));
}
[Fact]
public async Task DefensiveObjectCollectionAsync()
{
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeCollectionAsync<TestEntity>(null, (Stream)null, null, default));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeCollectionAsync<TestEntity>(_jsonContentType, (Stream)null, null));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeCollectionAsync<TestEntity>(null!, (Stream)null!, null!, default));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeCollectionAsync<TestEntity>(_jsonContentType, (Stream)null!, null!));
using var stream = new MemoryStream();
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeCollectionAsync<TestEntity>(_jsonContentType, stream, null));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeCollectionAsync<TestEntity>(_jsonContentType, "", null));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeCollectionAsync<TestEntity>(_jsonContentType, stream, null!));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await KiotaSerializer.DeserializeCollectionAsync<TestEntity>(_jsonContentType, "", null!));
}


[Fact]
public async Task DeserializesObjectWithoutReflectionAsync()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/abstractions/Serialization/Mocks/TestEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class TestEntity : IParsable, IAdditionalDataHolder
/// <summary>Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.</summary>
public IDictionary<string, object> AdditionalData { get; set; }
/// <summary>Read-only.</summary>
public string Id { get; set; }
public string? Id { get; set; }
/// <summary>Read-only.</summary>
public TimeSpan? WorkDuration { get; set; }
/// <summary>Read-only.</summary>
Expand All @@ -20,7 +20,7 @@ public class TestEntity : IParsable, IAdditionalDataHolder
/// <summary>Read-only.</summary>
public DateTimeOffset? CreatedDateTime { get; set; }
/// <summary>Read-only.</summary>
public string OfficeLocation { get; set; }
public string? OfficeLocation { get; set; }
/// <summary>
/// Instantiates a new entity and sets the default values.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public void ThrowsInvalidOperationExceptionForUnregisteredContentType()
[InlineData(null)]
[InlineData("")]
[Obsolete]
public void ThrowsArgumentNullExceptionForNoContentType(string contentType)
public void ThrowsArgumentNullExceptionForNoContentType(string? contentType)
{
// Arrange
using var testStream = new MemoryStream(Encoding.UTF8.GetBytes("test input"));
// Act
var exception = Assert.Throws<ArgumentNullException>(() => _parseNodeFactoryRegistry.GetRootParseNode(contentType, testStream));
var exception = Assert.Throws<ArgumentNullException>(() => _parseNodeFactoryRegistry.GetRootParseNode(contentType!, testStream));
// Assert
Assert.NotNull(exception);
Assert.Equal("contentType", exception.ParamName);
Expand Down Expand Up @@ -139,12 +139,12 @@ public async Task ThrowsInvalidOperationExceptionForUnregisteredContentTypeAsync
[Theory]
[InlineData(null)]
[InlineData("")]
public async Task ThrowsArgumentNullExceptionForNoContentTypeAsync(string contentType)
public async Task ThrowsArgumentNullExceptionForNoContentTypeAsync(string? contentType)
{
// Arrange
using var testStream = new MemoryStream(Encoding.UTF8.GetBytes("test input"));
// Act
var exception = await Assert.ThrowsAsync<ArgumentNullException>(async () => await _parseNodeFactoryRegistry.GetRootParseNodeAsync(contentType, testStream));
var exception = await Assert.ThrowsAsync<ArgumentNullException>(async () => await _parseNodeFactoryRegistry.GetRootParseNodeAsync(contentType!, testStream));
// Assert
Assert.NotNull(exception);
Assert.Equal("contentType", exception.ParamName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class SerializationHelpersTests
[Fact]
public void DefensiveObject()
{
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(null, (TestEntity)null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (TestEntity)null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(null!, (TestEntity)null!));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (TestEntity)null!));
}
[Fact]
public void DefensiveObjectCollection()
{
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(null, (IEnumerable<TestEntity>)null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (IEnumerable<TestEntity>)null));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(null!, (IEnumerable<TestEntity>)null!));
Assert.Throws<ArgumentNullException>(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (IEnumerable<TestEntity>)null!));
}
[Fact]
public async Task SerializesObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public void ThrowsInvalidOperationExceptionForUnregisteredContentType()
[Theory]
[InlineData(null)]
[InlineData("")]
public void ThrowsArgumentNullExceptionForNoContentType(string contentType)
public void ThrowsArgumentNullExceptionForNoContentType(string? contentType)
{
// Act
var exception = Assert.Throws<ArgumentNullException>(() => _serializationWriterFactoryRegistry.GetSerializationWriter(contentType));
var exception = Assert.Throws<ArgumentNullException>(() => _serializationWriterFactoryRegistry.GetSerializationWriter(contentType!));
// Assert
Assert.NotNull(exception);
Assert.Equal("contentType", exception.ParamName);
Expand Down
Loading
Loading