<commit_before>﻿using System;
using CSF.Screenplay.Scenarios;

namespace CSF.Screenplay.Integration
{
  public abstract class ScreenplayIntegration : IScreenplayIntegration
  {
    static ScreenplayEnvironment environment;

    public void EnsureServicesAreRegistered()
    {
      environment.ConfigureServiceRegistryIfRequired(GetServiceRegistry);
    }

    public void BeforeExecutingFirstScenario()
    {
      BeforeExecutingFirstScenario(environment, CreateSingletonResolver());
      NotifySubscribersOfTestRunStart();
    }

    protected virtual void BeforeExecutingFirstScenario(IProvidesTestRunEvents testRunEvents,
                                                        IServiceResolver serviceResolver)
    {
      // Intentional no-op for subclasses to implement
    }

    public void BeforeScenario(ScreenplayScenario scenario)
    {
      CustomiseScenario(scenario);
      MarkAsBegun(scenario);
    }

    public void AfterScenario(ScreenplayScenario scenario, bool success)
    {
      AfterScenario(scenario);
      MarkAsEnded(scenario, success);
    }

    public void AfterExecutedLastScenario()
    {
      NotifySubscribersOfTestRunCompletion();
      AfterExecutedLastScenario(CreateSingletonResolver());
    }

    protected virtual void AfterExecutedLastScenario(IServiceResolver serviceResolver)
    {
      // Intentional no-op for subclasses to implement
    }

    protected abstract IServiceRegistrationProvider GetRegistrationProvider();

    protected virtual void CustomiseScenario(ScreenplayScenario scenario)
    {
      // Intentional no-op for subclasses to implement
    }

    protected virtual void AfterScenario(ScreenplayScenario scenario)
    {
      // Intentional no-op for subclasses to implement
    }

    public IScenarioFactory GetScenarioFactory() => environment.GetScenarioFactory();

    protected void MarkAsBegun(ScreenplayScenario scenario) => scenario.Begin();

    protected void MarkAsEnded(ScreenplayScenario scenario, bool success) => scenario.End(success);

    protected IServiceResolver CreateSingletonResolver() => environment.CreateSingletonResolver();

    protected void NotifySubscribersOfTestRunStart() => environment.NotifyBeginTestRun();

    protected void NotifySubscribersOfTestRunCompletion() => environment.NotifyCompleteTestRun();

    ServiceRegistry GetServiceRegistry()
    {
      var provider = GetRegistrationProvider();
      return provider.GetServiceRegistry();
    }

    public ScreenplayIntegration()
    {
      environment = new ScreenplayEnvironment();
    }
  }
}
<commit_msg>Fix problem in integration base (repeated construction of singleton)<commit_after>﻿using System;
using CSF.Screenplay.Scenarios;

namespace CSF.Screenplay.Integration
{
  public abstract class ScreenplayIntegration : IScreenplayIntegration
  {
    static ScreenplayEnvironment environment;

    public void EnsureServicesAreRegistered()
    {
      environment.ConfigureServiceRegistryIfRequired(GetServiceRegistry);
    }

    public void BeforeExecutingFirstScenario()
    {
      BeforeExecutingFirstScenario(environment, CreateSingletonResolver());
      NotifySubscribersOfTestRunStart();
    }

    protected virtual void BeforeExecutingFirstScenario(IProvidesTestRunEvents testRunEvents,
                                                        IServiceResolver serviceResolver)
    {
      // Intentional no-op for subclasses to implement
    }

    public void BeforeScenario(ScreenplayScenario scenario)
    {
      CustomiseScenario(scenario);
      MarkAsBegun(scenario);
    }

    public void AfterScenario(ScreenplayScenario scenario, bool success)
    {
      AfterScenario(scenario);
      MarkAsEnded(scenario, success);
    }

    public void AfterExecutedLastScenario()
    {
      NotifySubscribersOfTestRunCompletion();
      AfterExecutedLastScenario(CreateSingletonResolver());
    }

    protected virtual void AfterExecutedLastScenario(IServiceResolver serviceResolver)
    {
      // Intentional no-op for subclasses to implement
    }

    protected abstract IServiceRegistrationProvider GetRegistrationProvider();

    protected virtual void CustomiseScenario(ScreenplayScenario scenario)
    {
      // Intentional no-op for subclasses to implement
    }

    protected virtual void AfterScenario(ScreenplayScenario scenario)
    {
      // Intentional no-op for subclasses to implement
    }

    public IScenarioFactory GetScenarioFactory() => environment.GetScenarioFactory();

    protected void MarkAsBegun(ScreenplayScenario scenario) => scenario.Begin();

    protected void MarkAsEnded(ScreenplayScenario scenario, bool success) => scenario.End(success);

    protected IServiceResolver CreateSingletonResolver() => environment.CreateSingletonResolver();

    protected void NotifySubscribersOfTestRunStart() => environment.NotifyBeginTestRun();

    protected void NotifySubscribersOfTestRunCompletion() => environment.NotifyCompleteTestRun();

    ServiceRegistry GetServiceRegistry()
    {
      var provider = GetRegistrationProvider();
      return provider.GetServiceRegistry();
    }

    static ScreenplayIntegration()
    {
      environment = new ScreenplayEnvironment();
    }
  }
}

{'fraction_non_alphanumeric': 0.06644901094680238, 'fraction_numerical': 0.0, 'mean_word_length': 3.8, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3626214', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1250, 'n_words': 276}