-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#225 Implemented ProgressNotifierConfiguration
- Loading branch information
Showing
12 changed files
with
271 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/LightBDD.Framework/Configuration/ProgressNotifierConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Linq; | ||
using LightBDD.Core.Configuration; | ||
using LightBDD.Core.Notification; | ||
using LightBDD.Framework.Notification; | ||
using LightBDD.Framework.Notification.Implementation; | ||
|
||
namespace LightBDD.Framework.Configuration | ||
{ | ||
/// <summary> | ||
/// Configuration class allowing to customize scenario progress notification behavior. | ||
/// </summary> | ||
public class ProgressNotifierConfiguration : FeatureConfiguration | ||
{ | ||
/// <summary> | ||
/// Returns progress notifier.<br/> | ||
/// By default it is configured to not report any notifications. | ||
/// </summary> | ||
public IProgressNotifier Notifier { get; private set; } = NoProgressNotifier.Default; | ||
|
||
/// <summary> | ||
/// Appends <paramref name="notifiers"/> to existing <see cref="Notifier"/> making all of them used during notification. | ||
/// </summary> | ||
/// <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> | ||
public ProgressNotifierConfiguration Append(params IProgressNotifier[] notifiers) | ||
{ | ||
ThrowIfSealed(); | ||
if (notifiers == null) | ||
throw new ArgumentNullException(nameof(notifiers)); | ||
Notifier = DelegatingProgressNotifier.Compose(Enumerable.Repeat(Notifier, 1).Concat(notifiers)); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Clears <see cref="Notifier"/> to use instance that does not report any notifications. | ||
/// </summary> | ||
/// <returns>Self.</returns> | ||
public ProgressNotifierConfiguration Clear() | ||
{ | ||
ThrowIfSealed(); | ||
Notifier = NoProgressNotifier.Default; | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/LightBDD.Framework/Notification/Implementation/DelegatingProgressNotifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using LightBDD.Core.Notification; | ||
using LightBDD.Core.Notification.Events; | ||
|
||
namespace LightBDD.Framework.Notification.Implementation | ||
{ | ||
class DelegatingProgressNotifier : IProgressNotifier | ||
{ | ||
private readonly IList<IProgressNotifier> _notifiers; | ||
private DelegatingProgressNotifier(IList<IProgressNotifier> notifiers) | ||
{ | ||
_notifiers = notifiers; | ||
} | ||
|
||
public void Notify(ProgressEvent e) | ||
{ | ||
foreach (var notifier in _notifiers) | ||
notifier.Notify(e); | ||
} | ||
|
||
public static IProgressNotifier Compose(params IProgressNotifier[] notifiers) => Compose(notifiers.AsEnumerable()); | ||
|
||
public static IProgressNotifier Compose(IEnumerable<IProgressNotifier> notifiers) | ||
{ | ||
var result = Flatten(notifiers, new List<IProgressNotifier>()); | ||
if (result.Count > 1) | ||
return new DelegatingProgressNotifier(result); | ||
|
||
return result.FirstOrDefault() ?? NoProgressNotifier.Default; | ||
} | ||
|
||
private static List<IProgressNotifier> Flatten(IEnumerable<IProgressNotifier> notifiers, List<IProgressNotifier> results) | ||
{ | ||
foreach (var notifier in notifiers ?? Enumerable.Empty<IProgressNotifier>()) | ||
{ | ||
if (notifier == null || notifier is NoProgressNotifier) | ||
continue; | ||
if (notifier is DelegatingProgressNotifier dp) | ||
Flatten(dp._notifiers, results); | ||
else | ||
results.Add(notifier); | ||
} | ||
|
||
return results; | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/LightBDD.Framework/Notification/Implementation/NotificationAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Threading; | ||
using LightBDD.Core.ExecutionContext; | ||
using LightBDD.Core.Notification; | ||
using LightBDD.Core.Notification.Events; | ||
|
||
namespace LightBDD.Framework.Notification.Implementation | ||
{ | ||
//TODO: remove in LightBDD 4.x | ||
class NotificationAdapter : IProgressNotifier | ||
{ | ||
private readonly IFeatureProgressNotifier _featureProgressNotifier; | ||
private readonly Func<object, IScenarioProgressNotifier> _scenarioProgressNotifierProvider; | ||
private readonly AsyncLocal<IScenarioProgressNotifier> _scenarioNotifier = new AsyncLocal<IScenarioProgressNotifier>(); | ||
|
||
public NotificationAdapter(IFeatureProgressNotifier featureProgressNotifier, Func<object, IScenarioProgressNotifier> scenarioProgressNotifierProvider) | ||
{ | ||
_featureProgressNotifier = featureProgressNotifier; | ||
_scenarioProgressNotifierProvider = scenarioProgressNotifierProvider; | ||
} | ||
|
||
public void Notify(ProgressEvent e) | ||
{ | ||
switch (e) | ||
{ | ||
case FeatureStarting fs: | ||
_featureProgressNotifier.NotifyFeatureStart(fs.Feature); | ||
break; | ||
case FeatureFinished ff: | ||
_featureProgressNotifier.NotifyFeatureFinished(ff.Result); | ||
break; | ||
case ScenarioStarting ss: | ||
HandleScenarioStarted(ss); | ||
break; | ||
case ScenarioFinished sf: | ||
HandleScenarioFinished(sf); | ||
break; | ||
case StepStarting sts: | ||
_scenarioNotifier.Value.NotifyStepStart(sts.Step); | ||
break; | ||
case StepFinished stf: | ||
_scenarioNotifier.Value.NotifyStepFinished(stf.Result); | ||
break; | ||
case StepCommented sc: | ||
_scenarioNotifier.Value.NotifyStepComment(sc.Step, sc.Comment); | ||
break; | ||
} | ||
} | ||
|
||
private void HandleScenarioStarted(ScenarioStarting scenarioStarting) | ||
{ | ||
_scenarioNotifier.Value = _scenarioProgressNotifierProvider.Invoke(ScenarioExecutionContext.CurrentScenarioFixture); | ||
_scenarioNotifier.Value.NotifyScenarioStart(scenarioStarting.Scenario); | ||
} | ||
|
||
private void HandleScenarioFinished(ScenarioFinished scenarioFinished) | ||
{ | ||
_scenarioNotifier.Value.NotifyScenarioFinished(scenarioFinished.Result); | ||
_scenarioNotifier.Value = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.