From a3648d177b90b6e9db9261de6497e4947b5b2909 Mon Sep 17 00:00:00 2001 From: "Leaf Shi (BEYONDSOFT CONSULTING INC)" Date: Wed, 17 Jul 2024 16:34:59 +0800 Subject: [PATCH 1/4] Add unit tests for ButtonBaseDesigner --- .../UnitTests/ButtonBaseDesignerTests.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs new file mode 100644 index 00000000000..f766908794c --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections; + +namespace System.Windows.Forms.Design.Tests; + +public class ButtonBaseDesignerTests +{ + [Fact] + public void ButtonBaseDesigner_Constructor_Initialize_AutoResizeHandles() + { + using ButtonBaseDesigner buttonBaseDesigner = new(); + bool autoResizeHandles = buttonBaseDesigner.AutoResizeHandles; + + buttonBaseDesigner.Should().NotBeNull(); + autoResizeHandles.Should().Be(true); + } + + public static IEnumerable IDictionary_TestData() + { + yield return new object[] { null }; + yield return new object[] { new Dictionary() }; + } + + [Theory] + [MemberData(nameof(IDictionary_TestData))] + public void ButtonBaseDesigner_InitializeNewComponent_WithDefaultButton(IDictionary defaultValues) + { + using ButtonBaseDesigner buttonBaseDesigner = new(); + using Button button = new(); + buttonBaseDesigner.Initialize(button); + + buttonBaseDesigner.InitializeNewComponent(defaultValues); + } + + [Theory] + [MemberData(nameof(IDictionary_TestData))] + public void ButtonBaseDesigner_InitializeNewComponent_NotInitialized(IDictionary defaultValues) + { + using ButtonBaseDesigner buttonBaseDesigner = new(); + Assert.Throws(() => buttonBaseDesigner.InitializeNewComponent(defaultValues)); + } + + [Fact] + public void ButtonBaseDesigner_SnapLinesWithDefaultButton_ShouldReturnExpectedCount() + { + using ButtonBaseDesigner buttonBaseDesigner = new(); + using Button button = new(); + buttonBaseDesigner.Initialize(button); + + buttonBaseDesigner.SnapLines.Count.Should().Be(9); + } +} From 57ac516a371d8cea19f4ee90d9699251466aea22 Mon Sep 17 00:00:00 2001 From: "Leaf Shi (BEYONDSOFT CONSULTING INC)" Date: Thu, 18 Jul 2024 11:12:17 +0800 Subject: [PATCH 2/4] enable nullability --- .../tests/UnitTests/ButtonBaseDesignerTests.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs index f766908794c..e74804ae903 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs @@ -1,11 +1,13 @@ // 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.Collections; namespace System.Windows.Forms.Design.Tests; -public class ButtonBaseDesignerTests +public sealed class ButtonBaseDesignerTests { [Fact] public void ButtonBaseDesigner_Constructor_Initialize_AutoResizeHandles() @@ -19,7 +21,7 @@ public void ButtonBaseDesigner_Constructor_Initialize_AutoResizeHandles() public static IEnumerable IDictionary_TestData() { - yield return new object[] { null }; + yield return new object[] { null! }; yield return new object[] { new Dictionary() }; } @@ -39,7 +41,8 @@ public void ButtonBaseDesigner_InitializeNewComponent_WithDefaultButton(IDiction public void ButtonBaseDesigner_InitializeNewComponent_NotInitialized(IDictionary defaultValues) { using ButtonBaseDesigner buttonBaseDesigner = new(); - Assert.Throws(() => buttonBaseDesigner.InitializeNewComponent(defaultValues)); + Action action = () => buttonBaseDesigner.InitializeNewComponent(defaultValues); + action.Should().Throw(); } [Fact] From 5f3beda5186c23c35c71e8045bf636959adeefc8 Mon Sep 17 00:00:00 2001 From: "Leaf Shi (BEYONDSOFT CONSULTING INC)" Date: Mon, 22 Jul 2024 09:50:22 +0800 Subject: [PATCH 3/4] Using ? replace of ! in IDictionary_TestData --- .../tests/UnitTests/ButtonBaseDesignerTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs index e74804ae903..13511a32f26 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs @@ -19,9 +19,9 @@ public void ButtonBaseDesigner_Constructor_Initialize_AutoResizeHandles() autoResizeHandles.Should().Be(true); } - public static IEnumerable IDictionary_TestData() + public static IEnumerable IDictionary_TestData() { - yield return new object[] { null! }; + yield return new object?[] { null }; yield return new object[] { new Dictionary() }; } From 1d6e1b518ca1f9bef2818fafbca00e96052eacba Mon Sep 17 00:00:00 2001 From: "Leaf Shi (BEYONDSOFT CONSULTING INC)" Date: Tue, 23 Jul 2024 10:08:29 +0800 Subject: [PATCH 4/4] using TheoryData in test cases --- .../UnitTests/ButtonBaseDesignerTests.cs | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs index 13511a32f26..1ba2893fbd3 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/ButtonBaseDesignerTests.cs @@ -13,35 +13,34 @@ public sealed class ButtonBaseDesignerTests public void ButtonBaseDesigner_Constructor_Initialize_AutoResizeHandles() { using ButtonBaseDesigner buttonBaseDesigner = new(); - bool autoResizeHandles = buttonBaseDesigner.AutoResizeHandles; - buttonBaseDesigner.Should().NotBeNull(); - autoResizeHandles.Should().Be(true); + buttonBaseDesigner.AutoResizeHandles.Should().Be(true); } - public static IEnumerable IDictionary_TestData() + public static TheoryData?> IDictionary_TheoryData => new() { - yield return new object?[] { null }; - yield return new object[] { new Dictionary() }; - } + null, + new Dictionary() + }; [Theory] - [MemberData(nameof(IDictionary_TestData))] - public void ButtonBaseDesigner_InitializeNewComponent_WithDefaultButton(IDictionary defaultValues) + [MemberData(nameof(IDictionary_TheoryData))] + public void ButtonBaseDesigner_InitializeNewComponent_WithDefaultButton(IDictionary? defaultValues) { using ButtonBaseDesigner buttonBaseDesigner = new(); using Button button = new(); buttonBaseDesigner.Initialize(button); - buttonBaseDesigner.InitializeNewComponent(defaultValues); + buttonBaseDesigner.InitializeNewComponent((IDictionary?)defaultValues); + Assert.False(button.IsHandleCreated); } [Theory] - [MemberData(nameof(IDictionary_TestData))] - public void ButtonBaseDesigner_InitializeNewComponent_NotInitialized(IDictionary defaultValues) + [MemberData(nameof(IDictionary_TheoryData))] + public void ButtonBaseDesigner_InitializeNewComponent_NotInitialized(IDictionary? defaultValues) { using ButtonBaseDesigner buttonBaseDesigner = new(); - Action action = () => buttonBaseDesigner.InitializeNewComponent(defaultValues); + Action action = () => buttonBaseDesigner.InitializeNewComponent((IDictionary?)defaultValues); action.Should().Throw(); }