From 9283d695cec491adcecbce03f21038465c3b3ba0 Mon Sep 17 00:00:00 2001 From: Ruairidh MacLeod Date: Wed, 23 Aug 2023 11:56:23 +0100 Subject: [PATCH] support hyphenated words in WordBefore/WordAfter --- IsIdentifiable/Rules/PartPatternFilterRule.cs | 4 +- .../Rules/PartPatternFilterRuleTests.cs | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/IsIdentifiable/Rules/PartPatternFilterRule.cs b/IsIdentifiable/Rules/PartPatternFilterRule.cs index 171e5f60..c81cda1b 100644 --- a/IsIdentifiable/Rules/PartPatternFilterRule.cs +++ b/IsIdentifiable/Rules/PartPatternFilterRule.cs @@ -65,7 +65,7 @@ public bool Covers(FailurePart failurePart, string problemValue) if (!string.IsNullOrWhiteSpace(WordBefore)) { var problemValueUpToOffset = problemValue[..(failurePart.Offset + failurePart.Word.Length)]; - var wordBeforeRegex = new Regex($"\\b{WordBefore}\\s+{IfPartPattern.TrimStart('^')}", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase)); + var wordBeforeRegex = new Regex($"\\b{WordBefore}(\\s|-)+{IfPartPattern.TrimStart('^')}", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase)); matchesBefore = wordBeforeRegex.Matches(problemValueUpToOffset).Any(); } @@ -73,7 +73,7 @@ public bool Covers(FailurePart failurePart, string problemValue) if (!string.IsNullOrWhiteSpace(WordAfter)) { var problemValueFromOffset = problemValue[failurePart.Offset..]; - var wordAfterRegex = new Regex($"{IfPartPattern.TrimEnd('$')}\\s+{WordAfter}\\b", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase)); + var wordAfterRegex = new Regex($"{IfPartPattern.TrimEnd('$')}(\\s|-)+{WordAfter}\\b", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase)); matchesAfter = wordAfterRegex.Matches(problemValueFromOffset).Any(); } diff --git a/Tests/IsIdentifiableTests/Rules/PartPatternFilterRuleTests.cs b/Tests/IsIdentifiableTests/Rules/PartPatternFilterRuleTests.cs index c7f93048..3b5530a5 100644 --- a/Tests/IsIdentifiableTests/Rules/PartPatternFilterRuleTests.cs +++ b/Tests/IsIdentifiableTests/Rules/PartPatternFilterRuleTests.cs @@ -91,4 +91,49 @@ public void HodgkinLymphoma(string valuePart) Assert.False(coversValidFailurePart); Assert.True(coversFilteredFailurePart); } + + [TestCase] + public void Hyphen_WordBefore() + { + // Arrange + var rule = new PartPatternFilterRule() + { + IfPartPattern = "^Hodgkin$", + WordBefore = "Non", + IfColumn = "TextValue", + As = FailureClassification.Person, + Action = RuleAction.Ignore, + }; + var problemValue = $"Non-Hodgkin's lymphoma"; + var failurePart = new FailurePart("Hodgkin", FailureClassification.Person, 4); + + // Act + var ruleCoversFailurePart = rule.Covers(failurePart, problemValue); + + // Assert + Assert.True(ruleCoversFailurePart); + } + + [TestCase] + public void Hyphen_WordAfter() + { + // Arrange + var rule = new PartPatternFilterRule() + { + IfPartPattern = "^Gr(a|e)y$", + WordAfter = "white", + IfColumn = "TextValue", + As = FailureClassification.Person, + Action = RuleAction.Ignore, + }; + var problemValue = $"Gray-white foo"; + var failurePart = new FailurePart("Gray", FailureClassification.Person, 0); + + // Act + var ruleCoversFailurePart = rule.Covers(failurePart, problemValue); + + // Assert + Assert.True(ruleCoversFailurePart); + } + }