Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for LinkAreaEditor.LinkAreaUI #12725

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using Moq;
using System.ComponentModel.Design;

namespace System.Windows.Forms.Design.Tests;

public class LinkAreaEditorLinkAreaUITests : IDisposable
{
private readonly LinkAreaEditor.LinkAreaUI _linkAreaUI;
private readonly Mock<IHelpService> _mockHelpService;

public LinkAreaEditorLinkAreaUITests()
{
_mockHelpService = new();
_linkAreaUI = new(_mockHelpService.Object);
}

public void Dispose()
{
_linkAreaUI.Dispose();
}

[Fact]
public void LinkAreaUI_Constructor_InitializesCorrectly()
{
_linkAreaUI.Should().NotBeNull();
_linkAreaUI.Should().BeOfType<LinkAreaEditor.LinkAreaUI>();

IHelpService? helpServiceField = _linkAreaUI.TestAccessor().Dynamic._helpService;

helpServiceField.Should().Be(_mockHelpService.Object);
}

[Fact]
public void SampleText_SetGet_WorksCorrectly()
{
string sampleText = "Test Sample Text";
_linkAreaUI.SampleText = sampleText;

_linkAreaUI.SampleText.Should().Be(sampleText);
}

[Fact]
public void SampleText_Set_UpdatesSelection()
{
string testText = "Sample text for testing";
LinkArea linkArea = new(7, 4);
_linkAreaUI.Start(linkArea);

_linkAreaUI.SampleText = testText;

_linkAreaUI.SampleText.Should().Be(testText);

dynamic testAccessor = _linkAreaUI.TestAccessor().Dynamic;
int selectionStart = (int)testAccessor._sampleEdit.SelectionStart;
int selectionLength = (int)testAccessor._sampleEdit.SelectionLength;

selectionStart.Should().Be(linkArea.Start);
selectionLength.Should().Be(linkArea.Length);
}

[Fact]
public void Value_SetGet_WorksCorrectly()
{
_linkAreaUI.Start(new object());
object? initialValue = _linkAreaUI.Value;

_linkAreaUI.End();
object? finalValue = _linkAreaUI.Value;

initialValue.Should().NotBeNull();
finalValue.Should().BeNull();
}

[Fact]
public void End_SetsValueToNull()
{
_linkAreaUI.Start(new LinkArea(1, 2));

_linkAreaUI.End();

_linkAreaUI.Value.Should().BeNull();
}

[Fact]
public void Start_SetsValueAndUpdatesSelection()
{
object? testValue = new LinkArea(3, 4);
_linkAreaUI.Start(testValue);

dynamic dynamicAccessor = _linkAreaUI.TestAccessor().Dynamic;
int selectionStart = (int)dynamicAccessor._sampleEdit.SelectionStart;
int selectionLength = (int)dynamicAccessor._sampleEdit.SelectionLength;

_linkAreaUI.Value.Should().Be(testValue);

string sampleText = _linkAreaUI.SampleText;
sampleText.Length.Should().BeGreaterThanOrEqualTo(selectionStart + selectionLength);

selectionStart.Should().Be(3);
selectionLength.Should().Be(4);
}

[Fact]
public void Start_WithNullValue_DoesNotThrow()
{
Action action = () => _linkAreaUI.Start(null);

action.Should().NotThrow();
_linkAreaUI.Value.Should().BeNull();
}
}