|
| 1 | +// Copyright (c) 2026 Siegfried Pammer |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +// software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +// without restriction, including without limitation the rights to use, copy, modify, merge, |
| 6 | +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 7 | +// to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +// substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 15 | +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 17 | +// DEALINGS IN THE SOFTWARE. |
| 18 | + |
| 19 | +using System.Linq; |
| 20 | +using System.Threading.Tasks; |
| 21 | + |
| 22 | +using Avalonia.Headless.NUnit; |
| 23 | +using Avalonia.VisualTree; |
| 24 | + |
| 25 | +using AwesomeAssertions; |
| 26 | + |
| 27 | +using ICSharpCode.ILSpy.TextView; |
| 28 | +using ICSharpCode.ILSpy.TreeNodes; |
| 29 | + |
| 30 | +using NUnit.Framework; |
| 31 | + |
| 32 | +namespace ICSharpCode.ILSpy.Tests.TextView; |
| 33 | + |
| 34 | +/// <summary> |
| 35 | +/// Sample type decompiled by <see cref="HoverOnlyReferenceTests"/>: a dynamic member access, whose |
| 36 | +/// synthesized member the decompiler renders as a hover-only reference. |
| 37 | +/// </summary> |
| 38 | +public class DynamicMemberSample |
| 39 | +{ |
| 40 | + public object Get(dynamic d) |
| 41 | + { |
| 42 | + return d.Property; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// <summary> |
| 47 | +/// Pins the third reference mode. A member synthesized for a dynamic access shows a hover tooltip but |
| 48 | +/// is not a navigation target and, unlike a local variable, clicking it does not highlight occurrences |
| 49 | +/// (it is a distinct synthetic member at every use). |
| 50 | +/// </summary> |
| 51 | +[TestFixture] |
| 52 | +public class HoverOnlyReferenceTests |
| 53 | +{ |
| 54 | + [AvaloniaTest] |
| 55 | + public async Task Dynamic_Member_Is_HoverOnly_And_A_Click_Neither_Navigates_Nor_Highlights() |
| 56 | + { |
| 57 | + var (window, vm) = await TestHarness.BootAsync(); |
| 58 | + await vm.OpenAssemblyAsync(typeof(DynamicMemberSample).Assembly.Location); |
| 59 | + var typeNode = vm.AssemblyTreeModel.FindNode<TypeTreeNode>( |
| 60 | + "ILSpy.Tests", |
| 61 | + "ICSharpCode.ILSpy.Tests.TextView", |
| 62 | + "ICSharpCode.ILSpy.Tests.TextView.DynamicMemberSample"); |
| 63 | + vm.AssemblyTreeModel.SelectNode(typeNode); |
| 64 | + var tab = await vm.DockWorkspace.WaitForDecompiledTextAsync(); |
| 65 | + var view = window.GetVisualDescendants().OfType<DecompilerTextView>().First(); |
| 66 | + |
| 67 | + var hoverOnly = tab.References! |
| 68 | + .Where(r => r.Kind == ReferenceMode.HoverOnly) |
| 69 | + .ToList(); |
| 70 | + hoverOnly.Should().NotBeEmpty("the dynamic member access 'd.Property' is a hover-only reference"); |
| 71 | + |
| 72 | + var navigated = false; |
| 73 | + tab.NavigateRequested += _ => navigated = true; |
| 74 | + view.OnReferenceClicked(hoverOnly.First()); |
| 75 | + |
| 76 | + navigated.Should().BeFalse("clicking a hover-only reference must not navigate"); |
| 77 | + view.LocalReferenceMarks.Should().BeEmpty("a hover-only reference must not highlight occurrences"); |
| 78 | + } |
| 79 | +} |
0 commit comments