Fixes #4282 - Migrate tests from UnitTests to UnitTests.Parallelizable (67 tests migrated + comprehensive documentation) (#4293)

* Initial plan

* Add 31 parallelizable LineCanvas unit tests

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add 4 parallelizable Ruler unit tests

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Complete Category A migration - 35 tests migrated

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Remove duplicate tests from UnitTests after migration to Parallelizable

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Migrate 6 pure unit tests from ColorPicker and DatePicker to Parallelizable

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix duplicate test names between UnitTests and Parallelizable

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Migrate 11 Label tests to Parallelizable (52 tests total)

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix documentation: SetupFakeDriver tests CAN be parallelized

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add comprehensive parallelization rules to documentation

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update README and start migrating Button/CheckBox/RadioGroup tests

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Remove duplicate Button tests from UnitTests

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Migrate 5 CheckBox unit tests to Parallelizable (27 with Theory expansion)

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Migrate 4 more CheckBox tests to Parallelizable (67 tests total, 9,478 passing)

Co-authored-by: tig <585482+tig@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
This commit is contained in:
Copilot
2025-10-19 19:04:43 -06:00
committed by GitHub
parent b6fff43824
commit ed64f5773e
16 changed files with 1255 additions and 834 deletions

View File

@@ -0,0 +1,260 @@
namespace Terminal.Gui.DrawingTests;
/// <summary>
/// Pure unit tests for <see cref="LineCanvas"/> that don't require Application.Driver or View context.
/// These tests focus on properties and behavior that don't depend on glyph rendering.
///
/// Note: Tests that verify rendered output (ToString()) cannot be parallelized because LineCanvas
/// depends on Application.Driver for glyph resolution and configuration. Those tests remain in UnitTests.
/// </summary>
public class LineCanvasTests : UnitTests.Parallelizable.ParallelizableBase
{
#region Basic API Tests
[Fact]
public void Empty_Canvas_ToString_Returns_EmptyString ()
{
var canvas = new LineCanvas ();
Assert.Equal (string.Empty, canvas.ToString ());
}
[Fact]
public void Clear_Removes_All_Lines ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
canvas.AddLine (new (0, 0), 3, Orientation.Vertical, LineStyle.Single);
canvas.Clear ();
Assert.Empty (canvas.Lines);
Assert.Equal (Rectangle.Empty, canvas.Bounds);
Assert.Equal (string.Empty, canvas.ToString ());
}
[Fact]
public void Lines_Property_Returns_ReadOnly_Collection ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
Assert.Single (canvas.Lines);
Assert.IsAssignableFrom<IReadOnlyCollection<StraightLine>> (canvas.Lines);
}
[Fact]
public void AddLine_Adds_Line_To_Collection ()
{
var canvas = new LineCanvas ();
Assert.Empty (canvas.Lines);
canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
Assert.Single (canvas.Lines);
canvas.AddLine (new (0, 0), 3, Orientation.Vertical, LineStyle.Single);
Assert.Equal (2, canvas.Lines.Count);
}
[Fact]
public void Constructor_With_Lines_Creates_Canvas_With_Lines ()
{
var lines = new[]
{
new StraightLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single),
new StraightLine (new (0, 0), 3, Orientation.Vertical, LineStyle.Single)
};
var canvas = new LineCanvas (lines);
Assert.Equal (2, canvas.Lines.Count);
}
#endregion
#region Bounds Tests - Tests for Bounds property
[Theory]
[InlineData (0, 0, 0, 0, 0, 1, 1)]
[InlineData (0, 0, 1, 0, 0, 1, 1)]
[InlineData (0, 0, 2, 0, 0, 2, 2)]
[InlineData (0, 0, 3, 0, 0, 3, 3)]
[InlineData (0, 0, -1, 0, 0, 1, 1)]
[InlineData (0, 0, -2, -1, -1, 2, 2)]
[InlineData (0, 0, -3, -2, -2, 3, 3)]
public void Viewport_H_And_V_Lines_Both_Positive (
int x,
int y,
int length,
int expectedX,
int expectedY,
int expectedWidth,
int expectedHeight
)
{
var canvas = new LineCanvas ();
canvas.AddLine (new (x, y), length, Orientation.Horizontal, LineStyle.Single);
canvas.AddLine (new (x, y), length, Orientation.Vertical, LineStyle.Single);
Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Bounds);
}
[Theory]
[InlineData (0, 0, 0, 0, 0, 1, 1)]
[InlineData (0, 0, 1, 0, 0, 1, 1)]
[InlineData (0, 0, 2, 0, 0, 2, 1)]
[InlineData (0, 0, 3, 0, 0, 3, 1)]
[InlineData (0, 0, -1, 0, 0, 1, 1)]
[InlineData (0, 0, -2, -1, 0, 2, 1)]
[InlineData (0, 0, -3, -2, 0, 3, 1)]
public void Viewport_H_Line (
int x,
int y,
int length,
int expectedX,
int expectedY,
int expectedWidth,
int expectedHeight
)
{
var canvas = new LineCanvas ();
canvas.AddLine (new (x, y), length, Orientation.Horizontal, LineStyle.Single);
Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Bounds);
}
[Fact]
public void Bounds_Specific_Coordinates ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (5, 5), 3, Orientation.Horizontal, LineStyle.Single);
Assert.Equal (new (5, 5, 3, 1), canvas.Bounds);
}
[Fact]
public void Bounds_Empty_Canvas_Returns_Empty_Rectangle ()
{
var canvas = new LineCanvas ();
Assert.Equal (Rectangle.Empty, canvas.Bounds);
}
[Fact]
public void Bounds_Single_Point_Zero_Length ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (5, 5), 0, Orientation.Horizontal, LineStyle.Single);
Assert.Equal (new (5, 5, 1, 1), canvas.Bounds);
}
[Fact]
public void Bounds_Horizontal_Line ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (2, 3), 5, Orientation.Horizontal, LineStyle.Single);
Assert.Equal (new (2, 3, 5, 1), canvas.Bounds);
}
[Fact]
public void Bounds_Vertical_Line ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (2, 3), 5, Orientation.Vertical, LineStyle.Single);
Assert.Equal (new (2, 3, 1, 5), canvas.Bounds);
}
[Fact]
public void Bounds_Multiple_Lines_Returns_Union ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
canvas.AddLine (new (0, 0), 3, Orientation.Vertical, LineStyle.Single);
Assert.Equal (new (0, 0, 5, 3), canvas.Bounds);
}
[Fact]
public void Bounds_Negative_Length_Line ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (5, 5), -3, Orientation.Horizontal, LineStyle.Single);
// Line from (5,5) going left 3 positions: includes points 3, 4, 5 (width 3, X starts at 3)
Assert.Equal (new (3, 5, 3, 1), canvas.Bounds);
}
[Fact]
public void Bounds_Complex_Box ()
{
var canvas = new LineCanvas ();
// top
canvas.AddLine (new (0, 0), 3, Orientation.Horizontal, LineStyle.Single);
// left
canvas.AddLine (new (0, 0), 2, Orientation.Vertical, LineStyle.Single);
// right
canvas.AddLine (new (2, 0), 2, Orientation.Vertical, LineStyle.Single);
// bottom
canvas.AddLine (new (0, 2), 3, Orientation.Horizontal, LineStyle.Single);
Assert.Equal (new (0, 0, 3, 3), canvas.Bounds);
}
#endregion
#region Exclusion Tests
[Fact]
public void ClearExclusions_Clears_Exclusion_Region ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
var region = new Region (new Rectangle (0, 0, 2, 1));
canvas.Exclude (region);
canvas.ClearExclusions ();
// After clearing exclusions, GetMap should return all points
var map = canvas.GetMap ();
Assert.Equal (5, map.Count);
}
[Fact]
public void Exclude_Removes_Points_From_Map ()
{
var canvas = new LineCanvas ();
canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
var region = new Region (new Rectangle (0, 0, 2, 1));
canvas.Exclude (region);
var map = canvas.GetMap ();
// Should have 5 - 2 = 3 points (excluding the first 2)
Assert.Equal (3, map.Count);
}
#endregion
#region Fill Property Tests
[Fact]
public void Fill_Property_Can_Be_Set ()
{
var foregroundFill = new SolidFill (new Color (255, 0));
var backgroundFill = new SolidFill (new Color (0, 0));
var fillPair = new FillPair (foregroundFill, backgroundFill);
var canvas = new LineCanvas { Fill = fillPair };
Assert.Equal (fillPair, canvas.Fill);
}
[Fact]
public void Fill_Property_Defaults_To_Null ()
{
var canvas = new LineCanvas ();
Assert.Null (canvas.Fill);
}
#endregion
}

View File

@@ -0,0 +1,46 @@
namespace Terminal.Gui.DrawingTests;
/// <summary>
/// Pure unit tests for <see cref="Ruler"/> that don't require Application.Driver or View context.
/// These tests focus on properties and behavior that don't depend on rendering.
///
/// Note: Tests that verify rendered output (Draw methods) require Application.Driver and remain in UnitTests as integration tests.
/// </summary>
public class RulerTests : UnitTests.Parallelizable.ParallelizableBase
{
[Fact]
public void Constructor_Defaults ()
{
var r = new Ruler ();
Assert.Equal (0, r.Length);
Assert.Equal (Orientation.Horizontal, r.Orientation);
}
[Fact]
public void Attribute_Set ()
{
var newAttribute = new Attribute (Color.Red, Color.Green);
var r = new Ruler ();
r.Attribute = newAttribute;
Assert.Equal (newAttribute, r.Attribute);
}
[Fact]
public void Length_Set ()
{
var r = new Ruler ();
Assert.Equal (0, r.Length);
r.Length = 42;
Assert.Equal (42, r.Length);
}
[Fact]
public void Orientation_Set ()
{
var r = new Ruler ();
Assert.Equal (Orientation.Horizontal, r.Orientation);
r.Orientation = Orientation.Vertical;
Assert.Equal (Orientation.Vertical, r.Orientation);
}
}

View File

@@ -1,2 +1,127 @@
# Automated Unit Tests for parallelizable code
# UnitTests.Parallelizable
This project contains unit tests that can run in parallel without interference. Tests here must not depend on global state or static Application infrastructure.
## Migration Rules
### Tests CAN be parallelized if they:
- ✅ Test properties, constructors, and basic operations
- ✅ Use `[SetupFakeDriver]` without Application statics
- ✅ Call `View.Draw()`, `LayoutAndDraw()` without Application statics
- ✅ Verify visual output with `DriverAssert` (when using `[SetupFakeDriver]`)
- ✅ Create View hierarchies without `Application.Top`
- ✅ Test events and behavior without global state
- ✅ Use `View.BeginInit()` / `View.EndInit()` for initialization
### Tests CANNOT be parallelized if they:
- ❌ Use `[AutoInitShutdown]` - requires `Application.Init/Shutdown` which creates global state
- ❌ Set `Application.Driver` (global singleton)
- ❌ Call `Application.Init()`, `Application.Run/Run<T>()`, or `Application.Begin()`
- ❌ Modify `ConfigurationManager` global state (Enable/Load/Apply/Disable)
- ❌ Modify static properties like `Key.Separator`, `CultureInfo.CurrentCulture`, etc.
- ❌ Use `Application.Top`, `Application.Driver`, `Application.MainLoop`, or `Application.Navigation`
- ❌ Are true integration tests that test multiple components working together
### Important Notes
- Many tests in `UnitTests` blindly use the above patterns when they don't actually need them
- These tests CAN be rewritten to remove unnecessary dependencies and migrated here
- Many tests APPEAR to be integration tests but are just poorly written and cover multiple surface areas - these can be split into focused unit tests
- When in doubt, analyze if the test truly needs global state or can be refactored
## How to Migrate Tests
1. **Identify** tests in `UnitTests` that don't actually need Application statics
2. **Rewrite** tests to remove `[AutoInitShutdown]`, `Application.Begin()`, etc. if not needed
3. **Move** the test to the equivalent file in `UnitTests.Parallelizable`
4. **Delete** the old test from `UnitTests` to avoid duplicates
5. **Verify** no duplicate test names exist (CI will check this)
6. **Test** to ensure the migrated test passes
## Example Migrations
### Simple Property Test (no changes needed)
```csharp
// Before (in UnitTests)
[Fact]
public void Constructor_Sets_Defaults ()
{
var view = new Button ();
Assert.Empty (view.Text);
}
// After (in UnitTests.Parallelizable) - just move it!
[Fact]
public void Constructor_Sets_Defaults ()
{
var view = new Button ();
Assert.Empty (view.Text);
}
```
### Remove Unnecessary [SetupFakeDriver]
```csharp
// Before (in UnitTests)
[Fact]
[SetupFakeDriver]
public void Event_Fires_When_Property_Changes ()
{
var view = new Button ();
var fired = false;
view.TextChanged += (s, e) => fired = true;
view.Text = "Hello";
Assert.True (fired);
}
// After (in UnitTests.Parallelizable) - remove attribute!
[Fact]
public void Event_Fires_When_Property_Changes ()
{
var view = new Button ();
var fired = false;
view.TextChanged += (s, e) => fired = true;
view.Text = "Hello";
Assert.True (fired);
}
```
### Replace Application.Begin with View Initialization
```csharp
// Before (in UnitTests)
[Fact]
[AutoInitShutdown]
public void Focus_Test ()
{
var view = new Button ();
var top = new Toplevel ();
top.Add (view);
Application.Begin (top);
view.SetFocus ();
Assert.True (view.HasFocus);
top.Dispose ();
}
// After (in UnitTests.Parallelizable) - use BeginInit/EndInit!
[Fact]
public void Focus_Test ()
{
var superView = new View ();
var view = new Button ();
superView.Add (view);
superView.BeginInit ();
superView.EndInit ();
view.SetFocus ();
Assert.True (view.HasFocus);
}
```
## Running Tests
Tests in this project run in parallel automatically. To run them:
```bash
dotnet test Tests/UnitTestsParallelizable/UnitTests.Parallelizable.csproj
```
## See Also
- [Category A Migration Summary](../CATEGORY_A_MIGRATION_SUMMARY.md) - Detailed analysis and migration guidelines
- [.NET Unit Testing Best Practices](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices)

View File

@@ -0,0 +1,147 @@
namespace Terminal.Gui.ViewsTests;
/// <summary>
/// Pure unit tests for <see cref="Button"/> that don't require Application static dependencies.
/// These tests can run in parallel without interference.
/// </summary>
public class ButtonTests : UnitTests.Parallelizable.ParallelizableBase
{
[Fact]
public void Text_Mirrors_Title ()
{
var view = new Button ();
view.Title = "Hello";
Assert.Equal ("Hello", view.Title);
Assert.Equal ("Hello", view.TitleTextFormatter.Text);
Assert.Equal ("Hello", view.Text);
Assert.Equal ($"{Glyphs.LeftBracket} Hello {Glyphs.RightBracket}", view.TextFormatter.Text);
view.Dispose ();
}
[Fact]
public void Title_Mirrors_Text ()
{
var view = new Button ();
view.Text = "Hello";
Assert.Equal ("Hello", view.Text);
Assert.Equal ($"{Glyphs.LeftBracket} Hello {Glyphs.RightBracket}", view.TextFormatter.Text);
Assert.Equal ("Hello", view.Title);
Assert.Equal ("Hello", view.TitleTextFormatter.Text);
view.Dispose ();
}
[Theory]
[InlineData ("01234", 0, 0, 0, 0)]
[InlineData ("01234", 1, 0, 1, 0)]
[InlineData ("01234", 0, 1, 0, 1)]
[InlineData ("01234", 1, 1, 1, 1)]
[InlineData ("01234", 10, 1, 10, 1)]
[InlineData ("01234", 10, 3, 10, 3)]
[InlineData ("0_1234", 0, 0, 0, 0)]
[InlineData ("0_1234", 1, 0, 1, 0)]
[InlineData ("0_1234", 0, 1, 0, 1)]
[InlineData ("0_1234", 1, 1, 1, 1)]
[InlineData ("0_1234", 10, 1, 10, 1)]
[InlineData ("0_12你", 10, 3, 10, 3)]
[InlineData ("0_12你", 0, 0, 0, 0)]
[InlineData ("0_12你", 1, 0, 1, 0)]
[InlineData ("0_12你", 0, 1, 0, 1)]
[InlineData ("0_12你", 1, 1, 1, 1)]
[InlineData ("0_12你", 10, 1, 10, 1)]
public void Button_AbsoluteSize_Text (string text, int width, int height, int expectedWidth, int expectedHeight)
{
// Override CM
Button.DefaultShadow = ShadowStyle.None;
var btn1 = new Button
{
Text = text,
Width = width,
Height = height
};
Assert.Equal (new (expectedWidth, expectedHeight), btn1.Frame.Size);
Assert.Equal (new (expectedWidth, expectedHeight), btn1.Viewport.Size);
Assert.Equal (new (expectedWidth, expectedHeight), btn1.GetContentSize ());
Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.TextFormatter.ConstrainToSize);
btn1.Dispose ();
}
[Theory]
[InlineData (0, 0, 0, 0)]
[InlineData (1, 0, 1, 0)]
[InlineData (0, 1, 0, 1)]
[InlineData (1, 1, 1, 1)]
[InlineData (10, 1, 10, 1)]
[InlineData (10, 3, 10, 3)]
public void Button_AbsoluteSize_DefaultText (int width, int height, int expectedWidth, int expectedHeight)
{
// Override CM
Button.DefaultShadow = ShadowStyle.None;
var btn1 = new Button ();
btn1.Width = width;
btn1.Height = height;
Assert.Equal (new (expectedWidth, expectedHeight), btn1.Frame.Size);
Assert.Equal (new (expectedWidth, expectedHeight), btn1.Viewport.Size);
Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.TextFormatter.ConstrainToSize);
btn1.Dispose ();
}
[Fact]
public void Button_HotKeyChanged_EventFires ()
{
var btn = new Button { Text = "_Yar" };
object sender = null;
KeyChangedEventArgs args = null;
btn.HotKeyChanged += (s, e) =>
{
sender = s;
args = e;
};
btn.HotKeyChanged += (s, e) =>
{
sender = s;
args = e;
};
btn.HotKey = KeyCode.R;
Assert.Same (btn, sender);
Assert.Equal (KeyCode.Y, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
btn.HotKey = KeyCode.R;
Assert.Same (btn, sender);
Assert.Equal (KeyCode.Y, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
btn.Dispose ();
}
[Fact]
public void Button_HotKeyChanged_EventFires_WithNone ()
{
var btn = new Button ();
object sender = null;
KeyChangedEventArgs args = null;
btn.HotKeyChanged += (s, e) =>
{
sender = s;
args = e;
};
btn.HotKey = KeyCode.R;
Assert.Same (btn, sender);
Assert.Equal (KeyCode.Null, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
btn.Dispose ();
}
}

View File

@@ -0,0 +1,264 @@
using UnitTests;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewsTests;
public class CheckBoxTests (ITestOutputHelper output)
{
[Theory]
[InlineData ("01234", 0, 0, 0, 0)]
[InlineData ("01234", 1, 0, 1, 0)]
[InlineData ("01234", 0, 1, 0, 1)]
[InlineData ("01234", 1, 1, 1, 1)]
[InlineData ("01234", 10, 1, 10, 1)]
[InlineData ("01234", 10, 3, 10, 3)]
[InlineData ("0_1234", 0, 0, 0, 0)]
[InlineData ("0_1234", 1, 0, 1, 0)]
[InlineData ("0_1234", 0, 1, 0, 1)]
[InlineData ("0_1234", 1, 1, 1, 1)]
[InlineData ("0_1234", 10, 1, 10, 1)]
[InlineData ("0_12你", 10, 3, 10, 3)]
[InlineData ("0_12你", 0, 0, 0, 0)]
[InlineData ("0_12你", 1, 0, 1, 0)]
[InlineData ("0_12你", 0, 1, 0, 1)]
[InlineData ("0_12你", 1, 1, 1, 1)]
[InlineData ("0_12你", 10, 1, 10, 1)]
public void CheckBox_AbsoluteSize_Text (string text, int width, int height, int expectedWidth, int expectedHeight)
{
var checkBox = new CheckBox
{
X = 0,
Y = 0,
Width = width,
Height = height,
Text = text
};
checkBox.Layout ();
Assert.Equal (new (expectedWidth, expectedHeight), checkBox.Frame.Size);
Assert.Equal (new (expectedWidth, expectedHeight), checkBox.Viewport.Size);
Assert.Equal (new (expectedWidth, expectedHeight), checkBox.TextFormatter.ConstrainToSize);
checkBox.Dispose ();
}
[Theory]
[InlineData (0, 0, 0, 0)]
[InlineData (1, 0, 1, 0)]
[InlineData (0, 1, 0, 1)]
[InlineData (1, 1, 1, 1)]
[InlineData (10, 1, 10, 1)]
[InlineData (10, 3, 10, 3)]
public void CheckBox_AbsoluteSize_DefaultText (int width, int height, int expectedWidth, int expectedHeight)
{
var checkBox = new CheckBox
{
X = 0,
Y = 0,
Width = width,
Height = height
};
Assert.Equal (new (expectedWidth, expectedHeight), checkBox.Frame.Size);
Assert.Equal (new (expectedWidth, expectedHeight), checkBox.Viewport.Size);
Assert.Equal (new (expectedWidth, expectedHeight), checkBox.TextFormatter.ConstrainToSize);
checkBox.Dispose ();
}
// Test that Title and Text are the same
[Fact]
public void Text_Mirrors_Title ()
{
var view = new CheckBox ();
view.Title = "Hello";
Assert.Equal ("Hello", view.Title);
Assert.Equal ("Hello", view.TitleTextFormatter.Text);
Assert.Equal ("Hello", view.Text);
Assert.Equal ($"{Glyphs.CheckStateUnChecked} Hello", view.TextFormatter.Text);
}
[Fact]
public void Title_Mirrors_Text ()
{
var view = new CheckBox ();
view.Text = "Hello";
Assert.Equal ("Hello", view.Text);
Assert.Equal ($"{Glyphs.CheckStateUnChecked} Hello", view.TextFormatter.Text);
Assert.Equal ("Hello", view.Title);
Assert.Equal ("Hello", view.TitleTextFormatter.Text);
}
[Fact]
public void Constructors_Defaults ()
{
var ckb = new CheckBox ();
Assert.True (ckb.Width is DimAuto);
Assert.True (ckb.Height is DimAuto);
ckb.Layout ();
Assert.Equal (CheckState.UnChecked, ckb.CheckedState);
Assert.False (ckb.AllowCheckStateNone);
Assert.Equal (string.Empty, ckb.Text);
Assert.Equal ($"{Glyphs.CheckStateUnChecked} ", ckb.TextFormatter.Text);
Assert.True (ckb.CanFocus);
Assert.Equal (new (0, 0, 2, 1), ckb.Frame);
ckb = new () { Text = "Test", CheckedState = CheckState.Checked };
Assert.True (ckb.Width is DimAuto);
Assert.True (ckb.Height is DimAuto);
ckb.Layout ();
Assert.Equal (CheckState.Checked, ckb.CheckedState);
Assert.False (ckb.AllowCheckStateNone);
Assert.Equal ("Test", ckb.Text);
Assert.Equal ($"{Glyphs.CheckStateChecked} Test", ckb.TextFormatter.Text);
Assert.True (ckb.CanFocus);
Assert.Equal (new (0, 0, 6, 1), ckb.Frame);
ckb = new () { Text = "Test", X = 1, Y = 2 };
Assert.True (ckb.Width is DimAuto);
Assert.True (ckb.Height is DimAuto);
ckb.Layout ();
Assert.Equal (CheckState.UnChecked, ckb.CheckedState);
Assert.False (ckb.AllowCheckStateNone);
Assert.Equal ("Test", ckb.Text);
Assert.Equal ($"{Glyphs.CheckStateUnChecked} Test", ckb.TextFormatter.Text);
Assert.True (ckb.CanFocus);
Assert.Equal (new (1, 2, 6, 1), ckb.Frame);
ckb = new () { Text = "Test", X = 3, Y = 4, CheckedState = CheckState.Checked };
Assert.True (ckb.Width is DimAuto);
Assert.True (ckb.Height is DimAuto);
ckb.Layout ();
Assert.Equal (CheckState.Checked, ckb.CheckedState);
Assert.False (ckb.AllowCheckStateNone);
Assert.Equal ("Test", ckb.Text);
Assert.Equal ($"{Glyphs.CheckStateChecked} Test", ckb.TextFormatter.Text);
Assert.True (ckb.CanFocus);
Assert.Equal (new (3, 4, 6, 1), ckb.Frame);
}
[Fact]
public void Accept_Cancel_Event_OnAccept_Returns_True ()
{
var ckb = new CheckBox ();
var acceptInvoked = false;
ckb.Accepting += ViewOnAccept;
bool? ret = ckb.InvokeCommand (Command.Accept);
Assert.True (ret);
Assert.True (acceptInvoked);
return;
void ViewOnAccept (object sender, CommandEventArgs e)
{
acceptInvoked = true;
e.Handled = true;
}
}
[Fact]
public void AllowCheckStateNone_Get_Set ()
{
var checkBox = new CheckBox { Text = "Check this out 你" };
checkBox.HasFocus = true;
Assert.True (checkBox.HasFocus);
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
// Select with keyboard
Assert.True (checkBox.NewKeyDownEvent (Key.Space));
Assert.Equal (CheckState.Checked, checkBox.CheckedState);
// Select with mouse
Assert.True (checkBox.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
checkBox.AllowCheckStateNone = true;
Assert.True (checkBox.NewKeyDownEvent (Key.Space));
Assert.Equal (CheckState.None, checkBox.CheckedState);
checkBox.AllowCheckStateNone = false;
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
}
[Fact]
public void Mouse_Click_Selects ()
{
var checkBox = new CheckBox { Text = "_Checkbox" };
Assert.True (checkBox.CanFocus);
var checkedStateChangingCount = 0;
checkBox.CheckedStateChanging += (s, e) => checkedStateChangingCount++;
var selectCount = 0;
checkBox.Selecting += (s, e) => selectCount++;
var acceptCount = 0;
checkBox.Accepting += (s, e) => acceptCount++;
checkBox.HasFocus = true;
Assert.True (checkBox.HasFocus);
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
Assert.Equal (0, checkedStateChangingCount);
Assert.Equal (0, selectCount);
Assert.Equal (0, acceptCount);
Assert.True (checkBox.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
Assert.Equal (CheckState.Checked, checkBox.CheckedState);
Assert.Equal (1, checkedStateChangingCount);
Assert.Equal (1, selectCount);
Assert.Equal (0, acceptCount);
Assert.True (checkBox.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
Assert.Equal (2, checkedStateChangingCount);
Assert.Equal (2, selectCount);
Assert.Equal (0, acceptCount);
checkBox.AllowCheckStateNone = true;
Assert.True (checkBox.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
Assert.Equal (CheckState.None, checkBox.CheckedState);
Assert.Equal (3, checkedStateChangingCount);
Assert.Equal (3, selectCount);
Assert.Equal (0, acceptCount);
}
[Fact]
public void Mouse_DoubleClick_Accepts ()
{
var checkBox = new CheckBox { Text = "_Checkbox" };
Assert.True (checkBox.CanFocus);
var checkedStateChangingCount = 0;
checkBox.CheckedStateChanging += (s, e) => checkedStateChangingCount++;
var selectCount = 0;
checkBox.Selecting += (s, e) => selectCount++;
var acceptCount = 0;
checkBox.Accepting += (s, e) =>
{
acceptCount++;
e.Handled = true;
};
checkBox.HasFocus = true;
Assert.True (checkBox.HasFocus);
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
Assert.Equal (0, checkedStateChangingCount);
Assert.Equal (0, selectCount);
Assert.Equal (0, acceptCount);
Assert.True (checkBox.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked }));
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
Assert.Equal (0, checkedStateChangingCount);
Assert.Equal (0, selectCount);
Assert.Equal (1, acceptCount);
}
}

View File

@@ -0,0 +1,40 @@
namespace Terminal.Gui.ViewsTests;
/// <summary>
/// Pure unit tests for <see cref="ColorPicker"/> that don't require Application.Driver or View context.
/// These tests can run in parallel without interference.
/// </summary>
public class ColorPickerTests : UnitTests.Parallelizable.ParallelizableBase
{
[Fact]
public void ColorPicker_ChangedEvent_Fires ()
{
Color newColor = default;
var count = 0;
var cp = new ColorPicker ();
cp.ColorChanged += (s, e) =>
{
count++;
newColor = e.Result;
Assert.Equal (cp.SelectedColor, e.Result);
};
cp.SelectedColor = new (1, 2, 3);
Assert.Equal (1, count);
Assert.Equal (new (1, 2, 3), newColor);
cp.SelectedColor = new (2, 3, 4);
Assert.Equal (2, count);
Assert.Equal (new (2, 3, 4), newColor);
// Set to same value
cp.SelectedColor = new (2, 3, 4);
// Should have no effect
Assert.Equal (2, count);
}
}

View File

@@ -0,0 +1,66 @@
using System.Globalization;
namespace Terminal.Gui.ViewsTests;
/// <summary>
/// Pure unit tests for <see cref="DatePicker"/> that don't require Application.Driver or View context.
/// These tests can run in parallel without interference.
/// </summary>
public class DatePickerTests : UnitTests.Parallelizable.ParallelizableBase
{
[Fact]
public void DatePicker_ChangingCultureChangesFormat ()
{
var date = new DateTime (2000, 7, 23);
var datePicker = new DatePicker (date);
datePicker.Culture = CultureInfo.GetCultureInfo ("en-GB");
Assert.Equal ("23/07/2000", datePicker.Text);
datePicker.Culture = CultureInfo.GetCultureInfo ("pl-PL");
Assert.Equal ("23.07.2000", datePicker.Text);
// Deafult date format for en-US is M/d/yyyy but we are using StandardizeDateFormat method
// to convert it to the format that has 2 digits for month and day.
datePicker.Culture = CultureInfo.GetCultureInfo ("en-US");
Assert.Equal ("07/23/2000", datePicker.Text);
}
[Fact]
public void DatePicker_Default_Constructor_ShouldSetCurrenDate ()
{
var datePicker = new DatePicker ();
Assert.Equal (DateTime.Now.Date.Day, datePicker.Date.Day);
Assert.Equal (DateTime.Now.Date.Month, datePicker.Date.Month);
Assert.Equal (DateTime.Now.Date.Year, datePicker.Date.Year);
}
[Fact]
public void DatePicker_Constrctor_Now_ShouldSetCurrenDate ()
{
var datePicker = new DatePicker (DateTime.Now);
Assert.Equal (DateTime.Now.Date.Day, datePicker.Date.Day);
Assert.Equal (DateTime.Now.Date.Month, datePicker.Date.Month);
Assert.Equal (DateTime.Now.Date.Year, datePicker.Date.Year);
}
[Fact]
public void DatePicker_X_Y_Init ()
{
var datePicker = new DatePicker { Y = Pos.Center (), X = Pos.Center () };
Assert.Equal (DateTime.Now.Date.Day, datePicker.Date.Day);
Assert.Equal (DateTime.Now.Date.Month, datePicker.Date.Month);
Assert.Equal (DateTime.Now.Date.Year, datePicker.Date.Year);
}
[Fact]
public void DatePicker_SetDate_ShouldChangeText ()
{
var datePicker = new DatePicker { Culture = CultureInfo.GetCultureInfo ("en-GB") };
var newDate = new DateTime (2024, 1, 15);
string format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
datePicker.Date = newDate;
Assert.Equal (newDate.ToString (format), datePicker.Text);
}
}

View File

@@ -0,0 +1,155 @@
namespace Terminal.Gui.ViewsTests;
/// <summary>
/// Pure unit tests for <see cref="Label"/> that don't require Application.Driver or Application context.
/// These tests can run in parallel without interference.
/// </summary>
public class LabelTests : UnitTests.Parallelizable.ParallelizableBase
{
[Fact]
public void Text_Mirrors_Title ()
{
var label = new Label ();
label.Title = "Hello";
Assert.Equal ("Hello", label.Title);
Assert.Equal ("Hello", label.TitleTextFormatter.Text);
Assert.Equal ("Hello", label.Text);
Assert.Equal ("Hello", label.TextFormatter.Text);
}
[Fact]
public void Title_Mirrors_Text ()
{
var label = new Label ();
label.Text = "Hello";
Assert.Equal ("Hello", label.Text);
Assert.Equal ("Hello", label.TextFormatter.Text);
Assert.Equal ("Hello", label.Title);
Assert.Equal ("Hello", label.TitleTextFormatter.Text);
}
[Theory]
[CombinatorialData]
public void HotKey_Command_SetsFocus_OnNextSubView (bool hasHotKey)
{
var superView = new View { CanFocus = true };
var label = new Label ();
label.HotKey = hasHotKey ? Key.A.WithAlt : Key.Empty;
var nextSubView = new View { CanFocus = true };
superView.Add (label, nextSubView);
superView.BeginInit ();
superView.EndInit ();
Assert.False (label.HasFocus);
Assert.False (nextSubView.HasFocus);
label.InvokeCommand (Command.HotKey);
Assert.False (label.HasFocus);
Assert.Equal (hasHotKey, nextSubView.HasFocus);
}
[Theory]
[CombinatorialData]
public void MouseClick_SetsFocus_OnNextSubView (bool hasHotKey)
{
var superView = new View { CanFocus = true, Height = 1, Width = 15 };
var focusedView = new View { CanFocus = true, Width = 1, Height = 1 };
var label = new Label { X = 2 };
label.HotKey = hasHotKey ? Key.X.WithAlt : Key.Empty;
var nextSubView = new View { CanFocus = true, X = 4, Width = 4, Height = 1 };
superView.Add (focusedView, label, nextSubView);
superView.BeginInit ();
superView.EndInit ();
Assert.False (focusedView.HasFocus);
Assert.False (label.HasFocus);
Assert.False (nextSubView.HasFocus);
label.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked });
Assert.False (label.HasFocus);
Assert.Equal (hasHotKey, nextSubView.HasFocus);
}
[Fact]
public void HotKey_Command_Does_Not_Accept ()
{
var label = new Label ();
var accepted = false;
label.Accepting += LabelOnAccept;
label.InvokeCommand (Command.HotKey);
Assert.False (accepted);
return;
void LabelOnAccept (object sender, CommandEventArgs e) { accepted = true; }
}
[Fact]
public void Constructors_Defaults ()
{
var label = new Label ();
Assert.Equal (string.Empty, label.Text);
Assert.Equal (Alignment.Start, label.TextAlignment);
Assert.False (label.CanFocus);
Assert.Equal (new (0, 0, 0, 0), label.Frame);
Assert.Equal (KeyCode.Null, label.HotKey);
}
[Fact]
public void Label_HotKeyChanged_EventFires ()
{
var label = new Label ();
var fired = false;
Key oldKey = Key.Empty;
Key newKey = Key.Empty;
label.HotKeyChanged += (s, e) =>
{
fired = true;
oldKey = e.OldKey;
newKey = e.NewKey;
};
label.HotKey = Key.A.WithAlt;
Assert.True (fired);
Assert.Equal (Key.Empty, oldKey);
Assert.Equal (Key.A.WithAlt, newKey);
}
[Fact]
public void Label_HotKeyChanged_EventFires_WithNone ()
{
var label = new Label { HotKey = Key.A.WithAlt };
var fired = false;
Key oldKey = Key.Empty;
Key newKey = Key.Empty;
label.HotKeyChanged += (s, e) =>
{
fired = true;
oldKey = e.OldKey;
newKey = e.NewKey;
};
label.HotKey = Key.Empty;
Assert.True (fired);
Assert.Equal (Key.A.WithAlt, oldKey);
Assert.Equal (Key.Empty, newKey);
}
[Fact]
public void TestAssignTextToLabel ()
{
var label = new Label ();
label.Text = "Test";
Assert.Equal ("Test", label.Text);
}
}