Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions src/Microsoft.TestPlatform.Filter.Source/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,32 @@ internal bool Evaluate(Func<string, object?> propertyValueProvider)
#if IS_VSTEST_REPO
ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider));
#endif
var multiValue = GetPropertyValue(propertyValueProvider);
var result = Operation switch
var propertyValue = propertyValueProvider(Name);

// Fast path: single string value (most common case for FullyQualifiedName, DisplayName, etc.)
// Avoids allocating a string[1] wrapper that the general multi-value path would create.
if (propertyValue is string singleValue)
{
return Operation switch
{
Operation.Equal => string.Equals(singleValue, Value, StringComparison.OrdinalIgnoreCase),
Operation.NotEqual => !string.Equals(singleValue, Value, StringComparison.OrdinalIgnoreCase),
Operation.Contains => singleValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) != -1,
Operation.NotContains => singleValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) == -1,
_ => false,
};
}

// Null, string[], or other types: use multi-value evaluation.
// Other types are coerced via ToString() for backward compatibility.
string[]? multiValue = propertyValue switch
{
null => null,
string[] arr => arr,
_ => new[] { propertyValue.ToString()! },
};

return Operation switch
{
// if any value in multi-valued property matches 'this.Value', for Equal to evaluate true.
Operation.Equal => EvaluateEqualOperation(multiValue),
Expand All @@ -170,8 +194,6 @@ internal bool Evaluate(Func<string, object?> propertyValueProvider)
Operation.NotContains => !EvaluateContainsOperation(multiValue),
_ => false,
};

return result;
}

/// <summary>
Expand Down Expand Up @@ -292,25 +314,6 @@ private static Operation GetOperator(string operationString)
};
}

/// <summary>
/// Returns property value for Property using propertValueProvider.
/// </summary>
private string[]? GetPropertyValue(Func<string, object?> propertyValueProvider)
{
var propertyValue = propertyValueProvider(Name);
if (null != propertyValue)
{
if (propertyValue is not string[] multiValue)
{
multiValue = new string[1];
multiValue[0] = propertyValue.ToString()!;
}
return multiValue;
}

return null;
}

internal static IEnumerable<string> TokenizeFilterConditionString(string str)
{
return str == null ? throw new ArgumentNullException(nameof(str)) : TokenizeFilterConditionStringWorker(str);
Expand Down
Loading