From df615eb5dd24220eef7545266901125f42b11448 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Fri, 23 Apr 2021 16:19:02 -0400 Subject: [PATCH] bugfix *StartsWith* matchers Bad handling of empty / null resulted in `""` not matching `""` --- src/XlsxCompare.Tests/MatchByTests.cs | 2 ++ src/XlsxCompare/MatchBy.cs | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/XlsxCompare.Tests/MatchByTests.cs b/src/XlsxCompare.Tests/MatchByTests.cs index 78fc252..4b700a6 100644 --- a/src/XlsxCompare.Tests/MatchByTests.cs +++ b/src/XlsxCompare.Tests/MatchByTests.cs @@ -17,9 +17,11 @@ public class MatchByTests [DataRow(MatchBy.Date, "2021-04-01", "04/01/2021")] [DataRow(MatchBy.Date, "2021-04-01 4:00AM", "04/01/2021")] [DataRow(MatchBy.StringLeftStartsWithRight, "asdf", "as")] + [DataRow(MatchBy.StringLeftStartsWithRight, "", "")] [DataRow(MatchBy.Decimal, "0.084400", "0.0844")] [DataRow(MatchBy.Decimal, "0.00", "0")] [DataRow(MatchBy.StringRightStartsWithLeft, "asdf", "asdf and then some")] + [DataRow(MatchBy.StringRightStartsWithLeft, "", "")] public void IsMatch_ThingsThatMatch_ReturnsTrue(MatchBy? match, string left, string right) { Assert.IsTrue(match.IsMatch(left, right)); diff --git a/src/XlsxCompare/MatchBy.cs b/src/XlsxCompare/MatchBy.cs index 38dc960..8728eb6 100644 --- a/src/XlsxCompare/MatchBy.cs +++ b/src/XlsxCompare/MatchBy.cs @@ -44,8 +44,12 @@ private static bool IsDateMatch(string left, string right) && leftDate.Date == rightDate.Date); private static bool IsLeftStartsWithRightMatch(string left, string right) - => right.Length > 0 - && left.StartsWith(right, StringComparison.OrdinalIgnoreCase); + => (left, right) switch + { + ("", "") => true, // both empty is a match + (_, "") => false, // left starting with empty doesn't count + (_, _) => left.StartsWith(right, StringComparison.OrdinalIgnoreCase), + }; private static bool IsDecimalMatch(string left, string right) => IsStringMatch(left, right)