Skip to content

Commit

Permalink
Merge pull request #85 from Rekkonnect/dev/testing/node-view
Browse files Browse the repository at this point in the history
Add unit tests for node details view
  • Loading branch information
Rekkonnect authored Aug 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents a4cac71 + f0993d8 commit 9228884
Showing 13 changed files with 20,665 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
using AvaloniaEdit.Document;
using Syndiesis.Controls.AnalysisVisualization;
using Syndiesis.Core;
using System.Collections.Immutable;

[assembly: Parallelizable(ParallelScope.Fixtures)]

namespace Syndiesis.Tests;

public abstract class BaseProjectCodeTests
{
protected static ProjectSourceProvider SourceProvider
= ProjectSourceProvider.Get();

protected static ImmutableArray<FileInfo> FilesToTest
= SourceProvider.GetFilePaths();

[Test]
public async Task TestAllFilesIndependently()
{
var sourceTests = new List<Task>();
foreach (var file in FilesToTest)
{
var text = await File.ReadAllTextAsync(file.FullName);
var testTask = TestSource(text);
sourceTests.Add(testTask);
}

await Task.WhenAll(sourceTests);
}

protected abstract Task TestSource(string text);
}

[TestFixtureSource(nameof(AnalysisNodeKindSource))]
public sealed class AnalysisPipelineHandlerTests(AnalysisNodeKind analysisNodeKind)
: BaseProjectCodeTests
@@ -93,5 +65,4 @@ private static IEnumerable<AnalysisNodeKind> AnalysisNodeKindSource()
AnalysisNodeKind.Attribute,
];
}

}
44 changes: 44 additions & 0 deletions Syndiesis.Tests/BaseProjectCodeTests.cs
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);
}
91 changes: 91 additions & 0 deletions Syndiesis.Tests/NodeViewDetailsHandlerTests.cs
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;
}
}
10 changes: 10 additions & 0 deletions Syndiesis.Tests/ProjectSourceProviderGetter.cs
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);
}
}
23 changes: 18 additions & 5 deletions Syndiesis.Tests/Syndiesis.Tests.csproj
Original file line number Diff line number Diff line change
@@ -10,11 +10,24 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<!-- To prevent the test resources from being compiled -->
<!-- They will usually be taken from external sources and produce errors in this project -->
<Compile Remove="TestResources\**\*.*" />
<Content Include="TestResources\**\*.*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
Loading

0 comments on commit 9228884

Please sign in to comment.