Skip to content

Commit

Permalink
Also check for namespace when finding matching tests. Fix #191
Browse files Browse the repository at this point in the history
  • Loading branch information
Socolin committed Nov 29, 2023
1 parent fe33349 commit e8a13fc
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JetBrains.Diagnostics;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.ReSharper.UnitTestFramework.Criteria;
using JetBrains.ReSharper.UnitTestFramework.Elements;
Expand All @@ -18,16 +21,36 @@ public static List<IUnitTestElement> GetRelatedFeatureTests(this IUnitTestElemen
var projectTests = unitTestElementRepository.Query(new ProjectCriterion(gherkinFile.GetProject().NotNull()))
.ToList();

var generatedClassName = GetGeneratedClassName(gherkinFile);
var generatedNamespace = GetGeneratedNamespace(gherkinFile);
var relatedTests = projectTests
.Where(t => string.Compare(t.ShortName,
GetGeneratedClassName(gherkinFile), StringComparison.InvariantCultureIgnoreCase) == 0).ToList();
.Where(t => string.Compare(t.ShortName, generatedClassName, StringComparison.InvariantCultureIgnoreCase) == 0)
.Where(x => x.GetNamespace().QualifiedName == generatedNamespace)
.ToList();

if (relatedTests.Count == 0)
return null;

var featureTests = relatedTests.Where(x => x.IsOfKind(UnitTestElementKind.TestContainer)).ToList();
return featureTests;
}

private static string GetGeneratedNamespace(GherkinFile gherkinFile)
{
var sb = new StringBuilder();
foreach (var projectItem in gherkinFile.GetSourceFile().ToProjectFile().GetPathChain().Reverse())
{
if (projectItem is IProjectFolder projectFolder)
{
sb.Append(projectFolder.Name);
sb.Append('.');
}
}
if (sb.Length > 0)
sb.Length--;
return sb.ToString();
}

private static string GetGeneratedClassName(GherkinFile gherkinFile)
{
return $"{gherkinFile.GetFeatures().FirstOrDefault()?.GetFeatureText()?.ToIdentifier()}Feature";
Expand Down

0 comments on commit e8a13fc

Please sign in to comment.