|
| 1 | +using Garyon.Extensions; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.Text; |
| 4 | +using Syndiesis.Core; |
| 5 | +using Syndiesis.Utilities; |
| 6 | + |
| 7 | +namespace Syndiesis.Tests; |
| 8 | + |
| 9 | +[Parallelizable(ParallelScope.Children)] |
| 10 | +public sealed class NodeViewDetailsHandlerTests |
| 11 | + : BaseProjectCodeTests |
| 12 | +{ |
| 13 | + protected override async Task TestSource(string text) |
| 14 | + { |
| 15 | + var hybridCompilation = new HybridSingleTreeCompilationSource(); |
| 16 | + hybridCompilation.SetSource(text, default); |
| 17 | + await TestEntireHybridCompilationTree(hybridCompilation); |
| 18 | + } |
| 19 | + |
| 20 | + private static async Task TestEntireHybridCompilationTree( |
| 21 | + HybridSingleTreeCompilationSource hybridCompilation) |
| 22 | + { |
| 23 | + var profiling = new SimpleProfiling(); |
| 24 | + int nodeCount = 0; |
| 25 | + int length = 0; |
| 26 | + using (var _ = profiling.BeginProcess()) |
| 27 | + { |
| 28 | + var tree = hybridCompilation.CurrentSource.Tree; |
| 29 | + Assert.That(tree, Is.Not.Null); |
| 30 | + var root = await tree.GetRootAsync(); |
| 31 | + Assert.That(root, Is.Not.Null); |
| 32 | + length = root.FullSpan.Length; |
| 33 | + |
| 34 | + var nodes = root.DescendantNodesAndSelf(descendIntoTrivia: true) |
| 35 | + .ToList(); |
| 36 | + nodeCount = nodes.Count; |
| 37 | + await Parallel.ForEachAsync( |
| 38 | + nodes, |
| 39 | + TestNodeLocal); |
| 40 | + } |
| 41 | + |
| 42 | + var seconds = profiling.SnapshotResults!.Time.TotalSeconds; |
| 43 | + TestContext.Progress.WriteLine($""" |
| 44 | + Finished testing all {nodeCount} nodes from {length} characters in {seconds:N3}s |
| 45 | + """); |
| 46 | + |
| 47 | + async ValueTask TestNodeLocal(SyntaxNode node, CancellationToken cancellationToken) |
| 48 | + { |
| 49 | + await TestNode(hybridCompilation, node); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static async Task TestNode( |
| 54 | + HybridSingleTreeCompilationSource hybridCompilation, |
| 55 | + SyntaxNode node) |
| 56 | + { |
| 57 | + var span = node.Span; |
| 58 | + var result = await TestExecutingResult(hybridCompilation, span); |
| 59 | + var rootNode = result.Root!.Node; |
| 60 | + Assert.That(rootNode, Is.Not.Null); |
| 61 | + |
| 62 | + // For nodes with zero length, this is the equivalent of hovering the caret |
| 63 | + // over the node that will be selected, and thus we care about containing the |
| 64 | + // intended node's span |
| 65 | + if (span.Length is 0) |
| 66 | + { |
| 67 | + Assert.That(rootNode.Span.Contains(span), Is.True); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + Assert.That(rootNode?.FullSpan, Is.EqualTo(node.FullSpan)); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + private static async Task<NodeViewAnalysisExecution> TestExecutingResult( |
| 77 | + HybridSingleTreeCompilationSource hybridCompilation, |
| 78 | + TextSpan span) |
| 79 | + { |
| 80 | + var execution = NodeViewAnalysisHelpers |
| 81 | + .GetNodeViewAnalysisExecutionForSpan(hybridCompilation, span); |
| 82 | + Assert.That(execution, Is.Not.Null); |
| 83 | + |
| 84 | + var result = execution.ExecuteCore(default); |
| 85 | + Assert.That(result, Is.Not.Null); |
| 86 | + |
| 87 | + bool allSuccessful = await result.AwaitAllLoaded(TimeSpan.FromMilliseconds(45)); |
| 88 | + Assert.That(allSuccessful, Is.True); |
| 89 | + return execution; |
| 90 | + } |
| 91 | +} |
0 commit comments