Skip to content

Commit

Permalink
Replaces constructor with method to initialize dependencies, as not a…
Browse files Browse the repository at this point in the history
…ll methods use them

Related dotnet#10773

## Proposed changes

- Adds code coverage to `FormatStringEditor`

## Customer Impact

- None

## Regression?

- No

## Risk

- Minimal

## Test methodology

- Unit tests

## Test environment(s)

- 9.0.100-rc.1.24452.12
  • Loading branch information
Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box) committed Sep 25, 2024
1 parent 6099c13 commit d8d237a
Showing 1 changed file with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ 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;
private Mock<IWindowsFormsEditorService>? _mockEditorService;
private Mock<IComponentChangeService>? _mockChangeService;
private Mock<IServiceProvider>? _provider;
private Mock<ITypeDescriptorContext>? _context;
private DataGridViewCellStyle? _cellStyle;

public FormatStringEditorTests()
public void ArrangeDependencies()
{
_mockEditorService = new();
_mockChangeService = new();
Expand All @@ -45,62 +45,68 @@ public void EditValue_InvalidProvider_ReturnsValue(IServiceProvider provider, ob
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetITypeDescriptorContextTestData))]
public void GetEditStyle_Invoke_ReturnsModal(ITypeDescriptorContext context)
{
_editor.GetEditStyle(context).Should().Be(UITypeEditorEditStyle.Modal);
_editor.GetEditStyle(context).Should().Be(UITypeEditorEditStyle.Modal);
}

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

object? result = _editor.EditValue(_context?.Object, _provider!.Object, "NewValue");

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

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

_editor.EditValue(_context?.Object, _provider!.Object, "NewValue");

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

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

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

[WinFormsFact]
public void EditValue_WithDirtyDialog_CallsOnComponentChanged_Once()
{
ArrangeDependencies();

dynamic? dialog = _editor.TestAccessor().Dynamic._formatStringDialog;
dialog = new FormatStringDialog(_context.Object);
dialog = new FormatStringDialog(_context?.Object);

var dialogField = typeof(FormatStringEditor).GetField("_formatStringDialog",
Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance);
dialogField?.SetValue(_editor, dialog);
dialogField?.SetValue(_editor, dialog);

var dirtyField = typeof(FormatStringDialog).GetField("_dirty",
Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance);
dirtyField?.SetValue(dialog, true);

_editor.EditValue(_context.Object, _provider.Object, "NewValue");
_editor.EditValue(_context?.Object, _provider!.Object!, "NewValue");

_mockChangeService.Verify(
cs => cs.OnComponentChanged(_cellStyle, TypeDescriptor.GetProperties(_cellStyle)["Format"], null, null),
_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),
_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),
_mockChangeService?.Verify(
cs => cs.OnComponentChanged(_cellStyle!, TypeDescriptor.GetProperties(_cellStyle!)["FormatProvider"], null, null),
Times.Once);
}
}

0 comments on commit d8d237a

Please sign in to comment.