-
Notifications
You must be signed in to change notification settings - Fork 348
Add test project for Microsoft.TestPlatform.Filter.Source source-only package #15649
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
Merged
Youssef1313
merged 4 commits into
dev/ygerges/filter-source-pkg
from
copilot/sub-pr-15638
Apr 8, 2026
+442
−0
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
test/Microsoft.TestPlatform.Filter.Source.UnitTests/FilterExpressionWrapperTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.Common.Filtering; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Microsoft.TestPlatform.Filter.Source.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public class FilterExpressionWrapperTests | ||
| { | ||
| [TestMethod] | ||
| public void ConstructorShouldSetFilterString() | ||
| { | ||
| var filterString = "FullyQualifiedName=Test1"; | ||
| var wrapper = new FilterExpressionWrapper(filterString); | ||
| Assert.AreEqual(filterString, wrapper.FilterString); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ParseErrorShouldBeNullForValidFilter() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name=Test1"); | ||
| Assert.IsNull(wrapper.ParseError); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ParseErrorShouldBeSetForInvalidFilter() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("(Name=Test1"); | ||
| Assert.IsNotNull(wrapper.ParseError); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EvaluateShouldReturnTrueWhenPropertyMatches() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name=Test1"); | ||
|
|
||
| bool result = wrapper.Evaluate(prop => prop == "Name" ? "Test1" : null); | ||
|
|
||
| Assert.IsTrue(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EvaluateShouldReturnFalseWhenPropertyDoesNotMatch() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name=Test1"); | ||
|
|
||
| bool result = wrapper.Evaluate(prop => prop == "Name" ? "Test2" : null); | ||
|
|
||
| Assert.IsFalse(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EvaluateShouldReturnFalseWhenFilterHasParseError() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("(Name=Test1"); | ||
|
|
||
| bool result = wrapper.Evaluate(prop => "Test1"); | ||
|
|
||
| Assert.IsFalse(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EvaluateShouldSupportAndOperator() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name=Test1&Category=Unit"); | ||
|
|
||
| bool result = wrapper.Evaluate(prop => prop switch | ||
| { | ||
| "Name" => "Test1", | ||
| "Category" => "Unit", | ||
| _ => null, | ||
| }); | ||
|
|
||
| Assert.IsTrue(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EvaluateShouldSupportOrOperator() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name=Test1|Name=Test2"); | ||
|
|
||
| bool matchFirst = wrapper.Evaluate(prop => prop == "Name" ? "Test1" : null); | ||
| bool matchSecond = wrapper.Evaluate(prop => prop == "Name" ? "Test2" : null); | ||
| bool matchNeither = wrapper.Evaluate(prop => prop == "Name" ? "Test3" : null); | ||
|
|
||
| Assert.IsTrue(matchFirst); | ||
| Assert.IsTrue(matchSecond); | ||
| Assert.IsFalse(matchNeither); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EvaluateShouldSupportNotEqualOperator() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name!=Test1"); | ||
|
|
||
| bool result = wrapper.Evaluate(prop => prop == "Name" ? "Test2" : null); | ||
|
|
||
| Assert.IsTrue(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EvaluateShouldSupportContainsOperator() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name~Test"); | ||
|
|
||
| bool result = wrapper.Evaluate(prop => prop == "Name" ? "MyTestMethod" : null); | ||
|
|
||
| Assert.IsTrue(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidForPropertiesShouldReturnNullWhenAllPropertiesAreSupported() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name=Test1"); | ||
| var supportedProperties = new List<string> { "Name" }; | ||
|
|
||
| string[]? invalidProperties = wrapper.ValidForProperties(supportedProperties); | ||
|
|
||
| Assert.IsNull(invalidProperties); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidForPropertiesShouldReturnUnsupportedPropertyNames() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("UnknownProperty=Value"); | ||
| var supportedProperties = new List<string> { "Name", "Category" }; | ||
|
|
||
| string[]? invalidProperties = wrapper.ValidForProperties(supportedProperties); | ||
|
|
||
| Assert.IsNotNull(invalidProperties); | ||
| Assert.AreEqual(1, invalidProperties.Length); | ||
| Assert.AreEqual("UnknownProperty", invalidProperties[0]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FastFilterShouldBeCreatedForSimpleEqualityFilter() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name=Test1"); | ||
|
|
||
| Assert.IsNotNull(wrapper.FastFilter); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FastFilterShouldBeNullForComplexFilter() | ||
| { | ||
| var wrapper = new FilterExpressionWrapper("Name=Test1&Name=Test2"); | ||
|
|
||
| Assert.IsNull(wrapper.FastFilter); | ||
| } | ||
| } |
112 changes: 112 additions & 0 deletions
112
test/Microsoft.TestPlatform.Filter.Source.UnitTests/FilterHelperTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Microsoft.TestPlatform.Filter.Source.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public class FilterHelperTests | ||
| { | ||
| [TestMethod] | ||
| public void EscapeShouldReturnOriginalStringWhenNoSpecialCharacters() | ||
| { | ||
| var input = "TestMethod"; | ||
| Assert.AreEqual(input, FilterHelper.Escape(input)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapeOpenParenthesis() | ||
| { | ||
| Assert.AreEqual(@"Test\(Method", FilterHelper.Escape("Test(Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapeCloseParenthesis() | ||
| { | ||
| Assert.AreEqual(@"Test\)Method", FilterHelper.Escape("Test)Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapeAmpersand() | ||
| { | ||
| Assert.AreEqual(@"Test\&Method", FilterHelper.Escape("Test&Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapePipe() | ||
| { | ||
| Assert.AreEqual(@"Test\|Method", FilterHelper.Escape("Test|Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapeEqualSign() | ||
| { | ||
| Assert.AreEqual(@"Test\=Method", FilterHelper.Escape("Test=Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapeExclamationMark() | ||
| { | ||
| Assert.AreEqual(@"Test\!Method", FilterHelper.Escape("Test!Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapeTilde() | ||
| { | ||
| Assert.AreEqual(@"Test\~Method", FilterHelper.Escape("Test~Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapeBackslash() | ||
| { | ||
| Assert.AreEqual(@"Test\\Method", FilterHelper.Escape(@"Test\Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeShouldEscapeMultipleSpecialCharacters() | ||
| { | ||
| Assert.AreEqual(@"Test\(A\|B\)", FilterHelper.Escape("Test(A|B)")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void UnescapeShouldReturnOriginalStringWhenNoEscapeCharacters() | ||
| { | ||
| var input = "TestMethod"; | ||
| Assert.AreEqual(input, FilterHelper.Unescape(input)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void UnescapeShouldUnescapeOpenParenthesis() | ||
| { | ||
| Assert.AreEqual("Test(Method", FilterHelper.Unescape(@"Test\(Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void UnescapeShouldUnescapeCloseParenthesis() | ||
| { | ||
| Assert.AreEqual("Test)Method", FilterHelper.Unescape(@"Test\)Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void UnescapeShouldUnescapeBackslash() | ||
| { | ||
| Assert.AreEqual(@"Test\Method", FilterHelper.Unescape(@"Test\\Method")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void UnescapeShouldThrowOnInvalidEscapeSequence() | ||
| { | ||
| Assert.ThrowsExactly<ArgumentException>(() => FilterHelper.Unescape(@"Test\AMethod")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void EscapeAndUnescapeRoundtrip() | ||
| { | ||
| var input = "TestClass(\"param\").Method(1.5)"; | ||
| Assert.AreEqual(input, FilterHelper.Unescape(FilterHelper.Escape(input))); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...estPlatform.Filter.Source.UnitTests/Microsoft.TestPlatform.Filter.Source.UnitTests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TestProject>true</TestProject> | ||
| <IsTestProject>true</IsTestProject> | ||
| <!-- | ||
| This prevents IS_VSTEST_REPO from being defined (see Directory.Build.targets), | ||
| simulating how end-users consume the source-only package. | ||
| --> | ||
| <IsFilterSourcePackage>true</IsFilterSourcePackage> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>net9.0;net48</TargetFrameworks> | ||
| <OutputType>Exe</OutputType> | ||
| <AssemblyName>Microsoft.TestPlatform.Filter.Source.UnitTests</AssemblyName> | ||
| </PropertyGroup> | ||
|
|
||
| <!-- | ||
| Remove the vstest project references added by Directory.Build.targets for TestProject==true. | ||
| This test project only compiles the source-only filter package files directly and | ||
| does not depend on any vstest assemblies at compile time. | ||
| The removal is done in a target so it runs after Directory.Build.targets has added the items. | ||
| --> | ||
| <Target Name="RemoveConflictingProjectReferences" BeforeTargets="ResolveProjectReferences"> | ||
| <ItemGroup> | ||
| <ProjectReference Remove="@(ProjectReference)" Condition="'%(ProjectReference.FromP2P)' == 'true'" /> | ||
| </ItemGroup> | ||
| </Target> | ||
|
|
||
| <!-- Include source files from the source-only package directly, simulating how end-users consume the package. --> | ||
| <ItemGroup> | ||
| <Compile Include="$(RepoRoot)src/Microsoft.TestPlatform.Filter.Source/Condition.cs" Link="Sources/Condition.cs" /> | ||
| <Compile Include="$(RepoRoot)src/Microsoft.TestPlatform.Filter.Source/EmbeddedAttribute.cs" Link="Sources/EmbeddedAttribute.cs" /> | ||
| <Compile Include="$(RepoRoot)src/Microsoft.TestPlatform.Filter.Source/FastFilter.cs" Link="Sources/FastFilter.cs" /> | ||
| <Compile Include="$(RepoRoot)src/Microsoft.TestPlatform.Filter.Source/FilterExpression.cs" Link="Sources/FilterExpression.cs" /> | ||
| <Compile Include="$(RepoRoot)src/Microsoft.TestPlatform.Filter.Source/FilterExpressionWrapper.cs" Link="Sources/FilterExpressionWrapper.cs" /> | ||
| <Compile Include="$(RepoRoot)src/Microsoft.TestPlatform.Filter.Source/FilterHelper.cs" Link="Sources/FilterHelper.cs" /> | ||
| <Compile Include="$(RepoRoot)src/Microsoft.TestPlatform.Filter.Source/TestCaseFilterExpression.cs" Link="Sources/TestCaseFilterExpression.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Revert the file mode change here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted in 1684c75.