Replaced [MaybeNullWhen(false)] with [NotNullWhen(true)] for UdiParser.TryParse method (#14880)

* Replaced [MaybeNullWhen(false)] with [NotNullWhen(true)] for UdiParser.TryParse method

* Removing some empty lines
This commit is contained in:
Anders Bjerner
2023-10-03 14:43:54 +02:00
committed by GitHub
parent 511ee96c9e
commit 4932119807
2 changed files with 26 additions and 4 deletions

View File

@@ -70,13 +70,13 @@ public sealed class UdiParser
/// <param name="s">The string to convert.</param>
/// <param name="udi">An Udi instance that contains the value that was parsed.</param>
/// <returns>A boolean value indicating whether the string could be parsed.</returns>
public static bool TryParse<T>(string? s, [MaybeNullWhen(false)] out T udi)
where T : Udi?
public static bool TryParse<T>(string? s, [NotNullWhen(true)] out T? udi)
where T : Udi
{
var result = ParseInternal(s, true, false, out Udi? parsed);
if (result && parsed is T)
if (result && parsed is T t)
{
udi = (T)parsed;
udi = t;
return true;
}

View File

@@ -205,6 +205,28 @@ public class UdiTests
Assert.Throws<ArgumentException>(() => new UdiRange(guidUdi, "x"));
}
[Test]
public void TryParseTest()
{
// try parse to "Udi"
var stringUdiString = "umb://document/b9a56165-6c4e-4e79-8277-620430174ad3";
Assert.IsTrue(UdiParser.TryParse(stringUdiString, out Udi udi1));
Assert.AreEqual("b9a56165-6c4e-4e79-8277-620430174ad3", udi1 is GuidUdi guidUdi1 ? guidUdi1.Guid.ToString() : string.Empty);
// try parse to "Udi"
Assert.IsFalse(UdiParser.TryParse("nope", out Udi udi2));
Assert.IsNull(udi2);
// try parse to "GuidUdi?"
Assert.IsTrue(UdiParser.TryParse(stringUdiString, out GuidUdi? guidUdi3));
Assert.AreEqual("b9a56165-6c4e-4e79-8277-620430174ad3", guidUdi3.Guid.ToString());
// try parse to "GuidUdi?"
Assert.IsFalse(UdiParser.TryParse("nope", out GuidUdi? guidUdi4));
Assert.IsNull(guidUdi4);
}
[Test]
public void SerializationTest()
{