Skip to content

Commit 94eaff5

Browse files
committed
Add tests for node view details
1 parent b300d97 commit 94eaff5

File tree

6 files changed

+20552
-21
lines changed

6 files changed

+20552
-21
lines changed

Syndiesis.Tests/BaseProjectCodeTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public async Task TestAllFilesIndependently()
1717
{
1818
Assert.That(FilesToTest, Is.Not.Empty);
1919

20-
var sourceTests = new List<Task>();
21-
foreach (var file in FilesToTest)
20+
await Parallel.ForEachAsync(
21+
FilesToTest,
22+
TestFile);
23+
24+
async ValueTask TestFile(FileInfo file, CancellationToken cancellationToken)
2225
{
23-
var text = await File.ReadAllTextAsync(file.FullName);
24-
var testTask = TestSource(text);
25-
sourceTests.Add(testTask);
26+
var text = await File.ReadAllTextAsync(file.FullName, cancellationToken);
27+
await TestSource(text);
2628
}
27-
28-
await Task.WhenAll(sourceTests);
2929
}
3030

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

Syndiesis.Tests/Syndiesis.Tests.csproj

+18-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,24 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15-
<PackageReference Include="NUnit" Version="3.14.0" />
16-
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
17-
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
13+
<!-- To prevent the test resources from being compiled -->
14+
<!-- They will usually be taken from external sources and produce errors in this project -->
15+
<Compile Remove="TestResources\**\*.*" />
16+
<Content Include="TestResources\**\*.*" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="coverlet.collector" Version="6.0.2">
21+
<PrivateAssets>all</PrivateAssets>
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
</PackageReference>
24+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
25+
<PackageReference Include="NUnit" Version="4.1.0" />
26+
<PackageReference Include="NUnit.Analyzers" Version="4.3.0">
27+
<PrivateAssets>all</PrivateAssets>
28+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
29+
</PackageReference>
30+
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
1831
</ItemGroup>
1932

2033
<ItemGroup>

0 commit comments

Comments
 (0)