Skip to content

Commit 6385133

Browse files
authored
Test null check in Timestamps.TryParse and document parameters (#285)
Signed-off-by: Safia Abdalla <[email protected]>
1 parent 3b78a26 commit 6385133

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/CloudNative.CloudEvents/Timestamps.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ internal static class Timestamps
4444
/// <summary>
4545
/// Attempts to parse a string as an RFC-3339-formatted date/time and UTC offset.
4646
/// </summary>
47-
/// <param name="input"></param>
48-
/// <param name="result"></param>
49-
/// <returns></returns>
50-
public static bool TryParse(string input, out DateTimeOffset result)
47+
/// <param name="input">A string to be parsed as a <see cref="DateTimeOffset"/>.</param>
48+
/// <param name="result">A <see cref="DateTimeOffset"/> parsed from the <paramref name="input"/>.</param>
49+
/// <returns><see langword="true"/> if <paramref name="input"/> was parsed successfully, <see langword="false"/> otherwise.</returns>
50+
internal static bool TryParse(string input, out DateTimeOffset result)
5151
{
52-
// TODO: Check this and add a test
5352
Validation.CheckNotNull(input, nameof(input));
5453

5554
if (input.Length < MinLength) // "yyyy-MM-ddTHH:mm:ssZ" is the shortest possible value.

test/CloudNative.CloudEvents.UnitTests/TimestampsTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Cloud Native Foundation.
1+
// Copyright 2021 Cloud Native Foundation.
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

@@ -123,6 +123,13 @@ public void Parse_Failure(string text)
123123
Assert.Throws<FormatException>(() => Timestamps.Parse(text));
124124
}
125125

126+
[Fact]
127+
public void Parse_Null()
128+
{
129+
Assert.Throws<ArgumentNullException>(() => Timestamps.TryParse(null!, out _));
130+
Assert.Throws<ArgumentNullException>(() => Timestamps.Parse(null!));
131+
}
132+
126133
/// <summary>
127134
/// As we're already testing parsing thoroughly, the simplest way of providing
128135
/// a value to format is to parse a string. Many examples will round-trip, in which

0 commit comments

Comments
 (0)