From 12ba476dc6f7219198ffb5f9b2b9e2a3d8ba9e99 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Sun, 2 Jan 2022 20:21:45 -0300 Subject: [PATCH] Allow passing a null string to TryParse Fixes #5 --- src/ISBN/ISBN.cs | 5 ++++- src/Tests/ISBNTests.cs | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ISBN/ISBN.cs b/src/ISBN/ISBN.cs index 69a836b..df6b5b8 100644 --- a/src/ISBN/ISBN.cs +++ b/src/ISBN/ISBN.cs @@ -63,9 +63,12 @@ static ISBN() /// The ISBN to parse. /// The parsed result, if valid. /// if the was not a valid ISBN. otherwise. - public static bool TryParse(string isbn, [NotNullWhen(true)] out ISBN? result) + public static bool TryParse(string? isbn, [NotNullWhen(true)] out ISBN? result) { result = default; + if (isbn == null) + return false; + isbn = new string(isbn.Where(c => c != ' ' && c != '-').ToArray()); if (isbn.Length == 10) isbn += "978"; diff --git a/src/Tests/ISBNTests.cs b/src/Tests/ISBNTests.cs index 56fd9df..f174826 100644 --- a/src/Tests/ISBNTests.cs +++ b/src/Tests/ISBNTests.cs @@ -26,4 +26,7 @@ public void ParseIsbn(string isbn) Assert.True(false, "Failed to parse " + isbn); } } + + [Fact] + public void ParseNullIsbnReturnsFalse() => Assert.False(ISBN.TryParse(null, out var _)); }