-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from Rekkonnect/dev/testing/node-view
Add unit tests for node details view
- Loading branch information
Showing
13 changed files
with
20,665 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Collections.Immutable; | ||
|
||
[assembly: Parallelizable(ParallelScope.Fixtures)] | ||
|
||
namespace Syndiesis.Tests; | ||
|
||
public abstract class BaseProjectCodeTests | ||
{ | ||
protected static readonly ProjectSourceProvider SourceProvider | ||
= Syndiesis.ProjectSourceProviderGetter.Get(); | ||
|
||
protected static readonly ProjectSourceProvider TestSourceProvider | ||
= Syndiesis.Tests.ProjectSourceProviderGetter.Get(); | ||
|
||
protected static readonly ImmutableArray<FileInfo> MainFilesToTest | ||
= SourceProvider.GetFilePaths(); | ||
|
||
protected static readonly ImmutableArray<FileInfo> TestFilesToTest | ||
= TestSourceProvider.GetFilePaths(); | ||
|
||
protected static readonly ImmutableArray<FileInfo> FilesToTest = | ||
[ | ||
.. MainFilesToTest, | ||
.. TestFilesToTest, | ||
]; | ||
|
||
[Test] | ||
public async Task TestAllFilesIndependently() | ||
{ | ||
Assert.That(FilesToTest, Is.Not.Empty); | ||
|
||
await Parallel.ForEachAsync( | ||
FilesToTest, | ||
TestFile); | ||
|
||
async ValueTask TestFile(FileInfo file, CancellationToken cancellationToken) | ||
{ | ||
var text = await File.ReadAllTextAsync(file.FullName, cancellationToken); | ||
await TestSource(text); | ||
} | ||
} | ||
|
||
protected abstract Task TestSource(string text); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using Garyon.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Syndiesis.Core; | ||
using Syndiesis.Utilities; | ||
|
||
namespace Syndiesis.Tests; | ||
|
||
[Parallelizable(ParallelScope.Children)] | ||
public sealed class NodeViewDetailsHandlerTests | ||
: BaseProjectCodeTests | ||
{ | ||
protected override async Task TestSource(string text) | ||
{ | ||
var hybridCompilation = new HybridSingleTreeCompilationSource(); | ||
hybridCompilation.SetSource(text, default); | ||
await TestEntireHybridCompilationTree(hybridCompilation); | ||
} | ||
|
||
private static async Task TestEntireHybridCompilationTree( | ||
HybridSingleTreeCompilationSource hybridCompilation) | ||
{ | ||
var profiling = new SimpleProfiling(); | ||
int nodeCount = 0; | ||
int length = 0; | ||
using (var _ = profiling.BeginProcess()) | ||
{ | ||
var tree = hybridCompilation.CurrentSource.Tree; | ||
Assert.That(tree, Is.Not.Null); | ||
var root = await tree.GetRootAsync(); | ||
Assert.That(root, Is.Not.Null); | ||
length = root.FullSpan.Length; | ||
|
||
var nodes = root.DescendantNodesAndSelf(descendIntoTrivia: true) | ||
.ToList(); | ||
nodeCount = nodes.Count; | ||
await Parallel.ForEachAsync( | ||
nodes, | ||
TestNodeLocal); | ||
} | ||
|
||
var seconds = profiling.SnapshotResults!.Time.TotalSeconds; | ||
TestContext.Progress.WriteLine($""" | ||
Finished testing all {nodeCount} nodes from {length} characters in {seconds:N3}s | ||
"""); | ||
|
||
async ValueTask TestNodeLocal(SyntaxNode node, CancellationToken cancellationToken) | ||
{ | ||
await TestNode(hybridCompilation, node); | ||
} | ||
} | ||
|
||
private static async Task TestNode( | ||
HybridSingleTreeCompilationSource hybridCompilation, | ||
SyntaxNode node) | ||
{ | ||
var span = node.Span; | ||
var result = await TestExecutingResult(hybridCompilation, span); | ||
var rootNode = result.Root!.Node; | ||
Assert.That(rootNode, Is.Not.Null); | ||
|
||
// For nodes with zero length, this is the equivalent of hovering the caret | ||
// over the node that will be selected, and thus we care about containing the | ||
// intended node's span | ||
if (span.Length is 0) | ||
{ | ||
Assert.That(rootNode.Span.Contains(span), Is.True); | ||
} | ||
else | ||
{ | ||
Assert.That(rootNode?.FullSpan, Is.EqualTo(node.FullSpan)); | ||
} | ||
|
||
} | ||
|
||
private static async Task<NodeViewAnalysisExecution> TestExecutingResult( | ||
HybridSingleTreeCompilationSource hybridCompilation, | ||
TextSpan span) | ||
{ | ||
var execution = NodeViewAnalysisHelpers | ||
.GetNodeViewAnalysisExecutionForSpan(hybridCompilation, span); | ||
Assert.That(execution, Is.Not.Null); | ||
|
||
var result = execution.ExecuteCore(default); | ||
Assert.That(result, Is.Not.Null); | ||
|
||
bool allSuccessful = await result.AwaitAllLoaded(TimeSpan.FromMilliseconds(45)); | ||
Assert.That(allSuccessful, Is.True); | ||
return execution; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Syndiesis.Tests; | ||
|
||
public static class ProjectSourceProviderGetter | ||
{ | ||
public static ProjectSourceProvider Get() | ||
{ | ||
var thisPath = ProjectSourceProvider.CallerFilePath(); | ||
return new(thisPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.