Skip to content

Commit

Permalink
support hyphenated words in WordBefore/WordAfter
Browse files Browse the repository at this point in the history
  • Loading branch information
rkm committed Aug 23, 2023
1 parent ee62305 commit 9283d69
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions IsIdentifiable/Rules/PartPatternFilterRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ 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();
}

bool matchesAfter = false;
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();
}

Expand Down
45 changes: 45 additions & 0 deletions Tests/IsIdentifiableTests/Rules/PartPatternFilterRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 9283d69

Please sign in to comment.