Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
This commit is contained in:
@@ -8,150 +8,17 @@ using NetDaemon.HassModel.Entities;
|
||||
namespace BatteryMonitor.Manager.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Shared, deterministic builders and datasets used by the BatteryMonitor tests.
|
||||
/// Keeping entity/state construction and generated inputs here means the test
|
||||
/// classes only describe the scenario under test.
|
||||
/// Shared, deterministic builders and datasets used by the BatteryMonitor tests.
|
||||
/// Keeping entity/state construction and generated inputs here means the test
|
||||
/// classes only describe the scenario under test.
|
||||
/// </summary>
|
||||
internal static class BatteryMonitorTestData
|
||||
{
|
||||
private const string DefaultFriendlyName = "Battery Device";
|
||||
|
||||
/// <summary>
|
||||
/// Describes one entity exposed through <see cref="IHaContext.GetAllEntities"/>.
|
||||
/// </summary>
|
||||
public sealed record EntitySpec
|
||||
{
|
||||
public required string EntityId { get; init; }
|
||||
|
||||
public string? State { get; init; } = "50";
|
||||
|
||||
public string? DeviceClass { get; init; } = BatteryMonitorConsts.BatteryDeviceClass;
|
||||
|
||||
public string? FriendlyName { get; init; } = DefaultFriendlyName;
|
||||
|
||||
/// <summary>When false the context returns no <see cref="EntityState"/> for the entity.</summary>
|
||||
public bool HasState { get; init; } = true;
|
||||
}
|
||||
|
||||
/// <summary>A battery device-class sensor.</summary>
|
||||
public static EntitySpec Battery(string entityId, string? state, string? friendlyName = DefaultFriendlyName)
|
||||
{
|
||||
return new EntitySpec
|
||||
{
|
||||
EntityId = entityId,
|
||||
State = state,
|
||||
FriendlyName = friendlyName
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>A sensor with an arbitrary (or missing) device class.</summary>
|
||||
public static EntitySpec Sensor(string entityId, string? deviceClass, string? state, string? friendlyName = DefaultFriendlyName)
|
||||
{
|
||||
return new EntitySpec
|
||||
{
|
||||
EntityId = entityId,
|
||||
DeviceClass = deviceClass,
|
||||
State = state,
|
||||
FriendlyName = friendlyName
|
||||
};
|
||||
}
|
||||
|
||||
public static EntityState CreateEntityState(EntitySpec spec)
|
||||
{
|
||||
var attributes = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
|
||||
if (spec.DeviceClass is not null)
|
||||
{
|
||||
attributes["device_class"] = spec.DeviceClass;
|
||||
}
|
||||
|
||||
if (spec.FriendlyName is not null)
|
||||
{
|
||||
attributes["friendly_name"] = spec.FriendlyName;
|
||||
}
|
||||
|
||||
return new EntityState
|
||||
{
|
||||
State = spec.State,
|
||||
AttributesJson = JsonSerializer.SerializeToElement(attributes)
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wires <see cref="IHaContext.GetState"/> and <see cref="IHaContext.GetAllEntities"/>
|
||||
/// on the mock, exposing the entities in declaration order.
|
||||
/// </summary>
|
||||
public static void ConfigureHaContext(Mock<IHaContext> haContextMock, params EntitySpec[] entities)
|
||||
{
|
||||
var entityList = new List<Entity>(entities.Length);
|
||||
|
||||
foreach (var spec in entities)
|
||||
{
|
||||
if (spec.HasState)
|
||||
{
|
||||
haContextMock.Setup(h => h.GetState(spec.EntityId)).Returns(CreateEntityState(spec));
|
||||
}
|
||||
|
||||
entityList.Add(new Entity(haContextMock.Object, spec.EntityId));
|
||||
}
|
||||
|
||||
haContextMock.Setup(h => h.GetAllEntities()).Returns(entityList);
|
||||
}
|
||||
|
||||
public static BatterySensorInfo CreateSensorInfo(string entityId, double batteryLevel, string? friendlyName = null)
|
||||
{
|
||||
return new BatterySensorInfo
|
||||
{
|
||||
EntityId = entityId,
|
||||
FriendlyName = friendlyName ?? entityId,
|
||||
BatteryLevel = batteryLevel
|
||||
};
|
||||
}
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(params (string EntityId, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return [.. sensors.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel))];
|
||||
}
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(params (string EntityId, string FriendlyName, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return [.. sensors.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel, s.FriendlyName))];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates battery sensors with every integer level in <c>0..count-1</c>.
|
||||
/// </summary>
|
||||
public static EntitySpec[] CreateGeneratedBatteryEntities(int count)
|
||||
{
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Battery(
|
||||
$"sensor.generated_battery_{i:D3}",
|
||||
i.ToString(CultureInfo.InvariantCulture),
|
||||
$"Generated Battery {i:D3}"))
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates notification devices with deterministic, non-monotonic levels so
|
||||
/// ordering behaviour is exercised over more than a handful of rows.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<BatterySensorInfo> CreateGeneratedSensorInfos(int count)
|
||||
{
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => CreateSensorInfo(
|
||||
$"sensor.generated_{i:D3}",
|
||||
i * 37 % 101,
|
||||
$"Generated Device {i:D3}"))
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// (threshold, state, expected) rows straddling the strict <c>level < threshold</c>
|
||||
/// boundary, including zero/negative thresholds and int min/max thresholds.
|
||||
/// (threshold, state, expected) rows straddling the strict <c>level < threshold</c>
|
||||
/// boundary, including zero/negative thresholds and int min/max thresholds.
|
||||
/// </summary>
|
||||
public static TheoryData<int, string, bool> ThresholdBoundaryRows { get; } = new()
|
||||
{
|
||||
@@ -183,8 +50,8 @@ internal static class BatteryMonitorTestData
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// States that must never produce a <see cref="BatterySensorInfo"/>, including
|
||||
/// whitespace/malformed strings and a null state.
|
||||
/// States that must never produce a <see cref="BatterySensorInfo" />, including
|
||||
/// whitespace/malformed strings and a null state.
|
||||
/// </summary>
|
||||
public static TheoryData<string?> UnusableStateRows { get; } =
|
||||
[
|
||||
@@ -203,9 +70,121 @@ internal static class BatteryMonitorTestData
|
||||
null
|
||||
];
|
||||
|
||||
/// <summary>Device counts used to exercise notification payload generation.</summary>
|
||||
public static TheoryData<int> NotifyDeviceCounts { get; } = [1, 2, 3, 5, 10, 50];
|
||||
|
||||
/// <summary>A battery device-class sensor.</summary>
|
||||
public static EntitySpec Battery(string entityId, string? state, string? friendlyName = DefaultFriendlyName)
|
||||
{
|
||||
return new EntitySpec
|
||||
{
|
||||
EntityId = entityId,
|
||||
State = state,
|
||||
FriendlyName = friendlyName
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>A sensor with an arbitrary (or missing) device class.</summary>
|
||||
public static EntitySpec Sensor(string entityId, string? deviceClass, string? state,
|
||||
string? friendlyName = DefaultFriendlyName)
|
||||
{
|
||||
return new EntitySpec
|
||||
{
|
||||
EntityId = entityId,
|
||||
DeviceClass = deviceClass,
|
||||
State = state,
|
||||
FriendlyName = friendlyName
|
||||
};
|
||||
}
|
||||
|
||||
public static EntityState CreateEntityState(EntitySpec spec)
|
||||
{
|
||||
var attributes = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
|
||||
if (spec.DeviceClass is not null) attributes["device_class"] = spec.DeviceClass;
|
||||
|
||||
if (spec.FriendlyName is not null) attributes["friendly_name"] = spec.FriendlyName;
|
||||
|
||||
return new EntityState
|
||||
{
|
||||
State = spec.State,
|
||||
AttributesJson = JsonSerializer.SerializeToElement(attributes)
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cross-product of every excluded keyword (lower and upper case) plus
|
||||
/// non-excluded look-alikes that must not be filtered.
|
||||
/// Wires <see cref="IHaContext.GetState" /> and <see cref="IHaContext.GetAllEntities" />
|
||||
/// on the mock, exposing the entities in declaration order.
|
||||
/// </summary>
|
||||
public static void ConfigureHaContext(Mock<IHaContext> haContextMock, params EntitySpec[] entities)
|
||||
{
|
||||
var entityList = new List<Entity>(entities.Length);
|
||||
|
||||
foreach (var spec in entities)
|
||||
{
|
||||
if (spec.HasState) haContextMock.Setup(h => h.GetState(spec.EntityId)).Returns(CreateEntityState(spec));
|
||||
|
||||
entityList.Add(new Entity(haContextMock.Object, spec.EntityId));
|
||||
}
|
||||
|
||||
haContextMock.Setup(h => h.GetAllEntities()).Returns(entityList);
|
||||
}
|
||||
|
||||
public static BatterySensorInfo CreateSensorInfo(string entityId, double batteryLevel, string? friendlyName = null)
|
||||
{
|
||||
return new BatterySensorInfo
|
||||
{
|
||||
EntityId = entityId,
|
||||
FriendlyName = friendlyName ?? entityId,
|
||||
BatteryLevel = batteryLevel
|
||||
};
|
||||
}
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(params (string EntityId, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return [.. sensors.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel))];
|
||||
}
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(
|
||||
params (string EntityId, string FriendlyName, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return [.. sensors.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel, s.FriendlyName))];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates battery sensors with every integer level in <c>0..count-1</c>.
|
||||
/// </summary>
|
||||
public static EntitySpec[] CreateGeneratedBatteryEntities(int count)
|
||||
{
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Battery(
|
||||
$"sensor.generated_battery_{i:D3}",
|
||||
i.ToString(CultureInfo.InvariantCulture),
|
||||
$"Generated Battery {i:D3}"))
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates notification devices with deterministic, non-monotonic levels so
|
||||
/// ordering behaviour is exercised over more than a handful of rows.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<BatterySensorInfo> CreateGeneratedSensorInfos(int count)
|
||||
{
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => CreateSensorInfo(
|
||||
$"sensor.generated_{i:D3}",
|
||||
i * 37 % 101,
|
||||
$"Generated Device {i:D3}"))
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cross-product of every excluded keyword (lower and upper case) plus
|
||||
/// non-excluded look-alikes that must not be filtered.
|
||||
/// </summary>
|
||||
public static TheoryData<string, bool> ExcludedEntityIdRows()
|
||||
{
|
||||
@@ -229,6 +208,20 @@ internal static class BatteryMonitorTestData
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>Device counts used to exercise notification payload generation.</summary>
|
||||
public static TheoryData<int> NotifyDeviceCounts { get; } = [1, 2, 3, 5, 10, 50];
|
||||
}
|
||||
/// <summary>
|
||||
/// Describes one entity exposed through <see cref="IHaContext.GetAllEntities" />.
|
||||
/// </summary>
|
||||
public sealed record EntitySpec
|
||||
{
|
||||
public required string EntityId { get; init; }
|
||||
|
||||
public string? State { get; init; } = "50";
|
||||
|
||||
public string? DeviceClass { get; init; } = BatteryMonitorConsts.BatteryDeviceClass;
|
||||
|
||||
public string? FriendlyName { get; init; } = DefaultFriendlyName;
|
||||
|
||||
/// <summary>When false the context returns no <see cref="EntityState" /> for the entity.</summary>
|
||||
public bool HasState { get; init; } = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user