Skip to content

Commit

Permalink
Test null check in Timestamps.TryParse and document parameters (#285)
Browse files Browse the repository at this point in the history
Signed-off-by: Safia Abdalla <[email protected]>
  • Loading branch information
captainsafia authored Mar 8, 2024
1 parent 3b78a26 commit 6385133
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/CloudNative.CloudEvents/Timestamps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ internal static class Timestamps
/// <summary>
/// Attempts to parse a string as an RFC-3339-formatted date/time and UTC offset.
/// </summary>
/// <param name="input"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryParse(string input, out DateTimeOffset result)
/// <param name="input">A string to be parsed as a <see cref="DateTimeOffset"/>.</param>
/// <param name="result">A <see cref="DateTimeOffset"/> parsed from the <paramref name="input"/>.</param>
/// <returns><see langword="true"/> if <paramref name="input"/> was parsed successfully, <see langword="false"/> otherwise.</returns>
internal static bool TryParse(string input, out DateTimeOffset result)
{
// TODO: Check this and add a test
Validation.CheckNotNull(input, nameof(input));

if (input.Length < MinLength) // "yyyy-MM-ddTHH:mm:ssZ" is the shortest possible value.
Expand Down
9 changes: 8 additions & 1 deletion test/CloudNative.CloudEvents.UnitTests/TimestampsTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Cloud Native Foundation.
// Copyright 2021 Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.

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

[Fact]
public void Parse_Null()
{
Assert.Throws<ArgumentNullException>(() => Timestamps.TryParse(null!, out _));
Assert.Throws<ArgumentNullException>(() => Timestamps.Parse(null!));
}

/// <summary>
/// As we're already testing parsing thoroughly, the simplest way of providing
/// a value to format is to parse a string. Many examples will round-trip, in which
Expand Down

0 comments on commit 6385133

Please sign in to comment.