Visitar URL original
Add HaveLineCount()/NotHaveLineCount() and ContainLine()/NotContainLine() to StringAssertions by dennisdoomen · Pull Request #3297 · fluentassertions/fluentassertions · GitHub
Skip to content

Add HaveLineCount()/NotHaveLineCount() and ContainLine()/NotContainLine() to StringAssertions - #3297

Merged
dennisdoomen merged 2 commits into
fluentassertions:mainfrom
dennisdoomen:dennisdoomen-add-have-line-count-contain-line
Aug 10, 2026
Merged

dennisdoomen merged 2 commits into
fluentassertions:mainfrom
dennisdoomen:dennisdoomen-add-have-line-count-contain-line

Conversation

@dennisdoomen

@dennisdoomen dennisdoomen commented Aug 9, 2026 •

Copy link
Copy Markdown
Member

Implements the API proposed in #3291.

What's new

Adds four methods to StringAssertions<TAssertions>, matching the proposal in the issue:

public AndConstraint<TAssertions> HaveLineCount(int expected, string because = "", params object[] becauseArgs);
public AndConstraint<TAssertions> NotHaveLineCount(int unexpected, string because = "", params object[] becauseArgs);
public AndConstraint<TAssertions> ContainLine(string expectedLine, string because = "", params object[] becauseArgs);
public AndConstraint<TAssertions> NotContainLine(string unexpectedLine, string because = "", params object[] becauseArgs);
string log = "Starting up\nConnected to database\nReady";

log.Should().HaveLineCount(3);
log.Should().ContainLine("Connected to database");
log.Should().NotContainLine("Error");
  • Lines are split using \r\n, \n, and \r as line separators (all three treated as equivalent), consistent with line-ending handling already used internally elsewhere in the library.
  • ContainLine/NotContainLine match whole lines only (not substrings), and guard against a null expected/unexpected line via Guard.ThrowIfArgumentIsNull (empty string is a valid line to search for).
  • HaveLineCount/NotHaveLineCount fail explicitly with a <null>-aware message when the subject string is null, following the same pattern as HaveLength.

Tests

Added spec files under Tests/FluentAssertions.Specs/Primitives/:

  • StringAssertionSpecs.HaveLineCount.cs
  • StringAssertionSpecs.NotHaveLineCount.cs
  • StringAssertionSpecs.ContainLine.cs
  • StringAssertionSpecs.NotContainLine.cs

Covering success/failure paths, null subjects, mixed line endings, and argument validation.

Housekeeping

  • Updated Tests/Approval.Tests/ApprovedApi/FluentAssertions/*.verified.txt via AcceptApiChanges.ps1 for the new public API surface.
  • Updated docs/_pages/releases.md and docs/_pages/strings.md.
  • Full FluentAssertions.Specs suite (6003 tests) and approval tests pass locally.

Closes #3291 (pending maintainer API approval).

@dennisdoomen dennisdoomen added this to the 8.11 milestone Aug 9, 2026
dennisdoomen pushed a commit to dennisdoomen/fluentassertions that referenced this pull request Aug 9, 2026
- Point releases.md entry at the implementing PR (fluentassertions#3297) instead of
  the issue, matching the convention used by every other entry in
  the file.
- Document and cover the trailing-line-terminator behavior of
  HaveLineCount/ContainLine (e.g. "one\ntwo\n" has 3 lines), which
  was previously untested and undocumented.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dennisdoomen
dennisdoomen requested a review from jnyrup August 10, 2026 11:23
dennisdoomen pushed a commit to dennisdoomen/fluentassertions that referenced this pull request Aug 10, 2026
- Point releases.md entry at the implementing PR (fluentassertions#3297) instead of
  the issue, matching the convention used by every other entry in
  the file.
- Document and cover the trailing-line-terminator behavior of
  HaveLineCount/ContainLine (e.g. "one\ntwo\n" has 3 lines), which
  was previously untested and undocumented.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dennisdoomen
dennisdoomen force-pushed the dennisdoomen-add-have-line-count-contain-line branch from 4c5b3ea to 166fca2 Compare August 10, 2026 12:01
@dennisdoomen
dennisdoomen force-pushed the dennisdoomen-add-have-line-count-contain-line branch from 82a49b6 to 51e7bbb Compare August 10, 2026 12:07
@dennisdoomen
dennisdoomen force-pushed the dennisdoomen-add-have-line-count-contain-line branch from 51e7bbb to a213a50 Compare August 10, 2026 14:39
dennisdoomen pushed a commit to dennisdoomen/fluentassertions that referenced this pull request Aug 10, 2026
…ases

Addresses review feedback from @jnyrup on fluentassertions#3297.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Dennis Doomen and others added 2 commits August 10, 2026 19:34
…StringAssertions

Adds four new assertion methods to StringAssertions, resolving fluentassertions#3291:
- HaveLineCount / NotHaveLineCount: assert on the number of lines in a
  string, normalizing `\r\n`, `\n` and `\r` line endings.
- ContainLine / NotContainLine: assert whether a string contains a
  specific line. ContainLine returns an AndWhichConstraint exposing the
  matched line for further chaining.

Also fixes a pre-existing gap in the net6.0 API approval baseline
(missing BeJsonSerializable entries from fluentassertions#3293).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ases

Addresses review feedback from @jnyrup on fluentassertions#3297.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dennisdoomen
dennisdoomen force-pushed the dennisdoomen-add-have-line-count-contain-line branch from 8f16f9d to 2816597 Compare August 10, 2026 17:35
@github-actions

Copy link
Copy Markdown

Qodana for .NET

It seems all right 👌

No new problems were found according to the checks applied

💡 Qodana analysis was run in the pull request mode: only the changed files were checked
☁️ View the detailed Qodana report

Contact Qodana team

Contact us at qodana-support@jetbrains.com

@dennisdoomen
dennisdoomen merged commit fbd1e92 into fluentassertions:main Aug 10, 2026
11 of 12 checks passed
@dennisdoomen
dennisdoomen deleted the dennisdoomen-add-have-line-count-contain-line branch August 10, 2026 19:34
This was referenced Nov 12, 2026

This branch was previously deployed

1 inactive deployment
qodana-pr — 2816597f Deployed Aug 10, 2026 by dennisdoomen via Qodana Scan #88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add HaveLineCount() and ContainLine() to StringAssertions

2 participants