Skip to content

Commit 9228884

Browse files
authored
Merge pull request #85 from Rekkonnect/dev/testing/node-view
Add unit tests for node details view
2 parents a4cac71 + f0993d8 commit 9228884

13 files changed

+20665
-79
lines changed

Syndiesis.Tests/SourceProvider.cs Syndiesis.Tests/AnalysisPipelineHandlerTests.cs

-29
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
11
using AvaloniaEdit.Document;
22
using Syndiesis.Controls.AnalysisVisualization;
33
using Syndiesis.Core;
4-
using System.Collections.Immutable;
5-
6-
[assembly: Parallelizable(ParallelScope.Fixtures)]
74

85
namespace Syndiesis.Tests;
96

10-
public abstract class BaseProjectCodeTests
11-
{
12-
protected static ProjectSourceProvider SourceProvider
13-
= ProjectSourceProvider.Get();
14-
15-
protected static ImmutableArray<FileInfo> FilesToTest
16-
= SourceProvider.GetFilePaths();
17-
18-
[Test]
19-
public async Task TestAllFilesIndependently()
20-
{
21-
var sourceTests = new List<Task>();
22-
foreach (var file in FilesToTest)
23-
{
24-
var text = await File.ReadAllTextAsync(file.FullName);
25-
var testTask = TestSource(text);
26-
sourceTests.Add(testTask);
27-
}
28-
29-
await Task.WhenAll(sourceTests);
30-
}
31-
32-
protected abstract Task TestSource(string text);
33-
}
34-
357
[TestFixtureSource(nameof(AnalysisNodeKindSource))]
368
public sealed class AnalysisPipelineHandlerTests(AnalysisNodeKind analysisNodeKind)
379
: BaseProjectCodeTests
@@ -93,5 +65,4 @@ private static IEnumerable<AnalysisNodeKind> AnalysisNodeKindSource()
9365
AnalysisNodeKind.Attribute,
9466
];
9567
}
96-
9768
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections.Immutable;
2+
3+
[assembly: Parallelizable(ParallelScope.Fixtures)]
4+
5+
namespace Syndiesis.Tests;
6+
7+
public abstract class BaseProjectCodeTests
8+
{
9+
protected static readonly ProjectSourceProvider SourceProvider
10+
= Syndiesis.ProjectSourceProviderGetter.Get();
11+
12+
protected static readonly ProjectSourceProvider TestSourceProvider
13+
= Syndiesis.Tests.ProjectSourceProviderGetter.Get();
14+
15+
protected static readonly ImmutableArray<FileInfo> MainFilesToTest
16+
= SourceProvider.GetFilePaths();
17+
18+
protected static readonly ImmutableArray<FileInfo> TestFilesToTest
19+
= TestSourceProvider.GetFilePaths();
20+
21+
protected static readonly ImmutableArray<FileInfo> FilesToTest =
22+
[
23+
.. MainFilesToTest,
24+
.. TestFilesToTest,
25+
];
26+
27+
[Test]
28+
public async Task TestAllFilesIndependently()
29+
{
30+
Assert.That(FilesToTest, Is.Not.Empty);
31+
32+
await Parallel.ForEachAsync(
33+
FilesToTest,
34+
TestFile);
35+
36+
async ValueTask TestFile(FileInfo file, CancellationToken cancellationToken)
37+
{
38+
var text = await File.ReadAllTextAsync(file.FullName, cancellationToken);
39+
await TestSource(text);
40+
}
41+
}
42+
43+
protected abstract Task TestSource(string text);
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Syndiesis.Tests;
2+
3+
public static class ProjectSourceProviderGetter
4+
{
5+
public static ProjectSourceProvider Get()
6+
{
7+
var thisPath = ProjectSourceProvider.CallerFilePath();
8+
return new(thisPath);
9+
}
10+
}

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)