using ErsatzTV.Infrastructure.Streaming.Graphics; using NCalc; using NUnit.Framework; using Shouldly; namespace ErsatzTV.Infrastructure.Tests.Streaming.Graphics; [TestFixture] public class OpacityExpressionHelperTests { // Exercises the NCalc custom-function wiring (FunctionEventArgs / FunctionData under NCalc 6.x) // exactly the way the graphics elements do: build an Expression, hook EvaluateFunction, evaluate. private static float Evaluate(string expressionString, TimeSpan timeOfDay) { var expression = new Expression(expressionString); expression.EvaluateFunction += OpacityExpressionHelper.EvaluateFunction; return OpacityExpressionHelper.GetOpacity( expression, timeOfDay, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero); } // LinearFadePoints(time, start=0, peakStart=10, peakEnd=20, end=30) [TestCase(5, 0.5)] // fade in: (5-0)/(10-0) [TestCase(15, 1.0)] // solid [TestCase(25, 0.5)] // fade out: (30-25)/(30-20) [TestCase(35, 0.0)] // past end public void LinearFadePoints_ReturnsExpectedOpacity(int timeOfDaySeconds, double expected) { float opacity = Evaluate( "LinearFadePoints(time_of_day_seconds, 0, 10, 20, 30)", TimeSpan.FromSeconds(timeOfDaySeconds)); opacity.ShouldBe((float)expected, 0.0001); } // LinearFadeDuration(time, start=0, fadeSeconds=10, peakSeconds=10) // -> peakStart=10, peakEnd=20, end=30 (same curve as the LinearFadePoints case above) [TestCase(5, 0.5)] // mid fade in [TestCase(15, 1.0)] // solid [TestCase(25, 0.5)] // mid fade out public void LinearFadeDuration_ReturnsExpectedOpacity(int timeOfDaySeconds, double expected) { float opacity = Evaluate( "LinearFadeDuration(time_of_day_seconds, 0, 10, 10)", TimeSpan.FromSeconds(timeOfDaySeconds)); opacity.ShouldBe((float)expected, 0.0001); } }