Skip to content

Add unit tests for the weaver - #4

Merged
christophwille merged 1 commit into
masterfrom
weaver-unit-tests
Nov 10, 2026
Merged

christophwille merged 1 commit into
masterfrom
weaver-unit-tests

Conversation

@christophwille

Copy link
Copy Markdown
Member

Summary

Follow-up 3 from #1: the only tests were whole-project builds under tests/, which is how the TypeReferenceEquivalenceComparer bug (Cecil's GetElementType() returns the innermost type, so Nullable<bool>& compared equal to Nullable<byte>&) went unnoticed. This adds a small, fast unit test project for the weaver and runs it in CI.

ICSharpCode.ReferenceAssemblyAnnotator.Tests: xUnit v3 (xunit.v3.mtp-v2 4.0.3) on Microsoft Testing Platform, net10.0. The tests build tiny Cecil modules in memory (CecilBuilder), so they need no files and no real reference assemblies, and run in about a second.

What the tests cover (12 tests)

  • Type comparer: by-ref, pointer and array types of Nullable<bool> vs Nullable<byte> (the regression); array rank; nested vs top-level types; generic arity; the same type shape from two different modules/assemblies compares equal (and has the same hash code).
  • Method comparer: overloads that differ only in a by-ref generic argument are told apart.
  • Program.AnnotateType (the full type walk, including FindMatchingMethod): the M(out bool?) / M(out byte?) case that used to throw in SingleOrDefault (MSB4018 for consumers); each overload now gets its own annotation.
  • Program.Annotate: existing nullability attributes are replaced, not duplicated; unrelated attributes are kept.
  • IsExcludedAnnotation: [DisallowNull] is not copied onto EqualityComparer<T>.GetHashCode / IEqualityComparer<T>.GetHashCode (theory over both types), while other annotations on the same parameter still are.

Changes

  • New project ICSharpCode.ReferenceAssemblyAnnotator.Tests/, added to ReferenceAssemblyAnnotator.slnx. It inherits signing, nullable, StyleCop and warnings-as-errors from Directory.Build.props; IsPackable=false.
    • <PackageDownload Remove="Microsoft.NETCore.App.Ref" />: net10.0 isn't annotated, and the SDK adds its own targeting pack download when the pack isn't installed, so the repo-wide one would fail with NU1505 (same reason as the tests/ projects).
  • Weaver (minimal): InternalsVisibleTo for the test assembly (same Key.snk, so the same public key as CecilBasedAnnotator), and Program.AnnotateType / Program.Annotate changed from private to internal. No behavior change.
  • global.json: "test": { "runner": "Microsoft.Testing.Platform" }, so dotnet test runs in MTP mode.
  • Workflow: new "Unit tests" step after "Build and pack" (dotnet test --project ICSharpCode.ReferenceAssemblyAnnotator.Tests -c Release --no-build). setup-dotnet now installs 10.0.x alongside 11.0.x, because the net10.0 test app needs the .NET 10 runtime and MTP apps don't roll forward across major versions.
  • CLAUDE.md: the "There is no unit test project" line is replaced with how the unit tests work and how to run them.

Validation

  • dotnet build ReferenceAssemblyAnnotator.slnx -c Release: 0 warnings, 0 errors.
  • Unit tests: 12/12 pass.
  • The tests catch the original bug: with the comparer's by-ref/pointer branches temporarily reverted to GetElementType(), 4 tests fail (ByReference_…, Pointer_…, MethodDefinition_…, AnnotateType_MatchesByRefGenericOverloads). The fix was restored afterwards.
  • Only the annotator .nupkg is produced (the test project isn't packed).
  • build/test.ps1 -Configuration Release -Engine dotnet: all 17 test builds pass. The msbuild engine wasn't run locally; CI covers it.

Not done, on purpose

🤖 Generated with Claude Code

….Tests)

xUnit v3 on Microsoft Testing Platform, net10.0. Covers the equivalence
comparers and Program.Annotate/AnnotateType on in-memory Cecil modules,
including the by-ref generic instance regression from #1. Runs in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@christophwille
christophwille merged commit d97e2e7 into master Nov 10, 2026
2 checks passed
@christophwille
christophwille deleted the weaver-unit-tests branch November 10, 2026 07:19
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.

1 participant