From 7d887519300b04881d228ca5fa5ee3ffcecca570 Mon Sep 17 00:00:00 2001 From: v-zhgl <38325459+Zheng-Li01@users.noreply.github.com> Date: Wed, 18 Dec 2024 19:06:48 +0000 Subject: [PATCH] Add code coverage for DataGridViewCellConverter (#12622) related #10453 Add unit tests for DataGridViewCellConverter.cs to test its methods --- .../Forms/DataGridViewCellConverterTests.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewCellConverterTests.cs diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewCellConverterTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewCellConverterTests.cs new file mode 100644 index 00000000000..2e46cb78ba2 --- /dev/null +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewCellConverterTests.cs @@ -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(); + } + + [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() + .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(); + _converter.ConvertTo(context: null, CultureInfo.InvariantCulture, cell, destinationType).Should().Be(cell.ToString()); + } +}