Convert test data sources to typed TheoryData
Replace untyped MemberData sources with TheoryData<...>, use collection expressions for array and list literals, drop redundant casts and null suppressions, and give log-assertion helpers an IsEnabled setup so guarded log calls stay verifiable.
This commit is contained in:
@@ -14,7 +14,7 @@ namespace BatteryMonitor.Manager.Tests;
|
||||
/// </summary>
|
||||
internal static class BatteryMonitorTestData
|
||||
{
|
||||
public const string DefaultFriendlyName = "Battery Device";
|
||||
private const string DefaultFriendlyName = "Battery Device";
|
||||
|
||||
/// <summary>
|
||||
/// Describes one entity exposed through <see cref="IHaContext.GetAllEntities"/>.
|
||||
@@ -79,9 +79,9 @@ internal static class BatteryMonitorTestData
|
||||
|
||||
/// <summary>
|
||||
/// Wires <see cref="IHaContext.GetState"/> and <see cref="IHaContext.GetAllEntities"/>
|
||||
/// on the mock and returns the created entities in declaration order.
|
||||
/// on the mock, exposing the entities in declaration order.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<Entity> ConfigureHaContext(Mock<IHaContext> haContextMock, params EntitySpec[] entities)
|
||||
public static void ConfigureHaContext(Mock<IHaContext> haContextMock, params EntitySpec[] entities)
|
||||
{
|
||||
var entityList = new List<Entity>(entities.Length);
|
||||
|
||||
@@ -96,8 +96,6 @@ internal static class BatteryMonitorTestData
|
||||
}
|
||||
|
||||
haContextMock.Setup(h => h.GetAllEntities()).Returns(entityList);
|
||||
|
||||
return entityList;
|
||||
}
|
||||
|
||||
public static BatterySensorInfo CreateSensorInfo(string entityId, double batteryLevel, string? friendlyName = null)
|
||||
@@ -112,16 +110,12 @@ internal static class BatteryMonitorTestData
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(params (string EntityId, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return sensors
|
||||
.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel))
|
||||
.ToList();
|
||||
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))
|
||||
.ToList();
|
||||
return [.. sensors.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel, s.FriendlyName))];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -129,12 +123,14 @@ internal static class BatteryMonitorTestData
|
||||
/// </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}"))
|
||||
.ToArray();
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Battery(
|
||||
$"sensor.generated_battery_{i:D3}",
|
||||
i.ToString(CultureInfo.InvariantCulture),
|
||||
$"Generated Battery {i:D3}"))
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -143,12 +139,14 @@ internal static class BatteryMonitorTestData
|
||||
/// </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}"))
|
||||
.ToList();
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => CreateSensorInfo(
|
||||
$"sensor.generated_{i:D3}",
|
||||
i * 37 % 101,
|
||||
$"Generated Device {i:D3}"))
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -188,8 +186,8 @@ internal static class BatteryMonitorTestData
|
||||
/// States that must never produce a <see cref="BatterySensorInfo"/>, including
|
||||
/// whitespace/malformed strings and a null state.
|
||||
/// </summary>
|
||||
public static TheoryData<string?> UnusableStateRows { get; } = new()
|
||||
{
|
||||
public static TheoryData<string?> UnusableStateRows { get; } =
|
||||
[
|
||||
"unavailable",
|
||||
"unknown",
|
||||
"UNAVAILABLE",
|
||||
@@ -202,8 +200,8 @@ internal static class BatteryMonitorTestData
|
||||
"--1",
|
||||
"low",
|
||||
"1 2",
|
||||
(string?)null
|
||||
};
|
||||
null
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Cross-product of every excluded keyword (lower and upper case) plus
|
||||
@@ -232,13 +230,5 @@ internal static class BatteryMonitorTestData
|
||||
}
|
||||
|
||||
/// <summary>Device counts used to exercise notification payload generation.</summary>
|
||||
public static TheoryData<int> NotifyDeviceCounts { get; } = new()
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
10,
|
||||
50
|
||||
};
|
||||
public static TheoryData<int> NotifyDeviceCounts { get; } = [1, 2, 3, 5, 10, 50];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user