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.Text.Json;
|
||||
using BatteryMonitor.Models;
|
||||
using BatteryMonitorManager;
|
||||
@@ -8,12 +10,14 @@ using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using static BatteryMonitor.Manager.Tests.BatteryMonitorTestData;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BatteryMonitor.Manager.Tests;
|
||||
|
||||
public class BatteryNotificationServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<BatteryNotificationService>> _loggerMock = new();
|
||||
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
|
||||
private readonly Mock<ILogger<BatteryNotificationService>> _loggerMock = new Mock<ILogger<BatteryNotificationService>>();
|
||||
|
||||
private BatteryNotificationService CreateSut()
|
||||
{
|
||||
@@ -23,10 +27,10 @@ public class BatteryNotificationServiceTests
|
||||
private void VerifyCallService(string service, Times times)
|
||||
{
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
service,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), times);
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
service,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), times);
|
||||
}
|
||||
|
||||
#region NotifyLowBattery
|
||||
@@ -38,7 +42,7 @@ public class BatteryNotificationServiceTests
|
||||
var devices = CreateSensorInfos(
|
||||
[
|
||||
.. Enumerable.Range(0, deviceCount)
|
||||
.Select(i => ($"sensor.device_{i:D3}", $"Device {i:D3}", (double)(i % 5)))
|
||||
.Select(i => ($"sensor.device_{i:D3}", $"Device {i:D3}", (double)(i % 5)))
|
||||
]);
|
||||
|
||||
var sut = CreateSut();
|
||||
@@ -54,17 +58,18 @@ public class BatteryNotificationServiceTests
|
||||
public void NotifyLowBattery_WithEmptyList_ShouldNotCallService(bool useArray)
|
||||
{
|
||||
IReadOnlyList<BatterySensorInfo> devices = useArray
|
||||
? Array.Empty<BatterySensorInfo>()
|
||||
: new List<BatterySensorInfo>();
|
||||
? Array.Empty<BatterySensorInfo>()
|
||||
: new List<BatterySensorInfo>();
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
_haContextMock.Verify(h => h.CallService(
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Never);
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()), Times.Never);
|
||||
|
||||
_loggerMock.Invocations.Should().BeEmpty();
|
||||
}
|
||||
|
||||
@@ -72,18 +77,20 @@ public class BatteryNotificationServiceTests
|
||||
public void NotifyLowBattery_ShouldSendTitleNotificationIdAndDeviceSummary()
|
||||
{
|
||||
object? payload = null;
|
||||
|
||||
_haContextMock
|
||||
.Setup(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()))
|
||||
.Callback<string, string, ServiceTarget?, object?>((_, _, _, data) => payload = data);
|
||||
.Setup(h => h.CallService(
|
||||
BatteryMonitorConsts.PersistentNotificationDomain,
|
||||
BatteryMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()))
|
||||
.Callback<string, string, ServiceTarget?, object?>((_, _, _, data) => payload = data);
|
||||
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.NotifyLowBattery(CreateSensorInfos(
|
||||
("sensor.a", "Device A", 4),
|
||||
("sensor.b", "Device B", 1)));
|
||||
("sensor.a", "Device A", 4),
|
||||
("sensor.b", "Device B", 1)));
|
||||
|
||||
payload.Should().NotBeNull();
|
||||
|
||||
@@ -101,16 +108,19 @@ public class BatteryNotificationServiceTests
|
||||
public void NotifyLowBattery_WithDevices_ShouldLogDeviceCount()
|
||||
{
|
||||
_loggerMock.Setup(l => l.IsEnabled(LogLevel.Information)).Returns(true);
|
||||
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.a", "Device A", 2),
|
||||
("sensor.b", "Device B", 3));
|
||||
("sensor.a", "Device A", 2),
|
||||
("sensor.b", "Device B", 3));
|
||||
|
||||
var sut = CreateSut();
|
||||
sut.NotifyLowBattery(devices);
|
||||
|
||||
var logInvocation = _loggerMock.Invocations
|
||||
.Where(invocation => invocation.Method.Name == nameof(ILogger.Log))
|
||||
.Should().ContainSingle().Subject;
|
||||
.Where(invocation => invocation.Method.Name == nameof(ILogger.Log))
|
||||
.Should()
|
||||
.ContainSingle()
|
||||
.Subject;
|
||||
|
||||
logInvocation.Arguments[0].Should().Be(LogLevel.Information);
|
||||
logInvocation.Arguments[2].Should().NotBeNull();
|
||||
@@ -139,7 +149,10 @@ public class BatteryNotificationServiceTests
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
for (var i = 0; i < calls; i++) sut.DismissNotification();
|
||||
for (var i = 0; i < calls; i++)
|
||||
{
|
||||
sut.DismissNotification();
|
||||
}
|
||||
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationDismissService, Times.Exactly(calls));
|
||||
VerifyCallService(BatteryMonitorConsts.PersistentNotificationCreateService, Times.Never());
|
||||
@@ -153,9 +166,9 @@ public class BatteryNotificationServiceTests
|
||||
public void BuildNotificationMessage_ShouldOrderByBatteryLevel()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.a", "Device A", 4),
|
||||
("sensor.b", "Device B", 1),
|
||||
("sensor.c", "Device C", 3));
|
||||
("sensor.a", "Device A", 4),
|
||||
("sensor.b", "Device B", 1),
|
||||
("sensor.c", "Device C", 3));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
@@ -170,9 +183,9 @@ public class BatteryNotificationServiceTests
|
||||
public void BuildNotificationMessage_ShouldKeepInsertionOrderForEqualLevels()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.first", "First Device", 4),
|
||||
("sensor.second", "Second Device", 4),
|
||||
("sensor.third", "Third Device", 1));
|
||||
("sensor.first", "First Device", 4),
|
||||
("sensor.second", "Second Device", 4),
|
||||
("sensor.third", "Third Device", 1));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
@@ -191,8 +204,8 @@ public class BatteryNotificationServiceTests
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
var expectedLines = devices
|
||||
.OrderBy(d => d.BatteryLevel)
|
||||
.Select(d => $"- **{d.FriendlyName}**: {d.BatteryLevel:F0}%");
|
||||
.OrderBy(d => d.BatteryLevel)
|
||||
.Select(d => $"- **{d.FriendlyName}**: {d.BatteryLevel:F0}%");
|
||||
|
||||
var lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
lines.Should().HaveCount(devices.Count + 1);
|
||||
@@ -219,8 +232,9 @@ public class BatteryNotificationServiceTests
|
||||
[InlineData("Full Battery", 100, "- **Full Battery**: 100%")]
|
||||
[InlineData("", 5, "- ****: 5%")]
|
||||
[InlineData(" ", 4, "- ** **: 4%")]
|
||||
public void BuildNotificationMessage_ShouldFormatDeviceLine(string friendlyName, double batteryLevel,
|
||||
string expectedLine)
|
||||
public void BuildNotificationMessage_ShouldFormatDeviceLine(string friendlyName,
|
||||
double batteryLevel,
|
||||
string expectedLine)
|
||||
{
|
||||
var devices = CreateSensorInfos(("sensor.a", friendlyName, batteryLevel));
|
||||
|
||||
@@ -256,8 +270,8 @@ public class BatteryNotificationServiceTests
|
||||
public void BuildNotificationMessage_WithDuplicateDevices_ShouldListEveryDevice()
|
||||
{
|
||||
var devices = CreateSensorInfos(
|
||||
("sensor.duplicate_battery", "Duplicate Battery", 2),
|
||||
("sensor.duplicate_battery", "Duplicate Battery", 2));
|
||||
("sensor.duplicate_battery", "Duplicate Battery", 2),
|
||||
("sensor.duplicate_battery", "Duplicate Battery", 2));
|
||||
|
||||
var message = BatteryNotificationService.BuildNotificationMessage(devices);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user