Skip to content

Commit c890b91

Browse files
authored
Refactor Condition evaluation (#15357)
1 parent a64cb01 commit c890b91

File tree

1 file changed

+43
-67
lines changed

1 file changed

+43
-67
lines changed

src/Microsoft.TestPlatform.Common/Filtering/Condition.cs

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -85,84 +85,60 @@ internal Condition(string name, Operation operation, string value)
8585
Value = value;
8686
}
8787

88-
/// <summary>
89-
/// Evaluate this condition for testObject.
90-
/// </summary>
91-
internal bool Evaluate(Func<string, object?> propertyValueProvider)
88+
private bool EvaluateEqualOperation(string[]? multiValue)
9289
{
93-
ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider));
94-
var result = false;
95-
var multiValue = GetPropertyValue(propertyValueProvider);
96-
switch (Operation)
90+
// if any value in multi-valued property matches 'this.Value', for Equal to evaluate true.
91+
if (multiValue != null)
9792
{
98-
case Operation.Equal:
99-
// if any value in multi-valued property matches 'this.Value', for Equal to evaluate true.
100-
if (null != multiValue)
93+
foreach (string propertyValue in multiValue)
94+
{
95+
if (string.Equals(propertyValue, Value, StringComparison.OrdinalIgnoreCase))
10196
{
102-
foreach (string propertyValue in multiValue)
103-
{
104-
result = result || string.Equals(propertyValue, Value, StringComparison.OrdinalIgnoreCase);
105-
if (result)
106-
{
107-
break;
108-
}
109-
}
97+
return true;
11098
}
111-
break;
112-
99+
}
100+
}
113101

114-
case Operation.NotEqual:
115-
// all values in multi-valued property should not match 'this.Value' for NotEqual to evaluate true.
116-
result = true;
102+
return false;
103+
}
117104

118-
// if value is null.
119-
if (null != multiValue)
105+
private bool EvaluateContainsOperation(string[]? multiValue)
106+
{
107+
if (multiValue != null)
108+
{
109+
foreach (string propertyValue in multiValue)
110+
{
111+
TPDebug.Assert(null != propertyValue, "PropertyValue can not be null.");
112+
if (propertyValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) != -1)
120113
{
121-
foreach (string propertyValue in multiValue)
122-
{
123-
result = result && !string.Equals(propertyValue, Value, StringComparison.OrdinalIgnoreCase);
124-
if (!result)
125-
{
126-
break;
127-
}
128-
}
114+
return true;
129115
}
130-
break;
116+
}
117+
}
131118

132-
case Operation.Contains:
133-
// if any value in multi-valued property contains 'this.Value' for 'Contains' to be true.
134-
if (null != multiValue)
135-
{
136-
foreach (string propertyValue in multiValue)
137-
{
138-
TPDebug.Assert(null != propertyValue, "PropertyValue can not be null.");
139-
result = result || propertyValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) != -1;
140-
if (result)
141-
{
142-
break;
143-
}
144-
}
145-
}
146-
break;
119+
return false;
120+
}
147121

148-
case Operation.NotContains:
149-
// all values in multi-valued property should not contain 'this.Value' for NotContains to evaluate true.
150-
result = true;
122+
/// <summary>
123+
/// Evaluate this condition for testObject.
124+
/// </summary>
125+
internal bool Evaluate(Func<string, object?> propertyValueProvider)
126+
{
127+
ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider));
128+
var multiValue = GetPropertyValue(propertyValueProvider);
129+
var result = Operation switch
130+
{
131+
// if any value in multi-valued property matches 'this.Value', for Equal to evaluate true.
132+
Operation.Equal => EvaluateEqualOperation(multiValue),
133+
// all values in multi-valued property should not match 'this.Value' for NotEqual to evaluate true.
134+
Operation.NotEqual => !EvaluateEqualOperation(multiValue),
135+
// if any value in multi-valued property contains 'this.Value' for 'Contains' to be true.
136+
Operation.Contains => EvaluateContainsOperation(multiValue),
137+
// all values in multi-valued property should not contain 'this.Value' for NotContains to evaluate true.
138+
Operation.NotContains => !EvaluateContainsOperation(multiValue),
139+
_ => false,
140+
};
151141

152-
if (null != multiValue)
153-
{
154-
foreach (string propertyValue in multiValue)
155-
{
156-
TPDebug.Assert(null != propertyValue, "PropertyValue can not be null.");
157-
result = result && propertyValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) == -1;
158-
if (!result)
159-
{
160-
break;
161-
}
162-
}
163-
}
164-
break;
165-
}
166142
return result;
167143
}
168144

0 commit comments

Comments
 (0)