Skip to content

Commit e7ebdd7

Browse files
committed
Enable nullable reference types for unit test project
Signed-off-by: Jon Skeet <[email protected]>
1 parent bb8f4a8 commit e7ebdd7

30 files changed

+179
-162
lines changed

test/CloudNative.CloudEvents.UnitTests/Amqp/AmqpTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public void AmqpStructuredMessageTest()
4545
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull"), receivedCloudEvent.Source);
4646
Assert.Equal("123", receivedCloudEvent.Subject);
4747
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
48-
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
48+
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time!.Value);
4949
Assert.Equal(MediaTypeNames.Text.Xml, receivedCloudEvent.DataContentType);
5050
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);
5151

52-
Assert.Equal("value", (string)receivedCloudEvent["comexampleextension1"]);
52+
Assert.Equal("value", (string?)receivedCloudEvent["comexampleextension1"]);
5353
}
5454

5555
[Fact]
@@ -82,11 +82,11 @@ public void AmqpBinaryMessageTest()
8282
Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
8383
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
8484
Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
85-
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
85+
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time!.Value);
8686
Assert.Equal(MediaTypeNames.Text.Xml, receivedCloudEvent.DataContentType);
8787
Assert.Equal("<much wow=\"xml\"/>", receivedCloudEvent.Data);
8888

89-
Assert.Equal("value", (string)receivedCloudEvent["comexampleextension1"]);
89+
Assert.Equal("value", (string?)receivedCloudEvent["comexampleextension1"]);
9090
}
9191

9292
[Fact]
@@ -109,7 +109,7 @@ public void AmqpNormalizesTimestampsToUtc()
109109
var message1 = Message.Decode(encodedAmqpMessage);
110110
var receivedCloudEvent = message1.ToCloudEvent(new JsonEventFormatter());
111111

112-
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
112+
AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time!.Value);
113113
}
114114

115115
[Fact]

test/CloudNative.CloudEvents.UnitTests/AspNetCore/HttpRequestExtensionsTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace CloudNative.CloudEvents.AspNetCore.UnitTests
1818
{
1919
public class HttpRequestExtensionsTest
2020
{
21-
public static TheoryData<string, string, IDictionary<string, string>> SingleCloudEventMessages = new TheoryData<string, string, IDictionary<string, string>>
21+
public static TheoryData<string, string, IDictionary<string, string>?> SingleCloudEventMessages = new TheoryData<string, string, IDictionary<string, string>?>
2222
{
2323
{
2424
"Binary",
@@ -38,7 +38,7 @@ public class HttpRequestExtensionsTest
3838
}
3939
};
4040

41-
public static TheoryData<string, string, IDictionary<string, string>> BatchMessages = new TheoryData<string, string, IDictionary<string, string>>
41+
public static TheoryData<string, string, IDictionary<string, string>?> BatchMessages = new TheoryData<string, string, IDictionary<string, string>?>
4242
{
4343
{
4444
"Batch",
@@ -47,7 +47,7 @@ public class HttpRequestExtensionsTest
4747
}
4848
};
4949

50-
public static TheoryData<string, string, IDictionary<string, string>> NonCloudEventMessages = new TheoryData<string, string, IDictionary<string, string>>
50+
public static TheoryData<string, string, IDictionary<string, string>?> NonCloudEventMessages = new TheoryData<string, string, IDictionary<string, string>?>
5151
{
5252
{
5353
"Plain text",
@@ -58,7 +58,7 @@ public class HttpRequestExtensionsTest
5858

5959
[Theory]
6060
[MemberData(nameof(SingleCloudEventMessages))]
61-
public void IsCloudEvent_True(string description, string contentType, IDictionary<string, string> headers)
61+
public void IsCloudEvent_True(string description, string contentType, IDictionary<string, string>? headers)
6262
{
6363
// Really only present for display purposes.
6464
Assert.NotNull(description);
@@ -71,7 +71,7 @@ public void IsCloudEvent_True(string description, string contentType, IDictionar
7171
[Theory]
7272
[MemberData(nameof(BatchMessages))]
7373
[MemberData(nameof(NonCloudEventMessages))]
74-
public void IsCloudEvent_False(string description, string contentType, IDictionary<string, string> headers)
74+
public void IsCloudEvent_False(string description, string contentType, IDictionary<string, string>? headers)
7575
{
7676
// Really only present for display purposes.
7777
Assert.NotNull(description);
@@ -83,7 +83,7 @@ public void IsCloudEvent_False(string description, string contentType, IDictiona
8383

8484
[Theory]
8585
[MemberData(nameof(BatchMessages))]
86-
public void IsCloudEventBatch_True(string description, string contentType, IDictionary<string, string> headers)
86+
public void IsCloudEventBatch_True(string description, string contentType, IDictionary<string, string>? headers)
8787
{
8888
// Really only present for display purposes.
8989
Assert.NotNull(description);
@@ -96,7 +96,7 @@ public void IsCloudEventBatch_True(string description, string contentType, IDict
9696
[Theory]
9797
[MemberData(nameof(SingleCloudEventMessages))]
9898
[MemberData(nameof(NonCloudEventMessages))]
99-
public void IsCloudEventBatch_False(string description, string contentType, IDictionary<string, string> headers)
99+
public void IsCloudEventBatch_False(string description, string contentType, IDictionary<string, string>? headers)
100100
{
101101
// Really only present for display purposes.
102102
Assert.NotNull(description);
@@ -138,7 +138,7 @@ private static HttpRequest CreateRequest(ReadOnlyMemory<byte> content, ContentTy
138138
Body = BinaryDataUtilities.AsStream(content)
139139
};
140140

141-
private static void CopyHeaders(IDictionary<string, string> source, HttpRequest target)
141+
private static void CopyHeaders(IDictionary<string, string>? source, HttpRequest target)
142142
{
143143
if (source is null)
144144
{

test/CloudNative.CloudEvents.UnitTests/AspNetCore/HttpResponseExtensionsTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task CopyToHttpResponseAsync_BinaryMode()
3636
Assert.Equal("1.0", response.Headers["ce-specversion"]);
3737
Assert.Equal(cloudEvent.Type, response.Headers["ce-type"]);
3838
Assert.Equal(cloudEvent.Id, response.Headers["ce-id"]);
39-
Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source), response.Headers["ce-source"]);
39+
Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source!), response.Headers["ce-source"]);
4040
// There's no data content type header; the content type itself is used for that.
4141
Assert.False(response.Headers.ContainsKey("ce-datacontenttype"));
4242
}
@@ -84,7 +84,7 @@ public async Task CopyToHttpResponseAsync_StructuredMode()
8484
Assert.Equal("1.0", response.Headers["ce-specversion"]);
8585
Assert.Equal(cloudEvent.Type, response.Headers["ce-type"]);
8686
Assert.Equal(cloudEvent.Id, response.Headers["ce-id"]);
87-
Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source), response.Headers["ce-source"]);
87+
Assert.Equal(CloudEventAttributeType.UriReference.Format(cloudEvent.Source!), response.Headers["ce-source"]);
8888
// We don't populate the data content type header
8989
Assert.False(response.Headers.ContainsKey("ce-datacontenttype"));
9090
}

test/CloudNative.CloudEvents.UnitTests/Avro/AvroEventFormatterTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void ReserializeTest()
3838
Assert.Equal(cloudEvent2.Type, cloudEvent.Type);
3939
Assert.Equal(cloudEvent2.Source, cloudEvent.Source);
4040
Assert.Equal(cloudEvent2.Id, cloudEvent.Id);
41-
AssertTimestampsEqual(cloudEvent2.Time.Value, cloudEvent.Time.Value);
41+
AssertTimestampsEqual(cloudEvent2.Time!.Value, cloudEvent.Time!.Value);
4242
Assert.Equal(cloudEvent2.DataContentType, cloudEvent.DataContentType);
4343
Assert.Equal(cloudEvent2.Data, cloudEvent.Data);
4444
}
@@ -56,11 +56,11 @@ public void StructuredParseSuccess()
5656
Assert.Equal("com.github.pull.create", cloudEvent.Type);
5757
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
5858
Assert.Equal("A234-1234-1234", cloudEvent.Id);
59-
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
59+
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time!.Value);
6060
Assert.Equal(MediaTypeNames.Text.Xml, cloudEvent.DataContentType);
6161
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
6262

63-
Assert.Equal("value", (string)cloudEvent["comexampleextension1"]);
63+
Assert.Equal("value", (string?)cloudEvent["comexampleextension1"]);
6464
}
6565

6666
[Fact]
@@ -77,7 +77,7 @@ public void StructuredParseWithExtensionsSuccess()
7777
Assert.Equal("com.github.pull.create", cloudEvent.Type);
7878
Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
7979
Assert.Equal("A234-1234-1234", cloudEvent.Id);
80-
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
80+
AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time!.Value);
8181
Assert.Equal(MediaTypeNames.Text.Xml, cloudEvent.DataContentType);
8282
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
8383

test/CloudNative.CloudEvents.UnitTests/CloudEventAttributeTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public void Properties_OptionalAttribute()
6767

6868
[Fact]
6969
public void CreateExtension_NullName() =>
70-
Assert.Throws<ArgumentNullException>(() => CloudEventAttribute.CreateExtension(null, CloudEventAttributeType.String));
70+
Assert.Throws<ArgumentNullException>(() => CloudEventAttribute.CreateExtension(null!, CloudEventAttributeType.String));
7171

7272
[Fact]
7373
public void CreateExtension_NullType() =>
74-
Assert.Throws<ArgumentNullException>(() => CloudEventAttribute.CreateExtension("name", null));
74+
Assert.Throws<ArgumentNullException>(() => CloudEventAttribute.CreateExtension("name", null!));
7575

7676
[Fact]
7777
public void CreateExtension_SpecVersionName() =>

test/CloudNative.CloudEvents.UnitTests/CloudEventAttributeTypeTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public void OrdinalTypeNameMatchesPropertyName()
5151
[Theory]
5252
[MemberData(nameof(AllTypes))]
5353
public void ParseNull(CloudEventAttributeType type) =>
54-
Assert.Throws<ArgumentNullException>(() => type.Parse(null));
54+
Assert.Throws<ArgumentNullException>(() => type.Parse(null!));
5555

5656
[Theory]
5757
[MemberData(nameof(AllTypes))]
5858
public void FormatNull(CloudEventAttributeType type) =>
59-
Assert.Throws<ArgumentNullException>(() => type.Format(null));
59+
Assert.Throws<ArgumentNullException>(() => type.Format(null!));
6060

6161
// None of our types can be constructed with a StringBuilder.
6262
[Theory]

test/CloudNative.CloudEvents.UnitTests/CloudEventFormatterAttributeTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class NonInstantiableAttribute
3535
{
3636
}
3737

38-
[CloudEventFormatter(null)]
38+
[CloudEventFormatter(null!)]
3939
public class NullFormatterAttribute
4040
{
4141
}
@@ -56,13 +56,13 @@ public abstract class AbstractCloudEventFormatter : CloudEventFormatter
5656

5757
public class SampleCloudEventFormatter : CloudEventFormatter
5858
{
59-
public override IReadOnlyList<CloudEvent> DecodeBatchModeMessage(ReadOnlyMemory<byte> body, ContentType contentType, IEnumerable<CloudEventAttribute> extensionAttributes) =>
59+
public override IReadOnlyList<CloudEvent> DecodeBatchModeMessage(ReadOnlyMemory<byte> body, ContentType? contentType, IEnumerable<CloudEventAttribute>? extensionAttributes) =>
6060
throw new NotImplementedException();
6161

6262
public override void DecodeBinaryModeEventData(ReadOnlyMemory<byte> body, CloudEvent cloudEvent) =>
6363
throw new NotImplementedException();
6464

65-
public override CloudEvent DecodeStructuredModeMessage(ReadOnlyMemory<byte> body, ContentType contentType, IEnumerable<CloudEventAttribute> extensionAttributes) =>
65+
public override CloudEvent DecodeStructuredModeMessage(ReadOnlyMemory<byte> body, ContentType? contentType, IEnumerable<CloudEventAttribute>? extensionAttributes) =>
6666
throw new NotImplementedException();
6767

6868
public override ReadOnlyMemory<byte> EncodeBatchModeMessage(IEnumerable<CloudEvent> cloudEvents, out ContentType contentType) =>

test/CloudNative.CloudEvents.UnitTests/CloudEventTest.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void CreateSimpleEvent()
3737
Assert.Equal(MediaTypeNames.Text.Xml, cloudEvent.DataContentType);
3838
Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);
3939

40-
Assert.Equal("value", (string)cloudEvent["comexampleextension1"]);
40+
Assert.Equal("value", (string?)cloudEvent["comexampleextension1"]);
4141
}
4242

4343
[Fact]
@@ -143,7 +143,7 @@ public void Constructor_SpecVersion()
143143

144144
[Fact]
145145
public void Constructor_NullVersion() =>
146-
Assert.Throws<ArgumentNullException>(() => new CloudEvent(specVersion: null));
146+
Assert.Throws<ArgumentNullException>(() => new CloudEvent(specVersion: null!));
147147

148148
[Fact]
149149
public void Constructor_SpecVersionAndExtensionAttributes()
@@ -167,7 +167,7 @@ public void Constructor_ExtensionAttributes_Duplicate()
167167
[Fact]
168168
public void Constructor_ExtensionAttributes_NullValue()
169169
{
170-
var extensions = new CloudEventAttribute[] { null };
170+
var extensions = new CloudEventAttribute[] { null! };
171171
Assert.Throws<ArgumentException>(() => new CloudEvent(extensions));
172172
}
173173

@@ -236,8 +236,8 @@ public void SetAttributeFromStringValue_Valid()
236236
Assert.Equal("text", cloudEvent["string"]);
237237
Assert.Equal(10, cloudEvent["integer"]);
238238
Assert.Equal(new byte[] { 77 }, cloudEvent["binary"]);
239-
Assert.True((bool) cloudEvent["boolean"]);
240-
AssertTimestampsEqual("2021-02-09T11:58:12.242Z", (DateTimeOffset) cloudEvent["timestamp"]);
239+
Assert.True((bool) cloudEvent["boolean"]!);
240+
AssertTimestampsEqual("2021-02-09T11:58:12.242Z", (DateTimeOffset) cloudEvent["timestamp"]!);
241241
Assert.Equal(new Uri("https://cloudevents.io"), cloudEvent["uri"]);
242242
Assert.Equal(new Uri("//auth", UriKind.RelativeOrAbsolute), cloudEvent["urireference"]);
243243
}
@@ -256,17 +256,17 @@ public void SetAttributeFromStringValue_NewAttribute()
256256
var cloudEvent = new CloudEvent();
257257
cloudEvent.SetAttributeFromString("ext", "text");
258258
Assert.Equal("text", cloudEvent["ext"]);
259-
Assert.Equal(CloudEventAttributeType.String, cloudEvent.GetAttribute("ext").Type);
259+
Assert.Equal(CloudEventAttributeType.String, cloudEvent.GetAttribute("ext")!.Type);
260260
}
261261

262262
[Fact]
263263
public void Indexer_NullKey_Throws()
264264
{
265265
var cloudEvent = new CloudEvent();
266-
Assert.Throws<ArgumentNullException>(() => cloudEvent[(string)null]);
267-
Assert.Throws<ArgumentNullException>(() => cloudEvent[(CloudEventAttribute)null]);
268-
Assert.Throws<ArgumentNullException>(() => cloudEvent[(string)null] = "text");
269-
Assert.Throws<ArgumentNullException>(() => cloudEvent[(CloudEventAttribute)null] = "text");
266+
Assert.Throws<ArgumentNullException>(() => cloudEvent[(string)null!]);
267+
Assert.Throws<ArgumentNullException>(() => cloudEvent[(CloudEventAttribute)null!]);
268+
Assert.Throws<ArgumentNullException>(() => cloudEvent[(string)null!] = "text");
269+
Assert.Throws<ArgumentNullException>(() => cloudEvent[(CloudEventAttribute)null!] = "text");
270270
}
271271

272272
[Fact]
@@ -326,7 +326,7 @@ public void SetIntegerExtensionSetImplicitlyWithString_Updates()
326326
{
327327
var cloudEvent = new CloudEvent();
328328
cloudEvent["ext"] = "10";
329-
Assert.Equal(CloudEventAttributeType.String, cloudEvent.GetAttribute("ext").Type);
329+
Assert.Equal(CloudEventAttributeType.String, cloudEvent.GetAttribute("ext")?.Type);
330330

331331
var attr = CloudEventAttribute.CreateExtension("ext", CloudEventAttributeType.Integer);
332332
// Setting the event with the attribute updates the extension registry...

test/CloudNative.CloudEvents.UnitTests/CloudEventsSpecVersionTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void FromVersionId_Known(string versionId)
2424
{
2525
var version = CloudEventsSpecVersion.FromVersionId(versionId);
2626
Assert.NotNull(version);
27-
Assert.Equal(versionId, version.VersionId);
27+
Assert.Equal(versionId, version!.VersionId);
2828
}
2929
}
3030
}

test/CloudNative.CloudEvents.UnitTests/CloudNative.CloudEvents.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
55
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

0 commit comments

Comments
 (0)