diff --git a/src/Microsoft.TestPlatform.Common/Filtering/Condition.cs b/src/Microsoft.TestPlatform.Common/Filtering/Condition.cs index 6d112c3fd3..a1064e7443 100644 --- a/src/Microsoft.TestPlatform.Common/Filtering/Condition.cs +++ b/src/Microsoft.TestPlatform.Common/Filtering/Condition.cs @@ -85,84 +85,60 @@ internal Condition(string name, Operation operation, string value) Value = value; } - /// - /// Evaluate this condition for testObject. - /// - internal bool Evaluate(Func propertyValueProvider) + private bool EvaluateEqualOperation(string[]? multiValue) { - ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider)); - var result = false; - var multiValue = GetPropertyValue(propertyValueProvider); - switch (Operation) + // if any value in multi-valued property matches 'this.Value', for Equal to evaluate true. + if (multiValue != null) { - case Operation.Equal: - // if any value in multi-valued property matches 'this.Value', for Equal to evaluate true. - if (null != multiValue) + foreach (string propertyValue in multiValue) + { + if (string.Equals(propertyValue, Value, StringComparison.OrdinalIgnoreCase)) { - foreach (string propertyValue in multiValue) - { - result = result || string.Equals(propertyValue, Value, StringComparison.OrdinalIgnoreCase); - if (result) - { - break; - } - } + return true; } - break; - + } + } - case Operation.NotEqual: - // all values in multi-valued property should not match 'this.Value' for NotEqual to evaluate true. - result = true; + return false; + } - // if value is null. - if (null != multiValue) + private bool EvaluateContainsOperation(string[]? multiValue) + { + if (multiValue != null) + { + foreach (string propertyValue in multiValue) + { + TPDebug.Assert(null != propertyValue, "PropertyValue can not be null."); + if (propertyValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) != -1) { - foreach (string propertyValue in multiValue) - { - result = result && !string.Equals(propertyValue, Value, StringComparison.OrdinalIgnoreCase); - if (!result) - { - break; - } - } + return true; } - break; + } + } - case Operation.Contains: - // if any value in multi-valued property contains 'this.Value' for 'Contains' to be true. - if (null != multiValue) - { - foreach (string propertyValue in multiValue) - { - TPDebug.Assert(null != propertyValue, "PropertyValue can not be null."); - result = result || propertyValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) != -1; - if (result) - { - break; - } - } - } - break; + return false; + } - case Operation.NotContains: - // all values in multi-valued property should not contain 'this.Value' for NotContains to evaluate true. - result = true; + /// + /// Evaluate this condition for testObject. + /// + internal bool Evaluate(Func propertyValueProvider) + { + ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider)); + var multiValue = GetPropertyValue(propertyValueProvider); + var result = Operation switch + { + // if any value in multi-valued property matches 'this.Value', for Equal to evaluate true. + Operation.Equal => EvaluateEqualOperation(multiValue), + // all values in multi-valued property should not match 'this.Value' for NotEqual to evaluate true. + Operation.NotEqual => !EvaluateEqualOperation(multiValue), + // if any value in multi-valued property contains 'this.Value' for 'Contains' to be true. + Operation.Contains => EvaluateContainsOperation(multiValue), + // all values in multi-valued property should not contain 'this.Value' for NotContains to evaluate true. + Operation.NotContains => !EvaluateContainsOperation(multiValue), + _ => false, + }; - if (null != multiValue) - { - foreach (string propertyValue in multiValue) - { - TPDebug.Assert(null != propertyValue, "PropertyValue can not be null."); - result = result && propertyValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) == -1; - if (!result) - { - break; - } - } - } - break; - } return result; }