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
110 changes: 43 additions & 67 deletions src/Microsoft.TestPlatform.Common/Filtering/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,84 +85,60 @@ internal Condition(string name, Operation operation, string value)
Value = value;
}

/// <summary>
/// Evaluate this condition for testObject.
/// </summary>
internal bool Evaluate(Func<string, object?> 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;
/// <summary>
/// Evaluate this condition for testObject.
/// </summary>
internal bool Evaluate(Func<string, object?> 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;
}

Expand Down
Loading