Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "!~" operator to test filter #1803

Merged
merged 4 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Microsoft.TestPlatform.Common/Filtering/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal enum Operation
Equal,
NotEqual,
Contains,
NotContains
}

/// <summary>
Expand Down Expand Up @@ -148,6 +149,24 @@ internal bool Evaluate(Func<string, Object> propertyValueProvider)
}
}
break;

case Operation.NotContains:
// all values in multi-valued property should not contain 'this.Value' for NotContains to evaluate true.
result = true;

if (null != multiValue)
{
foreach (string propertyValue in multiValue)
{
Debug.Assert(null != propertyValue, "PropertyValue can not be null.");
result = result && propertyValue.IndexOf(Value, StringComparison.OrdinalIgnoreCase) == -1;
if (!result)
{
break;
}
}
}
break;
}
return result;
}
Expand Down Expand Up @@ -251,6 +270,9 @@ private static Operation GetOperator(string operationString)

case "~":
return Operation.Contains;

case "!~":
return Operation.NotContains;
}
throw new FormatException(string.Format(CultureInfo.CurrentCulture, CommonResources.TestCaseFilterFormatException, string.Format(CultureInfo.CurrentCulture, CommonResources.InvalidOperator, operationString)));
}
Expand Down Expand Up @@ -327,14 +349,20 @@ IEnumerable<string> TokenizeFilterConditionStringWorker(string s)
yield return tokenBuilder.ToString();
tokenBuilder.Clear();
}
// Determine if this is a "!=" or just a single "!".
// Determine if this is a "!=" or "!~" or just a single "!".
var next = i + 1;
if (next < s.Length && s[next] == '=')
{
i = next;
current = '=';
yield return "!=";
}
else if (next < s.Length && s[next] == '~')
{
i = next;
current = '~';
yield return "!~";
}
else
{
yield return "!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ public void ParseShouldHandleEscapedTilde()
Assert.AreEqual(@"TestClass1(""~"").TestMethod(1.5)", condition.Value);
}

[TestMethod]
public void ParseShouldHandleEscapedNotTilde()
{
var conditionString = @"FullyQualifiedName!~TestClass1\(""\!\~""\).TestMethod\(1.5\)";

Condition condition = Condition.Parse(conditionString);
Assert.AreEqual("FullyQualifiedName", condition.Name);
Assert.AreEqual(Operation.NotContains, condition.Operation);
Assert.AreEqual(@"TestClass1(""!~"").TestMethod(1.5)", condition.Value);
}

[TestMethod]
public void ParseStringWithSingleUnescapedBangThrowsFormatException1()
{
Expand Down Expand Up @@ -185,6 +196,19 @@ public void TokenizeConditionShouldHandleEscapedTilde()
Assert.AreEqual(@"TestMethod\(""\~""\)", tokens[2]);
}

[TestMethod]
public void TokenizeConditionShouldHandleEscapedNotTilde()
{
var conditionString = @"FullyQualifiedName!~TestMethod\(""\!\~""\)";

var tokens = Condition.TokenizeFilterConditionString(conditionString).ToArray();

Assert.AreEqual(3, tokens.Length);
Assert.AreEqual("FullyQualifiedName", tokens[0]);
Assert.AreEqual("!~", tokens[1]);
Assert.AreEqual(@"TestMethod\(""\!\~""\)", tokens[2]);
}

[TestMethod]
public void TokenizeConditionShouldHandleSingleUnescapedBang()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public void ContainsOperationShouldNotCreateFastFilter()
Assert.IsTrue(fastFilter == null);
}

[TestMethod]
public void NotContainsOperationShouldNotCreateFastFilter()
{
var filterExpressionWrapper = new FilterExpressionWrapper("Name!~TestClass1");
var fastFilter = filterExpressionWrapper.fastFilter;

Assert.IsTrue(fastFilter == null);
}

[TestMethod]
public void AndOperatorAndEqualsOperationShouldNotCreateFastFilter()
{
Expand Down