Skip to content

fix: exclude internal and sealed override members from mock generation#338

Merged
vbreuss merged 11 commits intomainfrom
copilot/fix-flowdocument-mock-issues
Jan 2, 2026
Merged

fix: exclude internal and sealed override members from mock generation#338
vbreuss merged 11 commits intomainfrom
copilot/fix-flowdocument-mock-issues

Conversation

Copy link
Contributor

Copilot AI commented Jan 2, 2026

  • Understand the root cause of sealed override and internal member issues
  • Fix the source generator to exclude internal members (unless InternalsVisibleTo or abstract)
  • Add tests for internal member filtering
  • Fix the source generator to exclude sealed override members from mock generation
  • Add tests for sealed override member filtering
  • Add comprehensive test for complex inheritance scenarios
  • Address code review feedback (refactor for maintainability)
  • Fix nullability comparison for sealed override special methods (addresses comment #3705993017)
  • Improve string manipulation safety
  • Run security checks (CodeQL - no issues found)
  • Verify all tests pass (104 source generator tests + 1670 core tests passing)
Original prompt

This section details on the original issue you should resolve

<issue_title>Compilation Error in generated mocks when using FlowDocument</issue_title>
<issue_description>See branch topic/flowdocument-not-compiling:

When trying to create a mock for FlowDocument, the source generation does not compile, because

  • internal properties are mocked, but should only be mocked, when internal within the project
  • methods or properties that are sealed override are mocked, but should not (as they are sealed)</issue_description>

<agent_instructions>Check the SealedOverride_ShouldWorkForProperties test in branch topic/flowdocument-not-compiling and fix the issues, so that all tests pass</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits January 2, 2026 15:23
- Filter out internal and protected internal members from classes (not interfaces)
- Keep abstract members even if they are internal/protected internal
- Add tests for internal member filtering scenarios

Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
… chain

- Accumulate sealed members from all types in inheritance chain
- Add tests for sealed override methods and properties from base classes

Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
- Add test combining sealed override, internal, and protected internal members

Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
- Extract ShouldIncludeMember helper method
- Use null-coalescing assignment operator for except list initialization
- Improve code maintainability while preserving functionality

Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix compilation error in generated mocks for FlowDocument fix: exclude internal and sealed override members from mock generation Jan 2, 2026
Copilot AI requested a review from vbreuss January 2, 2026 15:41
@vbreuss
Copy link
Contributor

vbreuss commented Jan 2, 2026

@copilot When I try to build the FlowDocument mock, I still get a (single) error:

6>MockForFlowDocument.g.cs(271,23): Error CS0239 : "MockForFlowDocument.Equals(object?)": Der geerbte Member "DependencyObject.Equals(object)" kann nicht überschrieben werden, da er versiegelt ist.

So also for the special methods (Equals, ToString, GetHashCode), we have to check, if they can be overwritten.

…verride filtering

When filtering sealed override methods, parameter comparison now ignores nullable annotations (? suffix). This fixes the issue where DependencyObject.Equals(object) wouldn't match object.Equals(object?) due to nullability differences, causing sealed methods to not be properly filtered.

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

Copilot AI commented Jan 2, 2026

@copilot When I try to build the FlowDocument mock, I still get a (single) error:

6>MockForFlowDocument.g.cs(271,23): Error CS0239 : "MockForFlowDocument.Equals(object?)": Der geerbte Member "...

Fixed in commit b9a42df. The issue was that DependencyObject.Equals(object) has a non-nullable parameter while object.Equals(object?) has a nullable parameter. The method comparison now ignores nullability annotations when filtering sealed overrides, so both signatures are recognized as the same method.

…tation

Replace TrimEnd('?') with explicit EndsWith check and Substring to avoid accidentally removing multiple trailing '?' characters from complex generic types.

Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
@github-actions
Copy link

github-actions bot commented Jan 2, 2026

Test Results

    14 files  ±0      14 suites  ±0   5m 36s ⏱️ +28s
 1 797 tests +3   1 796 ✅ +3  1 💤 ±0  0 ❌ ±0 
11 996 runs  +9  11 995 ✅ +9  1 💤 ±0  0 ❌ ±0 

Results for commit 1ae06ec. ± Comparison against base commit b1c7ecf.

♻️ This comment has been updated with latest results.

@github-actions
Copy link

github-actions bot commented Jan 2, 2026

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.3 LTS (Noble Numbat)
AMD EPYC 7763 2.63GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.101
[Host] : .NET 10.0.1 (10.0.1, 10.0.125.57005), X64 RyuJIT x86-64-v3

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean Error StdDev Gen0 Gen1 Allocated
Simple_Mockolate 1.454 μs 0.0121 μs 0.0108 μs 0.2232 - 3.66 KB
Simple_Moq 179.653 μs 0.5345 μs 0.4463 μs 0.4883 - 14.55 KB
Simple_NSubstitute 6.000 μs 0.0790 μs 0.0700 μs 0.5569 0.0076 9.14 KB
Simple_FakeItEasy 6.440 μs 0.0314 μs 0.0294 μs 0.4959 - 8.11 KB

@vbreuss vbreuss marked this pull request as ready for review January 2, 2026 19:32
Copilot AI review requested due to automatic review settings January 2, 2026 19:32
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes compilation errors in generated mocks when dealing with internal and sealed override members. The root cause was that the source generator was attempting to mock members that cannot be overridden: internal members from external assemblies and sealed override members in the inheritance chain.

Key changes:

  • Added source assembly tracking to filter out external internal members (unless abstract or in interfaces)
  • Enhanced member filtering to exclude sealed override members from base classes
  • Improved method equality comparison to handle nullability differences in parameters (e.g., object vs object?)

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Tests/Mockolate.SourceGenerators.Tests/Sources/ForMockTests.cs Added comprehensive tests for complex inheritance scenarios with sealed and internal members, including special method handling
Source/Mockolate.SourceGenerators/MockGeneratorHelpers.cs Updated to pass source assembly information to MockClass constructors for internal member filtering
Source/Mockolate.SourceGenerators/Entities/MockClass.cs Updated constructor to receive and propagate source assembly information
Source/Mockolate.SourceGenerators/Entities/Method.cs Enhanced equality comparison to ignore nullability annotations when matching methods (e.g., object vs object?)
Source/Mockolate.SourceGenerators/Entities/Class.cs Added internal member filtering logic and source assembly tracking; refactored sealed member exclusion

@vbreuss vbreuss enabled auto-merge (squash) January 2, 2026 19:35
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 2, 2026

@vbreuss vbreuss disabled auto-merge January 2, 2026 19:36
@vbreuss vbreuss merged commit e8e30d7 into main Jan 2, 2026
12 checks passed
@vbreuss vbreuss deleted the copilot/fix-flowdocument-mock-issues branch January 2, 2026 19:39
@github-actions
Copy link

github-actions bot commented Jan 2, 2026

This is addressed in release v0.48.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compilation Error in generated mocks when using FlowDocument

3 participants