From dd3d73f3a6bec5a1d91996f35ad77252560fbe77 Mon Sep 17 00:00:00 2001 From: Jason Dove <1695733+jasongdove@users.noreply.github.com> Date: Tue, 20 Jan 2026 09:50:11 -0600 Subject: [PATCH] add tests --- .../Scheduling/CountExpressionTests.cs | 34 +++++++++++++++++++ ErsatzTV.Core/Scheduling/CountExpression.cs | 13 ++++++- ErsatzTV/Pages/ScheduleItemsEditor.razor | 3 +- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 ErsatzTV.Core.Tests/Scheduling/CountExpressionTests.cs diff --git a/ErsatzTV.Core.Tests/Scheduling/CountExpressionTests.cs b/ErsatzTV.Core.Tests/Scheduling/CountExpressionTests.cs new file mode 100644 index 000000000..b416dbd29 --- /dev/null +++ b/ErsatzTV.Core.Tests/Scheduling/CountExpressionTests.cs @@ -0,0 +1,34 @@ +using ErsatzTV.Core.Interfaces.Scheduling; +using ErsatzTV.Core.Scheduling; +using NSubstitute; +using NUnit.Framework; +using Shouldly; + +namespace ErsatzTV.Core.Tests.Scheduling; + +[TestFixture] +public class CountExpressionTests +{ + [Test] + [TestCase("2", 2)] + [TestCase("count", 10)] + [TestCase("count / 2", 5)] + [TestCase("count * 2", 20)] + [TestCase("count + 1", 11)] + [TestCase("count - 1", 9)] + [TestCase("random % 4 + 1", 3)] + [TestCase("invalid", 0)] + [TestCase("count / 0", 0)] + public void Should_Evaluate_Expression(string expression, int expected) + { + var enumerator = Substitute.For(); + enumerator.Count.Returns(10); + + var random = Substitute.For(); + random.Next().Returns(2); + + int result = CountExpression.Evaluate(expression, enumerator, random, CancellationToken.None); + + result.ShouldBe(expected); + } +} diff --git a/ErsatzTV.Core/Scheduling/CountExpression.cs b/ErsatzTV.Core/Scheduling/CountExpression.cs index 26a2fb718..e820431ee 100644 --- a/ErsatzTV.Core/Scheduling/CountExpression.cs +++ b/ErsatzTV.Core/Scheduling/CountExpression.cs @@ -25,11 +25,22 @@ public static class CountExpression }; }; - object expressionResult = expression.Evaluate(cancellationToken); + object expressionResult = 0; + try + { + expressionResult = expression.Evaluate(cancellationToken); + } + catch (Exception) + { + // do nothing + } + return expressionResult switch { + double d when double.IsInfinity(d) || double.IsNaN(d) => 0, double doubleResult => (int)Math.Floor(doubleResult), int intResult => intResult, + long longResult => (int)longResult, _ => 0 }; } diff --git a/ErsatzTV/Pages/ScheduleItemsEditor.razor b/ErsatzTV/Pages/ScheduleItemsEditor.razor index 7e05c74d6..ab1a6398f 100644 --- a/ErsatzTV/Pages/ScheduleItemsEditor.razor +++ b/ErsatzTV/Pages/ScheduleItemsEditor.razor @@ -532,7 +532,8 @@ + Disabled="@(_selectedItem.PlayoutMode is not PlayoutMode.Multiple || _selectedItem.MultipleMode is not MultipleMode.Count)" + HelperText="Can be an expression with 'count' (collection size) and 'random' (0 to count-1)"/>