GraphTUI/tests/GraphTUI.Tests/ClippingAndInteractiveTests.cs
2026-10-28 22:47:38 +03:00

131 lines
5.1 KiB
C#

using GraphTUI.Core.Math;
using GraphTUI.Core.Models;
using GraphTUI.Rendering;
using GraphTUI.Terminal;
using GraphTUI.UI.Views;
namespace GraphTUI.Tests;
public static class ClippingAndInteractiveTests
{
public static void Register(TestRunner runner)
{
runner.Add("Clipping: 8-pass Cohen-Sutherland keeps diagonal line", () =>
{
var canvas = new BrailleCanvas(20, 20); // 40 x 80 subpixels
// Line from (-50, -50) to (100, 100) crosses the canvas diagonally
canvas.DrawLine(-50, -50, 100, 100, ColorRgb.White);
// Subpixel center (20, 40) should have been drawn
// Check that at least some pixels were plotted on the canvas
bool anyPixelSet = false;
for (int sy = 0; sy < canvas.SubpixelHeight; sy++)
{
for (int sx = 0; sx < canvas.SubpixelWidth; sx++)
{
// If center line area has dots
if (sx == sy / 2)
{
anyPixelSet = true;
break;
}
}
if (anyPixelSet) break;
}
Assert.True(anyPixelSet, "Expected pixels to be rasterized on canvas");
});
runner.Add("Parameters: Unbounded range expansion and custom steps", () =>
{
var param = new Parameter("speed", 5.0, -10.0, 10.0, 0.1);
Assert.Equal(5.0, param.Value);
// Expand range beyond original [-10, 10]
param.SetRange(-100.0, 100.0, 1.0);
param.SetValue(75.0);
Assert.Equal(75.0, param.Value);
Assert.Equal(-100.0, param.Min);
Assert.Equal(100.0, param.Max);
// Increment
param.Increment();
Assert.Equal(76.0, param.Value);
param.Decrement(2.0);
Assert.Equal(74.0, param.Value);
});
runner.Add("Builtin Functions: Validation and comma counting", () =>
{
// 3-arg sum: sum(start, end, body)
var eval3 = Evaluator.Compile("sum(1, 5, x)");
Assert.NotNull(eval3);
Assert.Equal(15.0, eval3.Evaluate(3)); // 5 * 3 = 15
// 4-arg sum: sum(n, start, end, body)
var eval4 = Evaluator.Compile("sum(n, 1, 4, n)");
Assert.NotNull(eval4);
Assert.Equal(10.0, eval4.Evaluate(0)); // 1 + 2 + 3 + 4 = 10
// factorial function call
var evalFact = Evaluator.Compile("factorial(5)");
Assert.Equal(120.0, evalFact.Evaluate(0));
});
runner.Add("Modals: Keyboard exit and interactivity parity", () =>
{
var viewport = new Viewport();
// 1. CalculusModal
var calc = new CalculusModal { IsOpen = true };
var fn = new GraphFunction(1, "y = x^2", ColorRgb.Cyan);
calc.Open(fn, viewport, null);
Assert.True(calc.IsOpen);
// Up/Down/Tab navigation
calc.HandleKey(new KeyAction(KeyCode.Down), viewport, null, () => { });
Assert.Equal(1, calc.SelectedControlIndex);
calc.HandleKey(new KeyAction(KeyCode.Tab), viewport, null, () => { });
Assert.Equal(2, calc.SelectedControlIndex);
// Exit with 'q'
calc.HandleKey(new KeyAction(KeyCode.Char, 'q'), viewport, null, () => { });
Assert.False(calc.IsOpen, "CalculusModal should exit on 'q'");
// Exit with 'F3'
calc.IsOpen = true;
calc.HandleKey(new KeyAction(KeyCode.F3), viewport, null, () => { });
Assert.False(calc.IsOpen, "CalculusModal should exit on F3");
// 2. CheatsheetModal
var cheat = new CheatsheetModal { IsOpen = true };
cheat.HandleKey(new KeyAction(KeyCode.Char, 'q'));
Assert.False(cheat.IsOpen, "CheatsheetModal should exit on 'q'");
cheat.IsOpen = true;
cheat.HandleKey(new KeyAction(KeyCode.F5));
Assert.False(cheat.IsOpen, "CheatsheetModal should exit on F5");
// 3. PresetsGalleryModal
var presets = new PresetsGalleryModal { IsOpen = true };
presets.HandleKey(new KeyAction(KeyCode.Char, 'q'), _ => { });
Assert.False(presets.IsOpen, "PresetsGalleryModal should exit on 'q'");
presets.IsOpen = true;
presets.HandleKey(new KeyAction(KeyCode.F4), _ => { });
Assert.False(presets.IsOpen, "PresetsGalleryModal should exit on F4");
// 4. HelpModal
var help = new HelpModal { IsOpen = true };
help.HandleKey(new KeyAction(KeyCode.Char, 'q'));
Assert.False(help.IsOpen, "HelpModal should exit on 'q'");
help.IsOpen = true;
help.HandleKey(new KeyAction(KeyCode.F1));
Assert.False(help.IsOpen, "HelpModal should exit on F1");
help.IsOpen = true;
help.HandleKey(new KeyAction(KeyCode.Escape));
Assert.False(help.IsOpen, "HelpModal should exit on Escape");
});
}
}