Skip to content

Commit

Permalink
Add "!~" operator to test filter (#1803)
Browse files Browse the repository at this point in the history
* Add NotContains condition

* Add tests for NotContains condition

* Update comment to follow code strucutre
  • Loading branch information
hgran authored and mayankbansal018 committed Oct 22, 2018
1 parent ce2c9e1 commit d88fdbc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
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

0 comments on commit d88fdbc

Please sign in to comment.