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

Make default testcase filter property name FullyQualifiedName #555

Merged
merged 5 commits into from
Mar 2, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/Microsoft.TestPlatform.Common/Filtering/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ internal class Condition
/// </summary>
private static string propertyNameValueSeperatorString = @"(\!\=)|(\=)|(\~)|(\!)";

/// <summary>
/// Default property name which will be used when filter has only property value.
/// </summary>
public const string DefaultPropertyName = "FullyQualifiedName";

/// <summary>
/// Default operation which will be used when filter has only property value.
/// </summary>
public const Operation DefaultOperation = Operation.Contains;

/// <summary>
/// Name of the property used in condition.
Expand Down Expand Up @@ -156,17 +165,28 @@ internal bool Evaluate(Func<string, Object> propertyValueProvider)
/// </summary>
internal static Condition Parse(string conditionString)
{
if (string.IsNullOrWhiteSpace(conditionString))
{
ThrownFormatExceptionForInvalidCondition(conditionString);
}
string[] parts = Regex.Split(conditionString, propertyNameValueSeperatorString);
if (parts.Length == 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there an unit test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test is added in ConditionTests.cs.

{
// If only parameter values is passed, create condition with default property name,
// default operation and given condition string as parameter value.
return new Condition(Condition.DefaultPropertyName, Condition.DefaultOperation, conditionString.Trim());
}

if (parts.Length != 3)
{
throw new FormatException(string.Format(CultureInfo.CurrentCulture, CommonResources.TestCaseFilterFormatException, string.Format(CultureInfo.CurrentCulture, CommonResources.InvalidCondition, conditionString)));
ThrownFormatExceptionForInvalidCondition(conditionString);
}

for (int index = 0; index < 3; index++)
{
if (string.IsNullOrWhiteSpace(parts[index]))
{
throw new FormatException(string.Format(CultureInfo.CurrentCulture, CommonResources.TestCaseFilterFormatException, string.Format(CultureInfo.CurrentCulture, CommonResources.InvalidCondition, conditionString)));
ThrownFormatExceptionForInvalidCondition(conditionString);
}
parts[index] = parts[index].Trim();
}
Expand All @@ -176,7 +196,11 @@ internal static Condition Parse(string conditionString)
return condition;
}


private static void ThrownFormatExceptionForInvalidCondition(string conditionString)
{
throw new FormatException(string.Format(CultureInfo.CurrentCulture, CommonResources.TestCaseFilterFormatException,
string.Format(CultureInfo.CurrentCulture, CommonResources.InvalidCondition, conditionString)));
}


/// <summary>
Expand Down
21 changes: 21 additions & 0 deletions test/Microsoft.TestPlatform.AcceptanceTests/TestCaseFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,26 @@ public void RunSelectedTestsWithPriorityTrait(string runnerFramework, string tar
this.InvokeVsTest(arguments);
this.ValidateSummaryStatus(1, 0, 0);
}

/// <summary>
/// In case TestCaseFilter is provide without any property like Name or ClassName. ex. /TestCaseFilter:"UnitTest1"
/// this command should provide same results as /TestCaseFilter:"FullyQualifiedName~UnitTest1".
/// </summary>
[CustomDataTestMethod]
[NET46TargetFramework]
[NETCORETargetFramework]
public void TestCaseFilterShouldWorkIfOnlyPropertyValueGivenInExpression(string runnerFramework, string targetFramework, string targetRuntime)
{
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerFramework, targetFramework, targetRuntime);

var arguments = PrepareArguments(
this.GetSampleTestAssembly(),
this.GetTestAdapterPath(),
string.Empty,
this.FrameworkArgValue);
arguments = string.Concat(arguments, " /TestCaseFilter:UnitTest1");
this.InvokeVsTest(arguments);
this.ValidateSummaryStatus(1, 1, 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.Common.UnitTests.Filtering
{
using System;
using Microsoft.VisualStudio.TestPlatform.Common.Filtering;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class ConditionTests
{
[TestMethod]
public void ParseShouldThrownFormatExceptionOnNullConditionString()
{
string conditionString = null;
Assert.ThrowsException<FormatException>(() => Condition.Parse(conditionString));
}

[TestMethod]
public void ParseShouldThrownFormatExceptionOnEmptyConditionString()
{
var conditionString = "";
Assert.ThrowsException<FormatException>(() => Condition.Parse(conditionString));
}

[TestMethod]
public void ParseShouldThrownFormatExceptionOnIncompleteConditionString()
{
var conditionString = "PropertyName=";
Assert.ThrowsException<FormatException>( () => Condition.Parse(conditionString));
}

[TestMethod]
public void ParseShouldCreateDefaultConditionWhenOnlyPropertyValuePassed()
{
var conditionString = "ABC";
Condition condition = Condition.Parse(conditionString);
Assert.AreEqual(Condition.DefaultPropertyName, condition.Name);
Assert.AreEqual(Operation.Contains, condition.Operation);
Assert.AreEqual(conditionString, condition.Value);
}

[TestMethod]
public void ParseShouldCreateProperConditionOnValidConditionString()
{
var conditionString = "PropertyName=PropertyValue";
Condition condition = Condition.Parse(conditionString);
Assert.AreEqual("PropertyName", condition.Name);
Assert.AreEqual(Operation.Equal, condition.Operation);
Assert.AreEqual("PropertyValue", condition.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,27 @@ public void GetTestCaseFilterShouldReturnNullIfFilterExpressionIsNull()
Assert.IsNull(this.runContext.GetTestCaseFilter(null, (s) => { return null; }));
}

/// <summary>
/// If only property value passed, consider property key and operation defaults.
/// </summary>
[TestMethod]
public void GetTestCaseFilterShouldThrowOnfilterExpressionParsingError()
public void GetTestCaseFilterShouldNotThrowIfPropertyValueOnlyPassed()
{
this.runContext.FilterExpressionWrapper = new FilterExpressionWrapper("Infinity");

var isExceptionThrown = false;
var filter = this.runContext.GetTestCaseFilter(new List<string>{ "FullyQualifiedName" }, (s) => { return null; });

try
{
this.runContext.GetTestCaseFilter(null, (s) => { return null; });
}
catch (TestPlatformFormatException ex)
{
isExceptionThrown = true;
StringAssert.Contains(ex.Message, "Incorrect format for TestCaseFilter Error: Invalid Condition 'Infinity'. Specify the correct format and try again. Note that the incorrect format can lead to no test getting executed.");
}

Assert.IsTrue(isExceptionThrown);
Assert.IsNotNull(filter);
}

[TestMethod]
public void GetTestCaseFilterShouldThrowOnInvalidProperties()
{
this.runContext.FilterExpressionWrapper = new FilterExpressionWrapper("highlyunlikelyproperty=unused");

var isExceptionThrown = false;

try
{
this.runContext.GetTestCaseFilter(new List<string> { "TestCategory" }, (s) => { return null; });
}
catch (TestPlatformFormatException ex)
{
isExceptionThrown = true;
StringAssert.Contains(ex.Message, "No tests matched the filter because it contains one or more properties that are not valid (highlyunlikelyproperty). Specify filter expression containing valid properties (TestCategory) and try again.");
}
var exception = Assert.ThrowsException<TestPlatformFormatException>(() => this.runContext.GetTestCaseFilter(new List<string> { "TestCategory" }, (s) => { return null; }));

Assert.IsTrue(isExceptionThrown);
StringAssert.Contains(exception.Message, "No tests matched the filter because it contains one or more properties that are not valid (highlyunlikelyproperty). Specify filter expression containing valid properties (TestCategory) and try again.");
}

[TestMethod]
Expand Down