Skip to content

Commit

Permalink
Rename some entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Romfos committed Dec 17, 2023
1 parent 2524e42 commit 3052ac2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
13 changes: 6 additions & 7 deletions src/NGherkin.TestAdapter/NGherkinTestDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using NGherkin.Registrations;
using System.ComponentModel;
using System.Reflection;

Expand Down Expand Up @@ -85,9 +84,9 @@ internal static ServiceProvider GetServiceProvider(Type startupType)

internal static IEnumerable<TestCase> GetTestCases(string source, IServiceProvider serviceProvider)
{
foreach (var gherkinDocumentRegistration in serviceProvider.GetServices<GherkinDocumentRegistration>())
foreach (var gherkinFeature in serviceProvider.GetServices<GherkinFeature>())
{
var feature = gherkinDocumentRegistration.Document.Feature;
var feature = gherkinFeature.Document.Feature;

var featureBackgroundSteps = GetFeatureBackgroundSteps(feature);

Expand All @@ -100,7 +99,7 @@ internal static IEnumerable<TestCase> GetTestCases(string source, IServiceProvid
var testCase = new TestCase()
{
DisplayName = scenario.Name,
FullyQualifiedName = $"{gherkinDocumentRegistration.Name}.{gherkinDocumentRegistration.Document.Feature.Name}.{rule.Name}.{scenario.Name}",
FullyQualifiedName = $"{gherkinFeature.Name}.{gherkinFeature.Document.Feature.Name}.{rule.Name}.{scenario.Name}",
ExecutorUri = new Uri(NGherkinTestExecutor.ExecutorUri),
Source = source,
LocalExtensionData = new TestExecutionContext(feature, scenario, null, ruleBackgroundSteps)
Expand All @@ -124,7 +123,7 @@ internal static IEnumerable<TestCase> GetTestCases(string source, IServiceProvid
var testCase = new TestCase()
{
DisplayName = testName,
FullyQualifiedName = $"{gherkinDocumentRegistration.Name}.{gherkinDocumentRegistration.Document.Feature.Name}.{rule.Name}.{testName}",
FullyQualifiedName = $"{gherkinFeature.Name}.{gherkinFeature.Document.Feature.Name}.{rule.Name}.{testName}",
ExecutorUri = new Uri(NGherkinTestExecutor.ExecutorUri),
Source = source,
LocalExtensionData = new TestExecutionContext(feature, scenario, new(example.TableHeader, body), ruleBackgroundSteps)
Expand All @@ -143,7 +142,7 @@ internal static IEnumerable<TestCase> GetTestCases(string source, IServiceProvid
var testCase = new TestCase()
{
DisplayName = scenario.Name,
FullyQualifiedName = $"{gherkinDocumentRegistration.Name}.{gherkinDocumentRegistration.Document.Feature.Name}.{scenario.Name}",
FullyQualifiedName = $"{gherkinFeature.Name}.{gherkinFeature.Document.Feature.Name}.{scenario.Name}",
ExecutorUri = new Uri(NGherkinTestExecutor.ExecutorUri),
Source = source,
LocalExtensionData = new TestExecutionContext(feature, scenario, null, featureBackgroundSteps)
Expand All @@ -167,7 +166,7 @@ internal static IEnumerable<TestCase> GetTestCases(string source, IServiceProvid
var testCase = new TestCase()
{
DisplayName = testName,
FullyQualifiedName = $"{gherkinDocumentRegistration.Name}.{gherkinDocumentRegistration.Document.Feature.Name}.{testName}",
FullyQualifiedName = $"{gherkinFeature.Name}.{gherkinFeature.Document.Feature.Name}.{testName}",
ExecutorUri = new Uri(NGherkinTestExecutor.ExecutorUri),
Source = source,
LocalExtensionData = new TestExecutionContext(feature, scenario, new(example.TableHeader, body), featureBackgroundSteps)
Expand Down
39 changes: 19 additions & 20 deletions src/NGherkin.TestAdapter/NGherkinTestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using NGherkin.Registrations;
using System.Reflection;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -38,14 +37,14 @@ public void RunTests(IEnumerable<TestCase>? tests, IRunContext? runContext, IFra
}

using var serviceProvider = NGherkinTestDiscoverer.GetServiceProvider(startupType);
var gherkinStepRegistrations = serviceProvider.GetServices<GherkinStepRegistration>();
var gherkinStep = serviceProvider.GetServices<GherkinStep>();

foreach (var testCase in NGherkinTestDiscoverer.GetTestCases(source, serviceProvider))
{
if (testNames.Contains(testCase.FullyQualifiedName))
{
using var scopedServiceProvider = serviceProvider.CreateScope();
RunTest(frameworkHandle, scopedServiceProvider.ServiceProvider, gherkinStepRegistrations, testCase);
RunTest(frameworkHandle, scopedServiceProvider.ServiceProvider, gherkinStep, testCase);
}
}
}
Expand Down Expand Up @@ -74,12 +73,12 @@ public void RunTests(IEnumerable<string>? sources, IRunContext? runContext, IFra
}

using var serviceProvider = NGherkinTestDiscoverer.GetServiceProvider(startupType);
var gherkinStepRegistrations = serviceProvider.GetServices<GherkinStepRegistration>();
var gherkinStep = serviceProvider.GetServices<GherkinStep>();

foreach (var testCase in NGherkinTestDiscoverer.GetTestCases(source, serviceProvider))
{
using var scopedServiceProvider = serviceProvider.CreateScope();
RunTest(frameworkHandle, scopedServiceProvider.ServiceProvider, gherkinStepRegistrations, testCase);
RunTest(frameworkHandle, scopedServiceProvider.ServiceProvider, gherkinStep, testCase);
}
}
catch (Exception exception)
Expand All @@ -92,7 +91,7 @@ public void RunTests(IEnumerable<string>? sources, IRunContext? runContext, IFra
private void RunTest(
IFrameworkHandle frameworkHandle,
IServiceProvider serviceProvider,
IEnumerable<GherkinStepRegistration> gherkinStepRegistrations,
IEnumerable<GherkinStep> gherkinSteps,
TestCase testCase)
{
frameworkHandle.RecordStart(testCase);
Expand All @@ -106,7 +105,7 @@ public void RunTests(IEnumerable<string>? sources, IRunContext? runContext, IFra

try
{
var stepExecutionContexts = GetStepExecutionContexts(serviceProvider, gherkinStepRegistrations, testExecutionContext).ToList();
var stepExecutionContexts = GetStepExecutionContexts(serviceProvider, gherkinSteps, testExecutionContext).ToList();

foreach (var stepExecutionContext in stepExecutionContexts)
{
Expand All @@ -130,54 +129,54 @@ public void RunTests(IEnumerable<string>? sources, IRunContext? runContext, IFra

private IEnumerable<StepExecutionContext> GetStepExecutionContexts(
IServiceProvider serviceProvider,
IEnumerable<GherkinStepRegistration> gherkinStepRegistrations,
IEnumerable<GherkinStep> gherkinSteps,
TestExecutionContext testExecutionContext)
{
var backgroundStepKeyword = "Given";
foreach (var step in testExecutionContext.BackgroundSteps)
{
backgroundStepKeyword = GetRealKeyword(step, backgroundStepKeyword);
var stepText = step.Text;
yield return GetStepExecutionContext(serviceProvider, gherkinStepRegistrations, backgroundStepKeyword, stepText, step.Argument);
yield return GetStepExecutionContext(serviceProvider, gherkinSteps, backgroundStepKeyword, stepText, step.Argument);
}

var keyword = "Given";
foreach (var step in testExecutionContext.Scenario.Steps)
{
keyword = GetRealKeyword(step, keyword);
var stepText = GetRealStepText(step, testExecutionContext);
yield return GetStepExecutionContext(serviceProvider, gherkinStepRegistrations, keyword, stepText, step.Argument);
yield return GetStepExecutionContext(serviceProvider, gherkinSteps, keyword, stepText, step.Argument);
}
}

private StepExecutionContext GetStepExecutionContext(
IServiceProvider serviceProvider,
IEnumerable<GherkinStepRegistration> gherkinStepRegistrations,
IEnumerable<GherkinStep> gherkinSteps,
string keyword,
string stepText,
StepArgument stepArgument)
{
var errorMessageStepText = $"{keyword} {stepText}";

var matchedGherkinStepRegistrations = gherkinStepRegistrations
var matchedGherkinSteps = gherkinSteps
.Where(x => x.Keyword == keyword && x.Pattern.IsMatch(stepText))
.ToList();

if (matchedGherkinStepRegistrations.Count == 0)
if (matchedGherkinSteps.Count == 0)
{
throw new Exception($"Unable to find step implementation for: {errorMessageStepText}");
}

if (matchedGherkinStepRegistrations.Count > 1)
if (matchedGherkinSteps.Count > 1)
{
throw new Exception($"Multiple step implementations were found for: {errorMessageStepText}");
}

var matchedGherkinStepRegistration = matchedGherkinStepRegistrations.Single();
var matchedGherkinStep = matchedGherkinSteps.Single();

var service = serviceProvider.GetRequiredService(matchedGherkinStepRegistration.ServiceType);
var service = serviceProvider.GetRequiredService(matchedGherkinStep.ServiceType);

var parameters = matchedGherkinStepRegistration.Pattern
var parameters = matchedGherkinStep.Pattern
.Match(stepText)
.Groups
.Cast<Group>()
Expand All @@ -187,15 +186,15 @@ public void RunTests(IEnumerable<string>? sources, IRunContext? runContext, IFra

var expectedParametersCount = stepArgument != null ? parameters.Count + 1 : parameters.Count;

if (matchedGherkinStepRegistration.Method.GetParameters().Length != expectedParametersCount)
if (matchedGherkinStep.Method.GetParameters().Length != expectedParametersCount)
{
throw new Exception($"Invalid parameter count for {matchedGherkinStepRegistration.ServiceType.FullName}.{matchedGherkinStepRegistration.Method}");
throw new Exception($"Invalid parameter count for {matchedGherkinStep.ServiceType.FullName}.{matchedGherkinStep.Method}");
}

var stepExecutionContext = new StepExecutionContext(
errorMessageStepText,
service,
matchedGherkinStepRegistration.Method,
matchedGherkinStep.Method,
parameters,
stepArgument);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Gherkin.Ast;

namespace NGherkin.Registrations;
namespace NGherkin;

public sealed class GherkinDocumentRegistration(
public sealed class GherkinFeature(
string name,
GherkinDocument gherkinDocument)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Reflection;
using System.Text.RegularExpressions;

namespace NGherkin.Registrations;
namespace NGherkin;

public sealed class GherkinStepRegistration(
public sealed class GherkinStep(
Type serviceType,
MethodInfo method,
string keyword,
Expand Down
11 changes: 5 additions & 6 deletions src/NGherkin/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Gherkin;
using Microsoft.Extensions.DependencyInjection;
using NGherkin.Attributes;
using NGherkin.Registrations;
using System.Reflection;
using System.Text.RegularExpressions;

Expand All @@ -14,7 +13,7 @@ public static void AddGherkinFeatures(this IServiceCollection services)
var assembly = Assembly.GetCallingAssembly() ?? throw new Exception("Unable to get calling assembly");
var gherkinParser = new Parser();

GherkinDocumentRegistration CreateGherkinDocumentRegistration(Assembly assembly, string resourceName)
GherkinFeature CreateGherkinFeature(Assembly assembly, string resourceName)
{
using var stream = assembly.GetManifestResourceStream(resourceName) ?? throw new Exception($"Unable to get resource {resourceName}");
using var reader = new StreamReader(stream);
Expand All @@ -23,7 +22,7 @@ GherkinDocumentRegistration CreateGherkinDocumentRegistration(Assembly assembly,

var gherkinDocuments = assembly.GetManifestResourceNames()
.Where(resourceName => resourceName.EndsWith(".feature"))
.Select(resourceName => CreateGherkinDocumentRegistration(assembly, resourceName));
.Select(resourceName => CreateGherkinFeature(assembly, resourceName));

foreach (var gherkinDocument in gherkinDocuments)
{
Expand All @@ -44,17 +43,17 @@ public static void AddGherkinSteps(this IServiceCollection services)
{
foreach (var attribute in method.GetCustomAttributes<GivenAttribute>())
{
services.AddSingleton(new GherkinStepRegistration(stepType, method, "Given", new Regex(attribute.Pattern)));
services.AddSingleton(new GherkinStep(stepType, method, "Given", new Regex(attribute.Pattern)));
}

foreach (var attribute in method.GetCustomAttributes<WhenAttribute>())
{
services.AddSingleton(new GherkinStepRegistration(stepType, method, "When", new Regex(attribute.Pattern)));
services.AddSingleton(new GherkinStep(stepType, method, "When", new Regex(attribute.Pattern)));
}

foreach (var attribute in method.GetCustomAttributes<ThenAttribute>())
{
services.AddSingleton(new GherkinStepRegistration(stepType, method, "Then", new Regex(attribute.Pattern)));
services.AddSingleton(new GherkinStep(stepType, method, "Then", new Regex(attribute.Pattern)));
}
}
}
Expand Down

0 comments on commit 3052ac2

Please sign in to comment.