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

Add code coverage for ToolStripSplitButton #12238

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Zheng-Li01
Copy link
Member

@Zheng-Li01 Zheng-Li01 commented Sep 27, 2024

related #10453

Proposed changes
Add unit tests for ToolStripSplitButton.cs to test its properties & methods & events

Microsoft Reviewers: Open in CodeFlow

Copy link

codecov bot commented Sep 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.43926%. Comparing base (0d0e1c9) to head (554ff00).
Report is 11 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #12238         +/-   ##
===================================================
+ Coverage   75.42712%   75.43926%   +0.01214%     
===================================================
  Files           3102        3102                 
  Lines         634244      634472        +228     
  Branches       46866       46880         +14     
===================================================
+ Hits          478392      478641        +249     
+ Misses        152434      152417         -17     
+ Partials        3418        3414          -4     
Flag Coverage Δ
Debug 75.43926% <100.00000%> (+0.01214%) ⬆️
integration 17.98261% <ø> (+0.00303%) ⬆️
production 48.81450% <ø> (+0.01080%) ⬆️
test 97.02802% <100.00000%> (+0.00177%) ⬆️
unit 45.81943% <ø> (-0.01645%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@Zheng-Li01 Zheng-Li01 added the waiting-review This item is waiting on review by one or more members of team label Sep 27, 2024
Comment on lines 88 to 93
public void ToolStripSplitButton_Ctor_ImageAndNullImage()
{
using Bitmap image = new(10, 10);
_toolStripSplitButton.Image = image;
_toolStripSplitButton.Image.Should().Be(image);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the target of the test is the constructor, you must ensure that the corresponding constructor is called. From the current test, only the Image property is tested, Other test cases also have this problem.
so this test here should like following,

using Bitmap image1 = new Bitmap(10, 10);
_toolStripSplitButton = new("Test", image);

_toolStripSplitButton.SupportsSpaceKey.Should().BeTrue();
_toolStripSplitButton.Image.Should().Be(image);

int defaultDropDownButtonWidth = _toolStripSplitButton.TestAccessor().Dynamic.DefaultDropDownButtonWidth;
_toolStripSplitButton.DropDownButtonWidth.Should().Be(defaultDropDownButtonWidth);

_toolStripSplitButton = new("Test", image: null);
_toolStripSplitButton.Image = null;
_toolStripSplitButton.Image.Should().BeNull();
_toolStripSplitButton.DropDownButtonWidth.Should().Be(defaultDropDownButtonWidth);

The above example needs to be optimized

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the target of the test is the constructor, you must ensure that the corresponding constructor is called.

This test is testing constructor with 2 arguments, not the default one that you call on line 16 in the test class constructor.
In this case we want to dispose button allocated on line 16 and then initialize that field using constructor that takes 2 arguments

_toolStripSplitButton.Dispose();
_toolStripSplitButton = new("Test", image);

In other tests, that validate properties, we are guaranteed that the default constructor is called before the Fact runs. There is no need to execute it twice in other Facts.

@dotnet-policy-service dotnet-policy-service bot added the 📭 waiting-author-feedback The team requires more information from the author label Sep 27, 2024
ricardobossan

This comment was marked as duplicate.

@ricardobossan ricardobossan removed the waiting-review This item is waiting on review by one or more members of team label Sep 27, 2024
@Zheng-Li01 Zheng-Li01 added waiting-review This item is waiting on review by one or more members of team and removed 📭 waiting-author-feedback The team requires more information from the author labels Sep 29, 2024
@Epica3055 Epica3055 added 📭 waiting-author-feedback The team requires more information from the author and removed waiting-review This item is waiting on review by one or more members of team labels Sep 29, 2024
@dotnet-policy-service dotnet-policy-service bot removed the 📭 waiting-author-feedback The team requires more information from the author label Sep 30, 2024
@Zheng-Li01 Zheng-Li01 added the waiting-review This item is waiting on review by one or more members of team label Sep 30, 2024
Copy link
Member

@LeafShi1 LeafShi1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a little question

public void Dispose() => _toolStripSplitButton.Dispose();

public static TheoryData<ToolStripItem?> ToolStripItem_Set_TestData() => new()
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space

{
using ToolStripSplitButton toolStripSplitButton = new();

toolStripSplitButton.Text.Should().BeEmpty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
toolStripSplitButton.Text.Should().BeEmpty();
_toolStripSplitButton.Text.Should().BeEmpty();

I don't think you have to invoke constructor because we are guaranteed that it is called, we can test properties of our field.

Comment on lines 88 to 93
public void ToolStripSplitButton_Ctor_ImageAndNullImage()
{
using Bitmap image = new(10, 10);
_toolStripSplitButton.Image = image;
_toolStripSplitButton.Image.Should().Be(image);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the target of the test is the constructor, you must ensure that the corresponding constructor is called.

This test is testing constructor with 2 arguments, not the default one that you call on line 16 in the test class constructor.
In this case we want to dispose button allocated on line 16 and then initialize that field using constructor that takes 2 arguments

_toolStripSplitButton.Dispose();
_toolStripSplitButton = new("Test", image);

In other tests, that validate properties, we are guaranteed that the default constructor is called before the Fact runs. There is no need to execute it twice in other Facts.

@Tanya-Solyanik Tanya-Solyanik added 📬 waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s) 📭 waiting-author-feedback The team requires more information from the author and removed waiting-review This item is waiting on review by one or more members of team 📬 waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s) labels Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📭 waiting-author-feedback The team requires more information from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants