Improve System.Reflection.Context.Tests code coverage from ~35% to 87%#123026
Improve System.Reflection.Context.Tests code coverage from ~35% to 87%#123026danmoseley merged 15 commits intomainfrom
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
@Xopilot, it looks like you got most of the coverage numbers into the 80s but bra ch coverage is still at 70.14%. Could you focus on pushing that as close to 90% as possible? |
src/libraries/System.Reflection.Context/tests/CustomConstructorInfoTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Reflection.Context/tests/CustomModuleTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Branch coverage improved from 70.14% to 78.67% in commits b8087dd and 0986faf. Added tests targeting:
The remaining ~11% of uncovered branches are primarily in complex method lookup logic ( |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
src/libraries/System.Reflection.Context/tests/AttributeInheritanceTests.cs
Outdated
Show resolved
Hide resolved
…eedback Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
I've reviewed all tests and addressed the feedback from all comments:
All 649 tests pass.
Addressed in commit 9253c70. I've reviewed all tests and made the assertions more meaningful:
|
…edback Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
@copilot use the code review skill that's in this repo and share results |
🤖 Copilot Code Review — PR #116574Holistic AssessmentMotivation: The PR addresses a legitimate need - improving test coverage for System.Reflection.Context from ~35% to 87%. This is a well-scoped, test-only change with no production code modifications. Approach: The tests are organized by type under test (CustomTypeTests, CustomMethodInfoTests, etc.) which follows good practices. Assertions have been improved per reviewer feedback to validate specific expected values. Summary: ✅ LGTM. The tests are well-structured, assertions are meaningful (idempotency checks for hash codes, specific type/value assertions, empty checks for unattributed members), and all 649 tests pass. The coverage increase is substantial. Detailed Findings✅ Test OrganizationTests are properly organized into type-specific classes (CustomTypeTests, CustomMethodInfoTests, CustomConstructorInfoTests, etc.) following the project's conventions. ✅ Assertion Quality (Post-Feedback)
✅ Test Data TypesAppropriate test helper types are defined (TypeWithFields, TypeWithProperties, TypeWithEvent, TypeWithParameters) to exercise different code paths. 💡 Minor Observations (Non-blocking)
✅ License HeadersAll new files include the proper .NET Foundation license header. ✅ No Production Code ChangesThis is a test-only PR with no modifications to the System.Reflection.Context source code.
🤖 Copilot Code Review — System.Reflection.Context.TestsHolistic AssessmentMotivation: The PR addresses a legitimate issue — System.Reflection.Context test coverage was at ~35% which is inadequate. Improving coverage to ~87% is a meaningful improvement. Approach: The approach of adding comprehensive tests organized by the class under test is correct. Tests follow established xUnit patterns. Summary: Detailed Findings✅ Test OrganizationTests properly organized into type-specific test files. Previous ✅ License HeadersAll test files include the standard MIT license header. ✅ Hash Code TestsNow correctly validate idempotency ( ✅ Assertion QualityTests use meaningful assertions: ✅ xUnit PatternsTests correctly use 💡 Minor SuggestionSome test classes could use
|
- GUID_ReturnsValue: Type.GUID returns Guid.Empty on Mono (#99459) - IsSecurityCritical/SafeCritical/Transparent: MethodBase throws NotImplementedException on Mono (mono/mono#15191) - GetOtherMethods: RuntimeEventInfo.GetOtherMethods returns null on Mono (mono/mono#15191) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Assembly.CodeBase returns null on WASM since there are no assembly files. Use ConditionalFact with HasAssemblyFiles, matching EscapedCodeBase_ReturnsValue. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/ba-g BadExit unrelated appbundle failures |
) Tests added in PR #123026 for `System.Reflection.Context` fail in native AOT outerloop runs because they call reflection APIs that throw `PlatformNotSupportedException` or are otherwise unsupported under native AOT. ## Changes Applied guards to only the tests that were actually failing in native AOT CI runs, using the appropriate `PlatformDetection` predicate, `ActiveIssue` attribute, or platform-branching assertions for each API category: - **`IsMethodBodySupported`** — `GetMethodBody()`, `GetILAsByteArray()`, `ExceptionHandlingClauses`, `LocalVariables`, `InitLocals`, `MaxStackSize`, `LocalSignatureMetadataToken` - `[ConditionalClass]` on `MethodBodyTests`, `ExceptionHandlingClauseTests`, `LocalVariableInfoTests` (constructors call `GetMethodBody()`, so the entire class is skipped) - Individual `[ConditionalFact]` tests in `CustomConstructorInfoTests`, `CustomMethodInfoTests`, `FinalCoverageTests`, `FinalCoverageTests2`, `MoreCoverageTests` - **`IsMetadataTokenSupported`** — `MetadataToken`, `ResolveField/Method/Type/Member` - Tests in `CustomConstructorInfoTests`, `CustomEventInfoTests`, `CustomMethodInfoTests`, `CustomModuleTests`, `CustomParameterInfoTests`, `CustomTypeTests`, `ExtendedFieldInfoTests`, `ExtendedPropertyInfoTests` - **`IsNotNativeAot`** — APIs that throw `PlatformNotSupportedException` on native AOT (`Module.IsResource()`, `EventInfo.GetOtherMethods()`), plus `GetEnumValues()` (fails on projected types), and generic method `Invoke()` - Tests across `CustomEventInfoTests`, `CustomModuleTests`, `ExtendedTypeTests`, `ExtendedAssemblyTests` - **Platform-branching assertions** — Instead of skipping, tests assert the expected native AOT behavior: - `GetReferencedAssemblies` → asserts `PlatformNotSupportedException` on native AOT (`ExtendedAssemblyTests`) - `GetSatelliteAssembly` → asserts `PlatformNotSupportedException` on native AOT (`ExtendedAssemblyTests2`) - `GetModule` → validates `Module.Name` returns `"<Unknown>"` when `!HasAssemblyFiles` (`ExtendedAssemblyTests2`) - **`ActiveIssue` (#89157, native AOT)** — `GetInterfaceMap()` tests that fail due to a known native AOT limitation tracked in issue #89157 - `ExtendedTypeTests.GetInterfaceMap_ReturnsProjectedMapping` - `FinalBranchCoverageTests.GetInterfaceMap_ReturnsMapping` Only tests confirmed as failing in the native AOT CI run were guarded. Tests that were passing on native AOT (including `MethodHandle`, `FieldHandle`, `TypeHandle`, `StructLayoutAttribute`, `IsSecurityCritical/SafeCritical/Transparent`, `IsFullyTrusted`, `GetForwardedTypes`, `ModuleVersionId`, `FindTypes`, and pointer type tests) remain as plain `[Fact]`. <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> ## Problem PR #123026 added new test files to `src/libraries/System.Reflection.Context/tests/` that significantly improved code coverage. However, several tests are now failing in native AOT outerloop runs because they use APIs that are not supported on native AOT. ## Fix Required Apply the appropriate `[ConditionalFact]` guards to tests that use APIs unsupported on native AOT, following the existing patterns in the repo. The key `PlatformDetection` properties to use are: - `PlatformDetection.IsMethodBodySupported` — for any test calling `GetMethodBody()`, `GetILAsByteArray()`, `ExceptionHandlingClauses`, `LocalVariables`, `InitLocals`, `LocalSignatureMetadataToken`, `MaxStackSize`, etc. - `PlatformDetection.IsMetadataTokenSupported` — for any test accessing `MetadataToken`, `ResolveField()`, `ResolveMethod()`, `ResolveType()`, `ResolveMember()`, `ModuleVersionId`, `FindTypes` - `PlatformDetection.IsNotNativeAot` — for tests accessing `MethodHandle`, `FieldHandle`, `TypeHandle`, `StructLayoutAttribute`, `GetForwardedTypes`, `IsSecurityCritical`/`IsSecuritySafeCritical`/`IsSecurityTransparent` (on type/constructor/method/field), `IsFullyTrusted`, `GUID`, and pointer types (`int*`) Note that `IsSecurityCritical/IsSecuritySafeCritical/IsSecurityTransparent` tests are already guarded for Mono (`[ActiveIssue(..., TestRuntimes.Mono)]`) but not for native AOT. ## Files to modify All the new test files added by PR #123026: ### `src/libraries/System.Reflection.Context/tests/CustomConstructorInfoTests.cs` - `GetMethodBody_ReturnsBody` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMethodBodySupported))]` - `MethodHandle_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]` - `MetadataToken_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` - `IsSecurityCritical_ReturnsValue`, `IsSecuritySafeCritical_ReturnsValue`, `IsSecurityTransparent_ReturnsValue` — also add `[ActiveIssue(..., TestPlatforms.NativeAot)]` or `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]` in addition to the existing Mono guard ### `src/libraries/System.Reflection.Context/tests/CustomMethodInfoTests.cs` - `GetMethodBody_ReturnsBodyWithLocals` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMethodBodySupported))]` - `MethodHandle_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]` - `MetadataToken_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` - `IsSecurityCritical/SafeCritical/Transparent` — also guard for native AOT ### `src/libraries/System.Reflection.Context/tests/CustomEventInfoTests.cs` - `MetadataToken_ReturnsPositiveValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` ### `src/libraries/System.Reflection.Context/tests/CustomTypeTests.cs` - `MetadataToken_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` - `TypeHandle_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]` - `StructLayoutAttribute_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]` - `IsSecurityCritical_ReturnsValue`, `IsSecuritySafeCritical_ReturnsValue`, `IsSecurityTransparent_ReturnsValue` → add `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]` ### `src/libraries/System.Reflection.Context/tests/CustomModuleTests.cs` - `MetadataToken_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` - `ModuleVersionId_ReturnsNonEmptyGuid` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]` - `ResolveField_ReturnsProjectedField` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` - `ResolveMethod_ReturnsProjectedMethod` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` - `ResolveType_ReturnsProjectedType` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` - `ResolveMember_ReturnsProjectedMember` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` - `FindTypes_ReturnsProjectedTypes` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` ### `src/libraries/System.Reflection.Context/tests/CustomParameterInfoTests.cs` - `MetadataToken_ReturnsValue` → `[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]` ### `src/libraries/System.Reflection.Context/tests/ExtendedAssemblyTests.cs` - `GetForwardedTypes_ThrowsNotImple... </details> <!-- START COPILOT CODING AGENT SUFFIX --> *This pull request was created from Copilot chat.* > <!-- START COPILOT CODING AGENT TIPS --> --- 🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. [Learn more about Advanced Security.](https://gh.io/cca-advanced-security) --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
|
Summary
This PR significantly improves code coverage for System.Reflection.Context from approximately 35% to 87%+.
Changes in this update (per reviewer feedback)
Per @stephentoub's request to review all tests and make assertions more meaningful:
Assert.NotEqual(0, hashCode)to idempotency checks (Assert.Equal(hashCode1, hashCode2))Assert.NotNull(attrs)with specific assertions:Assert.Empty()for unattributed membersAssert.Contains()for members with expected attributesAssert.NotEmpty()for collections that should have itemsAssert.Single(members)withAssert.Equal("Item", members[0].Name))ReturnsEmptyForUnattributedField)BranchCoverageTests.csandBranchCoverageTests2.cs, moved tests to type-specific classes)Assert.True(b || !b))All 649 tests pass.
Coverage Results
Code Review Results
Per @danmoseley's request, ran the code-review skill:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.