Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using System.Reactive.Subjects;
|
||||
using FluentAssertions;
|
||||
using LightMonitor.Models;
|
||||
@@ -9,6 +11,8 @@ using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace LightMonitor.Manager.Tests;
|
||||
|
||||
public class LightMonitorInitTests
|
||||
@@ -18,30 +22,30 @@ public class LightMonitorInitTests
|
||||
|
||||
private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions();
|
||||
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILightControlService> _lightControlServiceMock = new();
|
||||
private readonly Mock<IPresenceService> _presenceServiceMock = new();
|
||||
private readonly Subject<StateChange> _stateChangesSubject = new();
|
||||
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
|
||||
private readonly Mock<ILightControlService> _lightControlServiceMock = new Mock<ILightControlService>();
|
||||
private readonly Mock<IPresenceService> _presenceServiceMock = new Mock<IPresenceService>();
|
||||
private readonly Subject<StateChange> _stateChangesSubject = new Subject<StateChange>();
|
||||
|
||||
private LightMonitorInit CreateSut(LightMonitorOptions? options = null, Exception? subscriptionException = null)
|
||||
{
|
||||
if (subscriptionException is null)
|
||||
{
|
||||
_haContextMock.Setup(x => x.StateAllChanges())
|
||||
.Returns(_stateChangesSubject);
|
||||
.Returns(_stateChangesSubject);
|
||||
}
|
||||
else
|
||||
{
|
||||
_haContextMock.Setup(x => x.StateAllChanges())
|
||||
.Throws(subscriptionException);
|
||||
.Throws(subscriptionException);
|
||||
}
|
||||
|
||||
return new LightMonitorInit(
|
||||
_haContextMock.Object,
|
||||
Mock.Of<ILogger<LightMonitorInit>>(),
|
||||
_presenceServiceMock.Object,
|
||||
_lightControlServiceMock.Object,
|
||||
Options.Create(options ?? _defaultOptions));
|
||||
_haContextMock.Object,
|
||||
Mock.Of<ILogger<LightMonitorInit>>(),
|
||||
_presenceServiceMock.Object,
|
||||
_lightControlServiceMock.Object,
|
||||
Options.Create(options ?? _defaultOptions));
|
||||
}
|
||||
|
||||
private void EmitStateChange(string entityId, string? oldState, string? newState)
|
||||
@@ -49,9 +53,9 @@ public class LightMonitorInitTests
|
||||
var entity = new Entity(_haContextMock.Object, entityId);
|
||||
|
||||
_stateChangesSubject.OnNext(new StateChange(
|
||||
entity,
|
||||
LightMonitorTestData.CreateOptionalState(oldState),
|
||||
LightMonitorTestData.CreateOptionalState(newState)));
|
||||
entity,
|
||||
LightMonitorTestData.CreateOptionalState(oldState),
|
||||
LightMonitorTestData.CreateOptionalState(newState)));
|
||||
}
|
||||
|
||||
private void VerifyTurnOn(bool shouldTurnOn)
|
||||
@@ -107,9 +111,9 @@ public class LightMonitorInitTests
|
||||
CreateSut(LightMonitorTestData.CreateOptions(personEntityIds: [PersonEntityId]));
|
||||
|
||||
EmitStateChange(
|
||||
LightMonitorTestData.SecondaryPersonEntityId,
|
||||
"not_home",
|
||||
LightMonitorConsts.PersonHomeState);
|
||||
LightMonitorTestData.SecondaryPersonEntityId,
|
||||
"not_home",
|
||||
LightMonitorConsts.PersonHomeState);
|
||||
|
||||
VerifyTurnOn(false);
|
||||
}
|
||||
@@ -180,14 +184,15 @@ public class LightMonitorInitTests
|
||||
public void SunStateChange_WhenPresenceCheckThrows_ShouldKeepListening()
|
||||
{
|
||||
_presenceServiceMock.SetupSequence(x => x.IsAnyoneHome())
|
||||
.Throws(new InvalidOperationException("presence failed"))
|
||||
.Returns(true);
|
||||
.Throws(new InvalidOperationException("presence failed"))
|
||||
.Returns(true);
|
||||
|
||||
CreateSut();
|
||||
|
||||
var act = () => EmitStateChange(
|
||||
SunEntityId,
|
||||
LightMonitorConsts.SunAboveHorizon,
|
||||
LightMonitorConsts.SunBelowHorizon);
|
||||
SunEntityId,
|
||||
LightMonitorConsts.SunAboveHorizon,
|
||||
LightMonitorConsts.SunBelowHorizon);
|
||||
|
||||
act.Should().NotThrow();
|
||||
VerifyTurnOn(false);
|
||||
@@ -200,8 +205,9 @@ public class LightMonitorInitTests
|
||||
public void PersonStateChange_WhenTurnOnLightThrows_ShouldKeepListening()
|
||||
{
|
||||
_lightControlServiceMock.SetupSequence(x => x.TurnOnLight())
|
||||
.Throws(new InvalidOperationException("light failed"))
|
||||
.Pass();
|
||||
.Throws(new InvalidOperationException("light failed"))
|
||||
.Pass();
|
||||
|
||||
CreateSut();
|
||||
|
||||
var act = () => EmitStateChange(PersonEntityId, "not_home", LightMonitorConsts.PersonHomeState);
|
||||
@@ -238,7 +244,7 @@ public class LightMonitorInitTests
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange("sensor.sun_next_dawn", LightMonitorConsts.UnavailableState,
|
||||
LightMonitorConsts.SunBelowHorizon);
|
||||
LightMonitorConsts.SunBelowHorizon);
|
||||
|
||||
VerifyTurnOn(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user