Skip to content

Commit b39ad35

Browse files
committed
Updated string compares to include culture info
1 parent b5db14b commit b39ad35

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/UnreachableCaseInspection/ParseTreeExpressionEvaluator.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,17 +386,22 @@ private bool Like(string input, string pattern)
386386

387387
private bool AreEqual(string lhs, string rhs)
388388
{
389-
return string.Compare(lhs, rhs, !_isOptionCompareBinary, CultureInfo.InvariantCulture) == 0;
389+
var compareOptions = _isOptionCompareBinary ?
390+
StringComparison.CurrentCulture | StringComparison.Ordinal
391+
: StringComparison.CurrentCulture | StringComparison.OrdinalIgnoreCase;
392+
return String.Equals(lhs, rhs, compareOptions);
390393
}
391394

392395
private bool IsLessThan(string lhs, string rhs)
393396
{
394-
return string.Compare(lhs, rhs, !_isOptionCompareBinary, CultureInfo.InvariantCulture) < 0;
397+
var compareOptions = _isOptionCompareBinary ? CompareOptions.None : CompareOptions.IgnoreCase;
398+
return String.Compare(lhs, rhs, CultureInfo.CurrentCulture, compareOptions) < 0;
395399
}
396400

397401
private bool IsGreaterThan(string lhs, string rhs)
398402
{
399-
return string.Compare(lhs, rhs, !_isOptionCompareBinary, CultureInfo.InvariantCulture) > 0;
403+
var compareOptions = _isOptionCompareBinary ? CompareOptions.None : CompareOptions.IgnoreCase;
404+
return String.Compare(lhs, rhs, CultureInfo.CurrentCulture, compareOptions) > 0;
400405
}
401406

402407
public static string ConvertLikePatternToRegexPattern(string likePattern)

RubberduckTests/Inspections/UnreachableCase/ParseTreeExpressionEvaluatorUnitTests.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,28 @@ public void ParseTreeValueExpressionEvaluator_LogicBinaryConstants(string operan
547547
[TestCase("10_>_11", "False")]
548548
[TestCase("10_>=_10", "True")]
549549
[TestCase("10_=>_10", "True")]
550-
[TestCase("6.5_>_5.2", "True")]
550+
[TestCase("6.5_>_5.2", "True")] //exercise decimal compares
551+
[TestCase("6.5_>=_5.2", "True")]
552+
[TestCase("6.5_=>_5.2", "True")]
553+
[TestCase("6.5_<_5.2", "False")]
554+
[TestCase("6.5_<=_5.2", "False")]
555+
[TestCase("6.5_=<_5.2", "False")]
556+
[TestCase("5.2_=<_5.2", "True")]
557+
[TestCase("6.5_<>_5.2", "True")]
558+
[TestCase("6.5_=_5.2", "False")]
551559
[TestCase("True_<_3", "True")]
552560
[TestCase("False_<_-2", "False")]
553-
[Category("Inspections")]
554-
public void ParseTreeValueExpressionEvaluator_RelationalOpConstants(string operands, string expected)
561+
[TestCase("4.0E30_<_4.6E30", "True")] //exercise double compares
562+
[TestCase("4.0E30_<=_4.6E30", "True")]
563+
[TestCase("4.0E30_=<_4.6E30", "True")]
564+
[TestCase("4.0E30_<>_4.6E30", "True")]
565+
[TestCase("4.0E30_>=_4.6E30", "False")]
566+
[TestCase("4.0E30_=>_4.6E30", "False")]
567+
[TestCase("4.6E30_=>_4.6E30", "True")]
568+
[TestCase("4.0E30_=_4.6E30", "False")]
569+
[TestCase("4.0E30_=_4.0E30", "True")]
570+
[Category("Inspections")]
571+
public void ParseTreeValueExpressionEvaluator_RelationalOpConstantOperands(string operands, string expected)
555572
{
556573
GetBinaryOpValues(operands, out IParseTreeValue LHS, out IParseTreeValue RHS, out string opSymbol);
557574

RubberduckTests/Inspections/UnreachableCase/UnreachableCaseInspectionTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,34 @@ End Select
21122112
Assert.AreEqual(expectedMsg, actualMsg);
21132113
}
21142114

2115+
[TestCase(@"Option Compare Binary", 1)]
2116+
[TestCase(@"Option Compare Text", 1)]
2117+
[Category("Inspections")]
2118+
public void UnreachableCaseInspection_StringCompares(string optionCompare, int unreachable)
2119+
{
2120+
string valueWithIgnorableChars = "Ani\u00ADmal";
2121+
string inputCode =
2122+
$@"
2123+
{optionCompare}
2124+
2125+
Sub FirstSub(y As Boolean)
2126+
2127+
Const s1 As String = ""animal""
2128+
Const s2 As String = {valueWithIgnorableChars}
2129+
2130+
Select Case y
2131+
Case s1 = s2 'Always false since we do not ignore the hyphen (""\u00AD"")
2132+
'OK
2133+
Case 10 > 2
2134+
'OK because first case is always false
2135+
Case 7 < 10
2136+
'Unreachable
2137+
End Select
2138+
End Sub";
2139+
(string expectedMsg, string actualMsg) = CheckActualResultsEqualsExpected(inputCode, unreachable: unreachable);
2140+
Assert.AreEqual(expectedMsg, actualMsg);
2141+
}
2142+
21152143
[Test]
21162144
[Category("Inspections")]
21172145
public void UnreachableCaseInspection_Ampersand()

0 commit comments

Comments
 (0)