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:
@@ -20,42 +20,42 @@ public class ExhaustFanControlServiceTests
|
||||
|
||||
private DateTimeOffset Now => _timeProvider.GetUtcNow();
|
||||
|
||||
public static IEnumerable<object[]> ShowerProgressFanStateCases =>
|
||||
[
|
||||
["active while fan is off", ShowerState.Active, "off", true],
|
||||
["active while fan is on", ShowerState.Active, "on", false],
|
||||
["active while fan reports uppercase on", ShowerState.Active, "ON", false],
|
||||
["active while fan is unavailable", ShowerState.Active, "unavailable", true],
|
||||
["active while fan state is unknown", ShowerState.Active, "unknown", true],
|
||||
["active while fan state is empty", ShowerState.Active, "", true],
|
||||
["active while fan state is missing", ShowerState.Active, null!, true],
|
||||
["suspected while fan is off", ShowerState.Suspected, "off", true],
|
||||
["suspected while fan is on", ShowerState.Suspected, "on", false],
|
||||
["idle does not touch the fan", ShowerState.Idle, "off", false],
|
||||
["declining does not turn the fan on", ShowerState.Declining, "off", false],
|
||||
["completed does not turn the fan on", ShowerState.Completed, "off", false]
|
||||
];
|
||||
public static TheoryData<string, ShowerState, string?, bool> ShowerProgressFanStateCases => new()
|
||||
{
|
||||
{ "active while fan is off", ShowerState.Active, "off", true },
|
||||
{ "active while fan is on", ShowerState.Active, "on", false },
|
||||
{ "active while fan reports uppercase on", ShowerState.Active, "ON", false },
|
||||
{ "active while fan is unavailable", ShowerState.Active, "unavailable", true },
|
||||
{ "active while fan state is unknown", ShowerState.Active, "unknown", true },
|
||||
{ "active while fan state is empty", ShowerState.Active, "", true },
|
||||
{ "active while fan state is missing", ShowerState.Active, null, true },
|
||||
{ "suspected while fan is off", ShowerState.Suspected, "off", true },
|
||||
{ "suspected while fan is on", ShowerState.Suspected, "on", false },
|
||||
{ "idle does not touch the fan", ShowerState.Idle, "off", false },
|
||||
{ "declining does not turn the fan on", ShowerState.Declining, "off", false },
|
||||
{ "completed does not turn the fan on", ShowerState.Completed, "off", false }
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> FanRunAfterCases =>
|
||||
[
|
||||
["well before run-after duration", 10, 60, false],
|
||||
["one second before run-after duration", 10, 599, false],
|
||||
["exactly at run-after duration", 10, 600, true],
|
||||
["one second after run-after duration", 10, 601, true],
|
||||
["well after run-after duration", 10, 660, true],
|
||||
["zero-minute run-after turns off immediately", 0, 0, true],
|
||||
["long run-after keeps fan running", 30, 300, false]
|
||||
];
|
||||
public static TheoryData<string, int, int, bool> FanRunAfterCases => new()
|
||||
{
|
||||
{ "well before run-after duration", 10, 60, false },
|
||||
{ "one second before run-after duration", 10, 599, false },
|
||||
{ "exactly at run-after duration", 10, 600, true },
|
||||
{ "one second after run-after duration", 10, 601, true },
|
||||
{ "well after run-after duration", 10, 660, true },
|
||||
{ "zero-minute run-after turns off immediately", 0, 0, true },
|
||||
{ "long run-after keeps fan running", 30, 300, false }
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> ResetCases =>
|
||||
[
|
||||
["fan on after we turned it on", true, "on", true],
|
||||
["fan reports uppercase on", true, "ON", true],
|
||||
["fan already off again", true, "off", false],
|
||||
["fan unavailable", true, "unavailable", false],
|
||||
["fan state missing", true, null!, false],
|
||||
["we never turned the fan on", false, "on", false]
|
||||
];
|
||||
public static TheoryData<string, bool, string?, bool> ResetCases => new()
|
||||
{
|
||||
{ "fan on after we turned it on", true, "on", true },
|
||||
{ "fan reports uppercase on", true, "ON", true },
|
||||
{ "fan already off again", true, "off", false },
|
||||
{ "fan unavailable", true, "unavailable", false },
|
||||
{ "fan state missing", true, null, false },
|
||||
{ "we never turned the fan on", false, "on", false }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ShowerProgressFanStateCases))]
|
||||
|
||||
@@ -13,28 +13,28 @@ public class HumidityRateCalculatorTests
|
||||
|
||||
private DateTimeOffset Now => _timeProvider.GetUtcNow();
|
||||
|
||||
public static IEnumerable<object[]> RateCases =>
|
||||
[
|
||||
["no readings", 5, Array.Empty<double>(), Array.Empty<int>(), 0.0],
|
||||
["single reading", 5, new[] { 52.9 }, new[] { 0 }, 0.0],
|
||||
["rising humidity", 5, new[] { 52.9, 62.9 }, new[] { 0, 60 }, 10.0],
|
||||
["falling humidity", 5, new[] { 62.9, 52.9 }, new[] { 0, 120 }, -5.0],
|
||||
["same timestamp", 5, new[] { 52.9, 62.9 }, new[] { 0, 0 }, 0.0],
|
||||
["flat humidity", 5, new[] { 55.0, 55.0, 55.0 }, new[] { 0, 60, 120 }, 0.0],
|
||||
["linear three readings", 5, new[] { 50.0, 55.0, 60.0 }, new[] { 0, 60, 120 }, 5.0],
|
||||
["window drops stalest reading", 3, new[] { 52.9, 57.9, 62.9, 64.9 }, new[] { 0, 60, 120, 180 }, 3.5],
|
||||
["window keeps only last two readings", 2, new[] { 50.0, 55.0, 60.0 }, new[] { 0, 60, 120 }, 5.0],
|
||||
["single-sample window always returns zero", 1, new[] { 50.0, 55.0, 60.0 }, new[] { 0, 60, 120 }, 0.0],
|
||||
["zero-sized window always returns zero", 0, new[] { 50.0, 55.0, 60.0 }, new[] { 0, 60, 120 }, 0.0],
|
||||
["gap larger than window", 5, new[] { 20.0, 26.0 }, new[] { 0, 3_600 }, 0.1],
|
||||
["out of order timestamps", 5, new[] { 60.0, 50.0 }, new[] { 120, 0 }, 0.0],
|
||||
["very large rise", 5, new[] { 0.0, 10_000.0 }, new[] { 0, 60 }, 10_000.0],
|
||||
["negative humidity values", 5, new[] { -20.0, 0.0 }, new[] { 0, 60 }, 20.0],
|
||||
["tiny rise", 5, new[] { 50.0, 50.001 }, new[] { 0, 60 }, 0.001],
|
||||
["shower spike over sixty seconds", 5, new[] { 52.9, 59.9, 64.9 }, new[] { 0, 30, 60 }, 12.0],
|
||||
["middle readings do not affect the window endpoints", 3, new[] { 40.0, 80.0, 60.0, 45.0 }, new[] { 0, 30, 60, 90 }, -35.0],
|
||||
["small change over fifteen minutes", 5, new[] { 50.0, 50.6 }, new[] { 0, 900 }, 0.04]
|
||||
];
|
||||
public static TheoryData<string, int, double[], int[], double> RateCases => new()
|
||||
{
|
||||
{ "no readings", 5, Array.Empty<double>(), Array.Empty<int>(), 0.0 },
|
||||
{ "single reading", 5, [52.9], [0], 0.0 },
|
||||
{ "rising humidity", 5, [52.9, 62.9], [0, 60], 10.0 },
|
||||
{ "falling humidity", 5, [62.9, 52.9], [0, 120], -5.0 },
|
||||
{ "same timestamp", 5, [52.9, 62.9], [0, 0], 0.0 },
|
||||
{ "flat humidity", 5, [55.0, 55.0, 55.0], [0, 60, 120], 0.0 },
|
||||
{ "linear three readings", 5, [50.0, 55.0, 60.0], [0, 60, 120], 5.0 },
|
||||
{ "window drops stalest reading", 3, [52.9, 57.9, 62.9, 64.9], [0, 60, 120, 180], 3.5 },
|
||||
{ "window keeps only last two readings", 2, [50.0, 55.0, 60.0], [0, 60, 120], 5.0 },
|
||||
{ "single-sample window always returns zero", 1, [50.0, 55.0, 60.0], [0, 60, 120], 0.0 },
|
||||
{ "zero-sized window always returns zero", 0, [50.0, 55.0, 60.0], [0, 60, 120], 0.0 },
|
||||
{ "gap larger than window", 5, [20.0, 26.0], [0, 3_600], 0.1 },
|
||||
{ "out of order timestamps", 5, [60.0, 50.0], [120, 0], 0.0 },
|
||||
{ "very large rise", 5, [0.0, 10_000.0], [0, 60], 10_000.0 },
|
||||
{ "negative humidity values", 5, [-20.0, 0.0], [0, 60], 20.0 },
|
||||
{ "tiny rise", 5, [50.0, 50.001], [0, 60], 0.001 },
|
||||
{ "shower spike over sixty seconds", 5, [52.9, 59.9, 64.9], [0, 30, 60], 12.0 },
|
||||
{ "middle readings do not affect the window endpoints", 3, [40.0, 80.0, 60.0, 45.0], [0, 30, 60, 90], -35.0 },
|
||||
{ "small change over fifteen minutes", 5, [50.0, 50.6], [0, 900], 0.04 }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RateCases))]
|
||||
|
||||
@@ -15,43 +15,43 @@ public class ShowerDetectionServiceTests
|
||||
|
||||
private DateTimeOffset Now => _timeProvider.GetUtcNow();
|
||||
|
||||
public static IEnumerable<object[]> IdleThresholdCases =>
|
||||
[
|
||||
["both thresholds met exactly", 52.0, 44.0, ShowerState.Suspected],
|
||||
["rate one step under threshold", 51.98, 44.0, ShowerState.Idle],
|
||||
["differential one step under threshold", 52.0, 44.02, ShowerState.Idle],
|
||||
["well above both thresholds", 60.0, 52.0, ShowerState.Suspected],
|
||||
["negative differential", 52.0, 60.0, ShowerState.Idle],
|
||||
["zero rate with large differential", 50.0, 42.0, ShowerState.Idle],
|
||||
["negative rate with large differential", 45.0, 37.0, ShowerState.Idle],
|
||||
["very large humidity spike", 1000.0, 500.0, ShowerState.Suspected]
|
||||
];
|
||||
public static TheoryData<string, double, double, ShowerState> IdleThresholdCases => new()
|
||||
{
|
||||
{ "both thresholds met exactly", 52.0, 44.0, ShowerState.Suspected },
|
||||
{ "rate one step under threshold", 51.98, 44.0, ShowerState.Idle },
|
||||
{ "differential one step under threshold", 52.0, 44.02, ShowerState.Idle },
|
||||
{ "well above both thresholds", 60.0, 52.0, ShowerState.Suspected },
|
||||
{ "negative differential", 52.0, 60.0, ShowerState.Idle },
|
||||
{ "zero rate with large differential", 50.0, 42.0, ShowerState.Idle },
|
||||
{ "negative rate with large differential", 45.0, 37.0, ShowerState.Idle },
|
||||
{ "very large humidity spike", 1000.0, 500.0, ShowerState.Suspected }
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> ConfirmationCases =>
|
||||
[
|
||||
["confirmation window reached", 66.0, 49.5, 120, ShowerState.Active],
|
||||
["confirmation window not yet reached", 66.0, 49.5, 119, ShowerState.Suspected],
|
||||
["same instant as the spike", 66.0, 49.5, 0, ShowerState.Suspected],
|
||||
["peak humidity exactly at threshold", 58.0, 49.0, 120, ShowerState.Active],
|
||||
["peak humidity just below threshold", 57.5, 49.0, 120, ShowerState.Suspected],
|
||||
["differential exactly at threshold", 60.0, 52.0, 120, ShowerState.Active],
|
||||
["differential just below threshold", 66.0, 58.02, 120, ShowerState.Suspected],
|
||||
["very large humidity at confirmation", 1000.0, 900.0, 120, ShowerState.Active]
|
||||
];
|
||||
public static TheoryData<string, double, double, int, ShowerState> ConfirmationCases => new()
|
||||
{
|
||||
{ "confirmation window reached", 66.0, 49.5, 120, ShowerState.Active },
|
||||
{ "confirmation window not yet reached", 66.0, 49.5, 119, ShowerState.Suspected },
|
||||
{ "same instant as the spike", 66.0, 49.5, 0, ShowerState.Suspected },
|
||||
{ "peak humidity exactly at threshold", 58.0, 49.0, 120, ShowerState.Active },
|
||||
{ "peak humidity just below threshold", 57.5, 49.0, 120, ShowerState.Suspected },
|
||||
{ "differential exactly at threshold", 60.0, 52.0, 120, ShowerState.Active },
|
||||
{ "differential just below threshold", 66.0, 58.02, 120, ShowerState.Suspected },
|
||||
{ "very large humidity at confirmation", 1000.0, 900.0, 120, ShowerState.Active }
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> RecoveryBoundaryCases =>
|
||||
[
|
||||
["differential well under threshold", 52.0, ShowerState.Completed],
|
||||
["differential exactly at threshold", 50.0, ShowerState.Completed],
|
||||
["differential just above threshold", 49.98, ShowerState.Declining]
|
||||
];
|
||||
public static TheoryData<string, double, ShowerState> RecoveryBoundaryCases => new()
|
||||
{
|
||||
{ "differential well under threshold", 52.0, ShowerState.Completed },
|
||||
{ "differential exactly at threshold", 50.0, ShowerState.Completed },
|
||||
{ "differential just above threshold", 49.98, ShowerState.Declining }
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> MaxDurationCases =>
|
||||
[
|
||||
["one second before max duration", 3_599, ShowerState.Active],
|
||||
["exactly at max duration", 3_600, ShowerState.Declining],
|
||||
["one second past max duration", 3_601, ShowerState.Declining]
|
||||
];
|
||||
public static TheoryData<string, int, ShowerState> MaxDurationCases => new()
|
||||
{
|
||||
{ "one second before max duration", 3_599, ShowerState.Active },
|
||||
{ "exactly at max duration", 3_600, ShowerState.Declining },
|
||||
{ "one second past max duration", 3_601, ShowerState.Declining }
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void InitialState_IsIdle()
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace ShowerMonitor.Manager.Tests;
|
||||
internal static class ShowerMonitorTestData
|
||||
{
|
||||
/// <summary>Fixed base time used by all timestamp-sensitive tests.</summary>
|
||||
public static readonly DateTimeOffset BaseTimestamp = new(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
|
||||
private static readonly DateTimeOffset BaseTimestamp = new(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
/// <summary>Creates a fake clock positioned at <see cref="BaseTimestamp"/> unless overridden.</summary>
|
||||
public static FakeTimeProvider CreateTimeProvider(DateTimeOffset? start = null) => new(start ?? BaseTimestamp);
|
||||
@@ -58,8 +58,7 @@ internal static class ShowerMonitorTestData
|
||||
/// <summary>Builds a sample series from (humidity, offset-in-seconds) pairs.</summary>
|
||||
public static IReadOnlyList<HumiditySample> CreateHumiditySeries(
|
||||
params (double Humidity, int OffsetSeconds)[] samples) =>
|
||||
samples.Select(sample => new HumiditySample(sample.Humidity, TimeSpan.FromSeconds(sample.OffsetSeconds)))
|
||||
.ToArray();
|
||||
[.. samples.Select(sample => new HumiditySample(sample.Humidity, TimeSpan.FromSeconds(sample.OffsetSeconds)))];
|
||||
|
||||
/// <summary>
|
||||
/// Generates evenly spaced samples along a constant humidity slope, useful for
|
||||
@@ -70,9 +69,8 @@ internal static class ShowerMonitorTestData
|
||||
double ratePerMinute,
|
||||
int sampleCount,
|
||||
int intervalSeconds = 60) =>
|
||||
Enumerable.Range(0, sampleCount)
|
||||
[.. Enumerable.Range(0, sampleCount)
|
||||
.Select(i => new HumiditySample(
|
||||
startHumidity + (ratePerMinute * i * intervalSeconds / 60.0),
|
||||
TimeSpan.FromSeconds(i * intervalSeconds)))
|
||||
.ToArray();
|
||||
startHumidity + ratePerMinute * i * intervalSeconds / 60.0,
|
||||
TimeSpan.FromSeconds(i * intervalSeconds)))];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user