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

Adds code coverage to FormatStringEditorTests #12208

Merged
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,100 @@
// 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 System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms.TestUtilities;
using Moq;

namespace System.Windows.Forms.Design.Tests;

public class FormatStringEditorTests
{
private readonly FormatStringEditor _editor = new();
private readonly Mock<IWindowsFormsEditorService> _mockEditorService;
private readonly Mock<IComponentChangeService> _mockChangeService;
private readonly Mock<IServiceProvider> _provider;
private readonly Mock<ITypeDescriptorContext> _context;
private readonly DataGridViewCellStyle _cellStyle;

public FormatStringEditorTests()
{
_mockEditorService = new();
_mockChangeService = new();
_cellStyle = new() { Format = "Initial" };

_provider = new(MockBehavior.Strict);
_provider.Setup(p => p.GetService(typeof(IWindowsFormsEditorService))).Returns(_mockEditorService.Object);
_provider.Setup(p => p.GetService(typeof(IComponentChangeService))).Returns(_mockChangeService.Object);

_context = new(MockBehavior.Strict);
_context.Setup(c => c.Instance).Returns(_cellStyle);
}

[Theory]
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetEditValueInvalidProviderTestData))]
public void EditValue_InvalidProvider_ReturnsValue(IServiceProvider provider, object value)
{
_editor.EditValue(null, provider, value).Should().Be(value);
}

[Theory]
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetITypeDescriptorContextTestData))]
public void GetEditStyle_Invoke_ReturnsModal(ITypeDescriptorContext context)
{
_editor.GetEditStyle(context).Should().Be(UITypeEditorEditStyle.Modal);
}

[WinFormsFact]
public void EditValue_ShowsDialogAndUpdatesValue()
{
object? result = _editor.EditValue(_context.Object, _provider.Object, "NewValue");

result.Should().Be("NewValue");
_mockEditorService.Verify(es => es.ShowDialog(It.IsAny<FormatStringDialog>()), Times.Once);
}

[WinFormsFact]
public void EditValue_CallsOnComponentChanging_Once()
{
_editor.EditValue(_context.Object, _provider.Object, "NewValue");

_mockChangeService.Verify(
cs => cs.OnComponentChanging(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["Format"]),
Times.Once);

_mockChangeService.Verify(
cs => cs.OnComponentChanging(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["NullValue"]),
Times.Once);

_mockChangeService.Verify(
cs => cs.OnComponentChanging(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["FormatProvider"]),
Times.Once);
}

[WinFormsFact]
public void EditValue_WithDirtyDialog_CallsOnComponentChanged_Once()
{
using FormatStringDialog dialog = new(_context.Object);

_editor.TestAccessor().Dynamic._formatStringDialog = dialog;
dialog.TestAccessor().Dynamic._dirty = true;

ricardobossan marked this conversation as resolved.
Show resolved Hide resolved
_editor.EditValue(_context.Object, _provider.Object, "NewValue");

_mockChangeService.Verify(
cs => cs.OnComponentChanged(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["Format"], null, null),
Times.Once);

_mockChangeService.Verify(
cs => cs.OnComponentChanged(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["NullValue"], null, null),
Times.Once);

_mockChangeService.Verify(
cs => cs.OnComponentChanged(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["FormatProvider"], null, null),
Times.Once);
}
}