From 6385133391dcc34bef563088c56468fc18a9dcdf Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Fri, 8 Mar 2024 01:35:36 -0800 Subject: [PATCH] Test null check in Timestamps.TryParse and document parameters (#285) Signed-off-by: Safia Abdalla --- src/CloudNative.CloudEvents/Timestamps.cs | 9 ++++----- test/CloudNative.CloudEvents.UnitTests/TimestampsTest.cs | 9 ++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/CloudNative.CloudEvents/Timestamps.cs b/src/CloudNative.CloudEvents/Timestamps.cs index 2d6ed7c..463c150 100644 --- a/src/CloudNative.CloudEvents/Timestamps.cs +++ b/src/CloudNative.CloudEvents/Timestamps.cs @@ -44,12 +44,11 @@ namespace CloudNative.CloudEvents /// /// Attempts to parse a string as an RFC-3339-formatted date/time and UTC offset. /// - /// - /// - /// - public static bool TryParse(string input, out DateTimeOffset result) + /// A string to be parsed as a . + /// A parsed from the . + /// if was parsed successfully, otherwise. + 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. diff --git a/test/CloudNative.CloudEvents.UnitTests/TimestampsTest.cs b/test/CloudNative.CloudEvents.UnitTests/TimestampsTest.cs index fb7afde..b0c46ba 100644 --- a/test/CloudNative.CloudEvents.UnitTests/TimestampsTest.cs +++ b/test/CloudNative.CloudEvents.UnitTests/TimestampsTest.cs @@ -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. @@ -123,6 +123,13 @@ namespace CloudNative.CloudEvents.UnitTests Assert.Throws(() => Timestamps.Parse(text)); } + [Fact] + public void Parse_Null() + { + Assert.Throws(() => Timestamps.TryParse(null!, out _)); + Assert.Throws(() => Timestamps.Parse(null!)); + } + /// /// 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