Skip to content

Commit

Permalink
#225 Obsoleted IFeatureProgressNotifier / IScenarioProgressNotifier a…
Browse files Browse the repository at this point in the history
…nd related types

Updated all code to use IProgressNotifier implementations instead
  • Loading branch information
Suremaker committed Feb 27, 2021
1 parent a1d67b8 commit 3fc3468
Show file tree
Hide file tree
Showing 53 changed files with 864 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@ namespace LightBDD.Core.ExecutionContext.Implementation
{
internal class CurrentScenarioProperty : IContextProperty
{
private IScenario _scenario;
private object _fixture;

public IScenario Scenario
{
get => _scenario ?? throw new InvalidOperationException("The current task does not run any initialized scenario. Ensure that feature is used within task running fully initialized scenario.");
set => _scenario = value;
}

public object Fixture
{
get => _fixture ?? throw new InvalidOperationException("The current task does not run any scenario with available fixture object.");
set => _fixture = value;
}
public IScenario Scenario { get; set; }
public object Fixture { get; set; }
}
}
19 changes: 17 additions & 2 deletions src/LightBDD.Core/ExecutionContext/ScenarioExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,27 @@ public static ScenarioExecutionContext Current
/// Returns currently executed scenario.
/// <exception cref="InvalidOperationException">Thrown if no scenario is executed by current task or if scenario initialization is not complete.</exception>
/// </summary>
public static IScenario CurrentScenario => Current.Get<CurrentScenarioProperty>().Scenario;
public static IScenario CurrentScenario => Current.Get<CurrentScenarioProperty>().Scenario ?? throw new InvalidOperationException("The current task does not run any initialized scenario. Ensure that feature is used within task running fully initialized scenario.");

/// <summary>
/// Returns currently executed scenario fixture object.<br/>
/// <exception cref="InvalidOperationException">Thrown if no scenario is executed by current task.</exception>
/// </summary>
public static object CurrentScenarioFixture => Current.Get<CurrentScenarioProperty>().Fixture;
public static object CurrentScenarioFixture => Current.Get<CurrentScenarioProperty>().Fixture ?? throw new InvalidOperationException("The current task does not run any scenario with available fixture object.");

/// <summary>
/// Returns currently executed scenario fixture object if present or <c>null</c> if no scenario is currently executed.<br/>
/// <exception cref="InvalidOperationException">Thrown if fixture object is present but not assignable to <typeparam name="TFixture"></typeparam>.</exception>
/// </summary>
public static TFixture GetScenarioFixtureIfPresent<TFixture>() where TFixture : class
{
var fixture = CurrentContext.Value?.Get<CurrentScenarioProperty>().Fixture;

if (fixture == null)
return null;

return fixture as TFixture
?? throw new InvalidOperationException($"Expected fixture of type '{typeof(TFixture)}' while got '{fixture.GetType()}'.");
}
}
}
4 changes: 4 additions & 0 deletions src/LightBDD.Core/Extensibility/IntegrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ public abstract class IntegrationContext
/// <summary>
/// Returns feature progress notifier.
/// </summary>
[Obsolete]
public abstract IFeatureProgressNotifier FeatureProgressNotifier { get; }

/// <summary>
/// Returns scenario progress notifier provider method.
/// </summary>
[Obsolete]
public abstract Func<object, IScenarioProgressNotifier> ScenarioProgressNotifierProvider { get; }

/// <summary>
Expand Down Expand Up @@ -72,6 +74,8 @@ public abstract class IntegrationContext
/// <summary>
/// Creates progress notifier.
/// </summary>
#pragma warning disable 612
protected virtual IProgressNotifier GetProgressNotifier() => new NotificationAdapter(FeatureProgressNotifier, ScenarioProgressNotifierProvider);
#pragma warning restore 612
}
}
4 changes: 3 additions & 1 deletion src/LightBDD.Core/Notification/IFeatureProgressNotifier.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using LightBDD.Core.Metadata;
using System;
using LightBDD.Core.Metadata;
using LightBDD.Core.Results;

namespace LightBDD.Core.Notification
{
/// <summary>
/// Feature progress notification interface.
/// </summary>
[Obsolete("Use " + nameof(IProgressNotifier) + " instead")]
public interface IFeatureProgressNotifier
{
/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/LightBDD.Core/Notification/IScenarioProgressNotifier.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using LightBDD.Core.Metadata;
using System;
using LightBDD.Core.Metadata;
using LightBDD.Core.Results;

namespace LightBDD.Core.Notification
{
/// <summary>
/// Scenario progress notification interface.
/// </summary>
[Obsolete("Use " + nameof(IProgressNotifier) + " instead")]
public interface IScenarioProgressNotifier
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace LightBDD.Core.Notification.Implementation
{
//TODO: remove in LightBDD 4.x
[Obsolete]
class NotificationAdapter : IProgressNotifier
{
private readonly IFeatureProgressNotifier _featureProgressNotifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LightBDD.Core.Formatting.ExceptionFormatting;
using System;
using LightBDD.Core.Formatting.ExceptionFormatting;
using LightBDD.Fixie2.Implementation;
using LightBDD.Framework.Configuration;

Expand All @@ -22,6 +23,7 @@ public static DefaultExceptionFormatter WithTestFrameworkDefaults(this DefaultEx
/// <summary>
/// Appends LightBDD.Fixie2 default scenario progress notifiers.
/// </summary>
[Obsolete("Use " + nameof(ProgressNotifierConfiguration) + " instead", true)]
public static ScenarioProgressNotifierConfiguration AppendFrameworkDefaultProgressNotifiers(this ScenarioProgressNotifierConfiguration configuration)
{
return configuration
Expand All @@ -31,9 +33,18 @@ public static ScenarioProgressNotifierConfiguration AppendFrameworkDefaultProgre
/// <summary>
/// Appends LightBDD.Fixie2 default feature progress notifiers.
/// </summary>
[Obsolete("Use " + nameof(ProgressNotifierConfiguration) + " instead", true)]
public static FeatureProgressNotifierConfiguration AppendFrameworkDefaultProgressNotifiers(this FeatureProgressNotifierConfiguration configuration)
{
return configuration.AppendNotifiers(FixieProgressNotifier.CreateFeatureProgressNotifier());
}

/// <summary>
/// Appends LightBDD.MsTest2 default progress notifiers.
/// </summary>
public static ProgressNotifierConfiguration AppendFrameworkDefaultProgressNotifiers(this ProgressNotifierConfiguration configuration)
{
return configuration.Append(FixieProgressNotifier.CreateProgressNotifier());
}
}
}
7 changes: 7 additions & 0 deletions src/LightBDD.Fixie2/Implementation/FixieProgressNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ namespace LightBDD.Fixie2.Implementation
{
internal class FixieProgressNotifier
{
[Obsolete]
public static IFeatureProgressNotifier CreateFeatureProgressNotifier()
{
return ParallelProgressNotifierProvider.Default.CreateFeatureProgressNotifier(Console.WriteLine);
}

[Obsolete]
public static IScenarioProgressNotifier CreateImmediateScenarioProgressNotifier()
{
return ParallelProgressNotifierProvider.Default.CreateScenarioProgressNotifier(Console.WriteLine);
}

public static IProgressNotifier CreateProgressNotifier()
{
return ParallelProgressNotifierProvider.Default.CreateProgressNotifier(Console.WriteLine);
}
}
}
5 changes: 1 addition & 4 deletions src/LightBDD.Fixie2/LightBddScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ private LightBddConfiguration Configure()
{
var configuration = new LightBddConfiguration().WithFrameworkDefaults();

configuration.Get<FeatureProgressNotifierConfiguration>()
.AppendFrameworkDefaultProgressNotifiers();

configuration.Get<ScenarioProgressNotifierConfiguration>()
configuration.ProgressNotifierConfiguration()
.AppendFrameworkDefaultProgressNotifiers();

configuration.ExceptionHandlingConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace LightBDD.Framework.Configuration
/// <summary>
/// Configuration class allowing to customize feature progress notification behavior.
/// </summary>
[Obsolete]
public class FeatureProgressNotifierConfiguration : FeatureConfiguration
{
/// <summary>
Expand All @@ -23,6 +24,7 @@ public class FeatureProgressNotifierConfiguration : FeatureConfiguration
/// <param name="notifier">New notifier to set.</param>
/// <returns>Self.</returns>
/// <exception cref="ArgumentNullException">Throws when <paramref name="notifier"/> is null.</exception>
[Obsolete("Use " + nameof(ProgressNotifierConfiguration) + " methods instead")]
public FeatureProgressNotifierConfiguration UpdateNotifier(IFeatureProgressNotifier notifier)
{
ThrowIfSealed();
Expand All @@ -36,6 +38,7 @@ public FeatureProgressNotifierConfiguration UpdateNotifier(IFeatureProgressNotif
/// <param name="notifiers">Notifiers to append</param>
/// <returns>Self</returns>
/// <exception cref="ArgumentNullException">Throws when <paramref name="notifiers"/> collection or any of it's item is null.</exception>
[Obsolete("Use " + nameof(ProgressNotifierConfiguration) + "." + nameof(ProgressNotifierConfiguration.Append) + " instead")]
public FeatureProgressNotifierConfiguration AppendNotifiers(params IFeatureProgressNotifier[] notifiers)
{
ThrowIfSealed();
Expand All @@ -49,6 +52,8 @@ public FeatureProgressNotifierConfiguration AppendNotifiers(params IFeatureProgr
/// Sets <see cref="Notifier"/> to <see cref="NoProgressNotifier.Default"/> instance that does not report any notifications.
/// </summary>
/// <returns>Self.</returns>
#pragma warning disable 618
public FeatureProgressNotifierConfiguration ClearNotifiers() => UpdateNotifier(NoProgressNotifier.Default);
#pragma warning restore 618
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using LightBDD.Core.Configuration;
using LightBDD.Framework.Formatting;
using LightBDD.Framework.Formatting.Values;
Expand Down Expand Up @@ -59,6 +60,7 @@ public static ReportWritersConfiguration RegisterFrameworkDefaultReportWriters(t
/// </summary>
/// <param name="configuration">Configuration object.</param>
/// <returns>Configuration object.</returns>
[Obsolete]
public static FeatureProgressNotifierConfiguration FeatureProgressNotifierConfiguration(this LightBddConfiguration configuration)
{
return configuration.Get<FeatureProgressNotifierConfiguration>();
Expand All @@ -69,6 +71,7 @@ public static FeatureProgressNotifierConfiguration FeatureProgressNotifierConfig
/// </summary>
/// <param name="configuration">Configuration object.</param>
/// <returns>Configuration object.</returns>
[Obsolete]
public static ScenarioProgressNotifierConfiguration ScenarioProgressNotifierConfiguration(this LightBddConfiguration configuration)
{
return configuration.Get<ScenarioProgressNotifierConfiguration>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace LightBDD.Framework.Configuration
/// <summary>
/// Configuration class allowing to customize scenario progress notification behavior.
/// </summary>
[Obsolete]
public class ScenarioProgressNotifierConfiguration : FeatureConfiguration
{
private readonly ScenarioProgressNotifierComposer _composer = new ScenarioProgressNotifierComposer();
Expand All @@ -27,6 +28,7 @@ public class ScenarioProgressNotifierConfiguration : FeatureConfiguration
/// <param name="notifierProvider">New provider to set.</param>
/// <returns>Self.</returns>
/// <exception cref="ArgumentNullException">Throws when <paramref name="notifierProvider"/> is null.</exception>
[Obsolete("Use " + nameof(ProgressNotifierConfiguration) + "." + nameof(ProgressNotifierConfiguration.Append) + " instead")]
public ScenarioProgressNotifierConfiguration UpdateNotifierProvider(Func<IScenarioProgressNotifier> notifierProvider)
{
ThrowIfSealed();
Expand All @@ -43,6 +45,7 @@ public ScenarioProgressNotifierConfiguration UpdateNotifierProvider(Func<IScenar
/// <typeparam name="TFixture">Feature fixture type.</typeparam>
/// <returns>Self.</returns>
/// <exception cref="ArgumentNullException">Throws when <paramref name="notifierProvider"/> is null.</exception>
[Obsolete("Use " + nameof(ProgressNotifierConfiguration) + "." + nameof(ProgressNotifierConfiguration.Append) + " instead")]
public ScenarioProgressNotifierConfiguration UpdateNotifierProvider<TFixture>(Func<TFixture, IScenarioProgressNotifier> notifierProvider)
{
ThrowIfSealed();
Expand All @@ -58,6 +61,7 @@ public ScenarioProgressNotifierConfiguration UpdateNotifierProvider<TFixture>(Fu
/// <param name="notifierProviders">Notifiers to append</param>
/// <returns>Self</returns>
/// <exception cref="ArgumentNullException">Throws when <paramref name="notifierProviders"/> collection or any of it's item is null.</exception>
[Obsolete("Use " + nameof(ProgressNotifierConfiguration) + "." + nameof(ProgressNotifierConfiguration.Append) + " instead")]
public ScenarioProgressNotifierConfiguration AppendNotifierProviders(params Func<IScenarioProgressNotifier>[] notifierProviders)
{
ThrowIfSealed();
Expand All @@ -75,6 +79,7 @@ public ScenarioProgressNotifierConfiguration AppendNotifierProviders(params Func
/// <param name="notifierProviders">Notifiers to append</param>
/// <returns>Self</returns>
/// <exception cref="ArgumentNullException">Throws when <paramref name="notifierProviders"/> collection or any of it's item is null.</exception>
[Obsolete("Use " + nameof(ProgressNotifierConfiguration) + "." + nameof(ProgressNotifierConfiguration.Append) + " instead")]
public ScenarioProgressNotifierConfiguration AppendNotifierProviders<TFixture>(params Func<TFixture, IScenarioProgressNotifier>[] notifierProviders)
{
ThrowIfSealed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using LightBDD.Framework.Configuration;
using LightBDD.Framework.Notification;
using LightBDD.Framework.Notification.Implementation;
#pragma warning disable 612

namespace LightBDD.Framework.Extensibility
{
Expand All @@ -25,9 +26,11 @@ public class DefaultIntegrationContext : IntegrationContext
public override Func<Exception, ExecutionStatus> ExceptionToStatusMapper { get; }

/// <inheritdoc />
[Obsolete]
public override IFeatureProgressNotifier FeatureProgressNotifier { get; }

/// <inheritdoc />
[Obsolete]
public override Func<object, IScenarioProgressNotifier> ScenarioProgressNotifierProvider { get; }

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
using LightBDD.Core.Results;
using LightBDD.Core.Results.Parameters.Tabular;
using LightBDD.Framework.Reporting.Formatters;
#pragma warning disable 618

namespace LightBDD.Framework.Notification
{
/// <summary>
/// The default implementation of <see cref="IScenarioProgressNotifier"/> and <see cref="IFeatureProgressNotifier"/> which renders the notification text and delegates to provided notification actions configured in constructor.
/// </summary>
public class DefaultProgressNotifier : IScenarioProgressNotifier, IFeatureProgressNotifier, IProgressNotifier
public class DefaultProgressNotifier : IProgressNotifier, IScenarioProgressNotifier, IFeatureProgressNotifier
{
private readonly Action<string> _onNotify;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace LightBDD.Framework.Notification
/// <summary>
/// Feature progress notifier allowing to delegate notification to zero or more notifiers.
/// </summary>
[Obsolete]
public class DelegatingFeatureProgressNotifier : IFeatureProgressNotifier
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace LightBDD.Framework.Notification
/// <summary>
/// Scenario progress notifier allowing to delegate notification to zero or more notifiers.
/// </summary>
[Obsolete]
public class DelegatingScenarioProgressNotifier : IScenarioProgressNotifier
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using LightBDD.Core.Notification;
using System;
using LightBDD.Core.Notification;

namespace LightBDD.Framework.Notification.Implementation
{
[Obsolete]
internal interface IScenarioProgressNotifierProvider
{
IScenarioProgressNotifier Provide(object fixture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace LightBDD.Framework.Notification.Implementation
{
//TODO: remove in LightBDD 4.x
[Obsolete]
class NotificationAdapter : IProgressNotifier
{
private readonly IFeatureProgressNotifier _featureProgressNotifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Linq;
using System.Threading;
using LightBDD.Core.Metadata;
using LightBDD.Core.Notification;
using LightBDD.Core.Notification.Events;
using LightBDD.Core.Results;
#pragma warning disable 618

namespace LightBDD.Framework.Notification.Implementation
{
internal class ParallelProgressNotifier : IScenarioProgressNotifier, IFeatureProgressNotifier, IProgressNotifier
{
private readonly ProgressManager _manager;
private int? _currentScenarioNumber;
private readonly DefaultProgressNotifier _notifier;
private readonly Action<string> _onNotify;

Expand All @@ -27,7 +28,7 @@ public ParallelProgressNotifier(ProgressManager manager, Action<string>[] onNoti
private void NotifyProgress(string message)
{
var progress = _manager.GetProgress();
var header = $"Fi={progress.FinishedScenarios:D3},Fa={progress.FailedScenarios:D3},Pe={progress.PendingScenarios:D3} #{_currentScenarioNumber,3}> ";
var header = $"Fi={progress.FinishedScenarios:D3},Fa={progress.FailedScenarios:D3},Pe={progress.PendingScenarios:D3} #{progress.CurrentScenarioNumber,3}> ";
_onNotify(header + message.Replace(Environment.NewLine, Environment.NewLine + new string(' ', header.Length)));
}

Expand All @@ -43,7 +44,7 @@ public void NotifyFeatureFinished(IFeatureResult feature)

public void NotifyScenarioStart(IScenarioInfo scenario)
{
_currentScenarioNumber = _manager.StartNewScenario();
_manager.StartNewScenario();
_notifier.NotifyScenarioStart(scenario);
}

Expand Down
Loading

0 comments on commit 3fc3468

Please sign in to comment.