Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 31 additions & 0 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,37 @@ public void HasMetadata()
logger.AssertLogContains("[One|Three|Four]");
}


/// <summary>
/// Filter items by WithoutMetadataValue function
/// </summary>
[Fact]
public void WithoutMetadataValue()
{
MockLogger logger = Helpers.BuildProjectWithNewOMExpectSuccess("""
<Project>
<ItemGroup>
<_Item Include="One">
<A>true</A>
</_Item>
<_Item Include="Two">
<A>false</A>
</_Item>
<_Item Include="Three">
<A></A>
</_Item>
<_Item Include="Four">
<B></B>
</_Item>
</ItemGroup>
<Target Name="AfterBuild">
<Message Text="[@(_Item->WithoutMetadataValue('a', 'true'),'|')]"/>
</Target>
</Project>
""");

logger.AssertLogContains("[Two|Three|Four]");
}
[Fact]
public void DirectItemMetadataReferenceShouldBeCaseInsensitive()
{
Expand Down
36 changes: 36 additions & 0 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2843,6 +2843,42 @@ internal static IEnumerable<Pair<string, S>> WithMetadataValue(Expander<P, I> ex
}
}

/// <summary>
/// Intrinsic function that returns those items don't have the given metadata value
/// Using a case insensitive comparison.
/// </summary>
internal static IEnumerable<Pair<string, S>> WithoutMetadataValue(Expander<P, I> expander, IElementLocation elementLocation, bool includeNullEntries, string functionName, IEnumerable<Pair<string, S>> itemsOfType, string[] arguments)
{
ProjectErrorUtilities.VerifyThrowInvalidProject(arguments?.Length == 2, elementLocation, "InvalidItemFunctionSyntax", functionName, arguments == null ? 0 : arguments.Length);

string metadataName = arguments[0];
string metadataValueToFind = arguments[1];

foreach (Pair<string, S> item in itemsOfType)
{
string metadataValue = null;

try
{
metadataValue = item.Value.GetMetadataValueEscaped(metadataName);
}
catch (ArgumentException ex) // Blank metadata name
{
ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "CannotEvaluateItemMetadata", metadataName, ex.Message);
}
catch (InvalidOperationException ex)
{
ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "CannotEvaluateItemMetadata", metadataName, ex.Message);
}

if (!String.Equals(metadataValue, metadataValueToFind, StringComparison.OrdinalIgnoreCase))
{
// return a result through the enumerator
yield return new Pair<string, S>(item.Key, item.Value);
}
}
}

/// <summary>
/// Intrinsic function that returns a boolean to indicate if any of the items have the given metadata value
/// Using a case insensitive comparison.
Expand Down