Skip to content

Improve error message for all Assert.Contains.cs APIs #5793

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 16, 2025

The error message for ContainsSingle with a predicate was confusing because it used the same generic message as the non-predicate version, suggesting the collection should have size 1 rather than explaining that exactly one item should match the predicate.

Before:

var collection = new List<int> { 1, 3, 5 };
Assert.ContainsSingle(x => x % 2 == 0, collection);
// Error: "Assert.ContainsSingle failed. Expected collection of size 1. Actual: 0."

This message is misleading because it suggests the entire collection should have size 1, when actually the assertion is checking that exactly one item matches the predicate x % 2 == 0.

After:

var collection = new List<int> { 1, 3, 5 };
Assert.ContainsSingle(x => x % 2 == 0, collection);
// Error: "Assert.ContainsSingle failed. Expected exactly one item to match the predicate. Actual: 0."

The new message clearly explains that the assertion is looking for exactly one item that matches the predicate.

Changes made:

  • Added new resource string ContainsSingleMatchFailMsg for predicate-specific error messages
  • Added ThrowAssertSingleMatchFailed method to handle predicate-specific errors
  • Updated predicate version of ContainsSingle to use the new error method
  • Updated all corresponding tests to expect the new error message format
  • Non-predicate version continues to use the original error message format

This improvement makes debugging failed assertions much clearer for developers using predicate-based ContainsSingle calls.

Fixes #5770.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug (dns block)
  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25313.2.txt (dns block)
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug (dns block)
    • Triggering command: dotnet restore src/TestFramework/TestFramework/TestFramework.csproj --ignore-failed-sources (dns block)
  • d0svsblobprodcus381.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug (dns block)
  • vb4vsblobprodcus33.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits June 16, 2025 20:30
…ment

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Improve message for ContainsSingle Improve error message for ContainsSingle with predicate to clarify what is being checked Jun 16, 2025
@Copilot Copilot AI requested a review from Evangelink June 16, 2025 20:40
Copilot finished work on behalf of Evangelink June 16, 2025 20:40
Copy link
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

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

Please also fix the message for:

  • overloads of ContainsSingle without predicate
  • overloads of Contains
  • overloads of Contains with predicate
  • all overloads of DoesNotContain

…overloads

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 16, 2025

Please also fix the message for:

  • overloads of ContainsSingle without predicate
  • overloads of Contains
    ...

I've implemented improved error messages for all the requested assertion overloads in commit 614da6b:

  • ContainsSingle without predicate: Now shows "Expected collection to contain exactly one element. Actual: {count}." instead of the generic count message
  • Contains overloads: Now show "Expected collection to contain '{item}'." for item-based assertions
  • Contains with predicate: Now shows "Expected at least one item to match the predicate." for predicate-based assertions
  • DoesNotContain overloads: Now show "Expected collection to not contain '{item}'." for item-based and "Expected no items to match the predicate." for predicate-based assertions

All error messages are now specific and clearly explain what the assertion was checking, making debugging much easier for developers.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug --verbosity minimal (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug --verbosity minimal (dns block)
  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: dotnet restore src/TestFramework/TestFramework/TestFramework.csproj --ignore-failed-sources (dns block)
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug --verbosity minimal (dns block)
  • d0svsblobprodcus381.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug --verbosity minimal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug --verbosity minimal (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug --verbosity minimal (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug --verbosity minimal (dns block)
  • vb4vsblobprodcus33.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj --configuration Debug --verbosity minimal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of Evangelink June 16, 2025 20:51
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot finished work on behalf of Evangelink June 16, 2025 21:13
@Evangelink Evangelink marked this pull request as ready for review June 17, 2025 07:22
@Evangelink Evangelink changed the title Improve error message for ContainsSingle with predicate to clarify what is being checked Improve error message for all Assert.Contains.cs APIs Jun 17, 2025
@Evangelink Evangelink enabled auto-merge (squash) June 17, 2025 07:23
@Evangelink
Copy link
Member

I have increased scope of the ticket as I have noticed most of the Assert.Contains APIs were buggy (missing resx entry, wrong API name, ...)

Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

I have increased scope of the ticket as I have noticed most of the Assert.Contains APIs were buggy (missing resx entry, wrong API name, ...)

I've identified the bugs in the Assert.Contains APIs that need to be fixed:

  1. Wrong API names: String Contains/DoesNotContain methods incorrectly use "StringAssert.Contains" instead of "Assert.Contains" in error messages
  2. Wrong parameter order: Contains<T>(IEnumerable<T> collection, Func<T, bool> predicate, string? message) has parameters in wrong order
  3. Wrong XML documentation: DoesNotContain string methods have copy-pasted XML comments incorrectly describing Contains behavior

I'll fix these issues systematically in the next commit.

… docs

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot finished work on behalf of Evangelink June 17, 2025 07:31
Copilot finished work on behalf of nohwnd June 17, 2025 07:51
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

I have increased scope of the ticket as I have noticed most of the Assert.Contains APIs were buggy (missing resx entry, wrong API name, ...)

The expanded scope has been addressed in commits 9767224 and 8f490a2. I've fixed the bugs in Assert.Contains APIs including wrong API names in error messages, incorrect parameter order, and incorrect XML documentation comments.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25313.2.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of Evangelink June 17, 2025 20:05
@Copilot Copilot AI requested a review from Evangelink June 17, 2025 20:05
# Conflicts:
#	src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt
- Change ContainsSingleMatchFailMsg to use "but found {1} item(s)" format
- Change ContainsSingleFailMsg to use "but found {1} element(s)" format
- Remove object values from ContainsItemFailMsg and DoesNotContainItemFailMsg
- Update all corresponding test expectations
- Update all xlf localization files

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 18, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 5rqvsblobprodcus385.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25316.2.txt (dns block)
    • Triggering command: ./.dotnet/dotnet restore src/TestFramework/TestFramework/TestFramework.csproj --ignore-failed-sources (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Copilot Copilot AI requested a review from Evangelink June 18, 2025 11:55
Copilot finished work on behalf of Evangelink June 18, 2025 11:55
# Conflicts:
#	src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve message for ContainsSingle
4 participants