Skip to content

Commit

Permalink
Add code coverage for DataGridViewCellConverter (#12622)
Browse files Browse the repository at this point in the history
related #10453

Add unit tests for DataGridViewCellConverter.cs to test its methods
  • Loading branch information
Zheng-Li01 authored Dec 18, 2024
1 parent ac6adf7 commit 7d88751
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.Design.Serialization;
using System.Globalization;

namespace System.Windows.Forms.Tests;

public class DataGridViewCellConverterTests
{
private readonly DataGridViewCellConverter _converter = new();

[WinFormsTheory]
[InlineData(typeof(InstanceDescriptor), true)]
[InlineData(typeof(string), true)]
public void CanConvertTo_ReturnsExpected(Type destinationType, bool expected) =>
_converter.CanConvertTo(context: null, destinationType).Should().Be(expected);

[WinFormsFact]
public void ConvertTo_InstanceDescriptorWithDataGridViewCell_ReturnsInstanceDescriptor()
{
using DataGridViewTextBoxCell cell = new();
Type destinationType = typeof(InstanceDescriptor);

_converter.ConvertTo(context: null, CultureInfo.InvariantCulture, cell, destinationType).Should().NotBeNull();
_converter.ConvertTo(context: null, CultureInfo.InvariantCulture, cell, destinationType).Should().BeOfType<InstanceDescriptor>();
}

[WinFormsFact]
public void ConvertTo_InstanceDescriptorWithNonDataGridViewCell_ThrowsNotSupportedException()
{
object value = new();
Type destinationType = typeof(InstanceDescriptor);

Action action = () => _converter.ConvertTo(context: null, CultureInfo.InvariantCulture, value, destinationType);
action.Should().Throw<NotSupportedException>()
.WithMessage("'DataGridViewCellConverter' is unable to convert 'System.Object' to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'.");
}

[WinFormsFact]
public void ConvertTo_OtherType_ReturnsStringRepresentation()
{
using DataGridViewTextBoxCell cell = new();
Type destinationType = typeof(string);

_converter.ConvertTo(context: null, CultureInfo.InvariantCulture, cell, destinationType).Should().NotBeNull();
_converter.ConvertTo(context: null, CultureInfo.InvariantCulture, cell, destinationType).Should().BeOfType<string>();
_converter.ConvertTo(context: null, CultureInfo.InvariantCulture, cell, destinationType).Should().Be(cell.ToString());
}
}

0 comments on commit 7d88751

Please sign in to comment.