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 unit test for ImageListCodeDomSerializer #12753

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

John-Qiao
Copy link
Member

@John-Qiao John-Qiao commented Jan 10, 2025

Related #10773

Proposed changes

  • Add unit test ImageCollectionCodeDomSerializerTests.cs for public Deserialize() method of the ImageCollectionCodeDomSerializer.
  • Enable nullability in ImageCollectionCodeDomSerializerTests.cs.
Microsoft Reviewers: Open in CodeFlow

@John-Qiao John-Qiao requested a review from a team as a code owner January 10, 2025 10:35
@ricardobossan
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.15627%. Comparing base (084e6d0) to head (3e96033).
Report is 48 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #12753         +/-   ##
===================================================
+ Coverage   76.03186%   76.15627%   +0.12441%     
===================================================
  Files           3181        3193         +12     
  Lines         639670      640294        +624     
  Branches       47215       47238         +23     
===================================================
+ Hits          486353      487624       +1271     
+ Misses        149797      149163        -634     
+ Partials        3520        3507         -13     
Flag Coverage Δ
Debug 76.15627% <100.00000%> (+0.12441%) ⬆️
integration 18.16436% <ø> (-0.01192%) ⬇️
production 50.06432% <ø> (+0.24233%) ⬆️
test 97.01938% <100.00000%> (-0.00978%) ⬇️
unit 47.24706% <ø> (+0.20415%) ⬆️

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

var baseSerializerMock = new Mock<CodeDomSerializer>();

managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer)))
.Returns(baseSerializerMock.Object);
Copy link
Member

Choose a reason for hiding this comment

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

Please use 4 spaces to indent code.

Copy link
Member

Choose a reason for hiding this comment

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

If you want to align dots, you can do this -

        managerMock
            .Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer)))
            .Returns(baseSerializerMock.Object);

@Tanya-Solyanik Tanya-Solyanik added the 📭 waiting-author-feedback The team requires more information from the author label Jan 10, 2025
[Fact]
public void Deserialize_ShouldCallBaseSerializer_WhenBaseSerializerFound()
{
var managerMock = new Mock<IDesignerSerializationManager>();
Copy link
Member

Choose a reason for hiding this comment

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

Please declare variables with their specifc types, e.g.:

Mock<IDesignerSerializationManager> managerMock = new();
Mock<CodeDomSerializer> baseSerializerMock = new();

This happens other times in this PR's code.

var baseSerializerMock = new Mock<CodeDomSerializer>();

managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer)))
.Returns(baseSerializerMock.Object);
Copy link
Member

Choose a reason for hiding this comment

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

This test doesn't correctly trigger the expected codepath to return null. The Deserialize method only returns null by chance, making the test pass incorrectly:

wrong-codepath

To ensure the test executes the intended codepath, the mock setup should return false:

right-codepath

Additionally, since the correct code path contains a Debug.Fail() call, you'll need to clear the Trace.Listeners to prevent the test from halting prematurely. Here's the corrected test code:

[Fact]
public void Deserialize_ShouldReturnNull_WhenBaseSerializerNotFound()
{
    Mock<IDesignerSerializationManager> managerMock = new();
    Mock<CodeDomSerializer> baseSerializerMock = new();

    managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer)))
             .Returns(false);

    Trace.Listeners.Clear();

    object codeObject = new();
    object? result = _serializer.Deserialize(managerMock.Object, codeObject);

    result.Should().BeNull();
}


result.Should().BeNull();
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Could increase code coverage a bit by adding a test case to the Serialize method like this:

[Fact]
public void Serialize_ShouldGenerateCodeStatements_ForImageListWithKeys()
{
    Mock<IDesignerSerializationManager> managerMock = new();
    Mock<CodeDomSerializer> baseSerializerMock = new();
    ImageList imageList = new();
    imageList.Images.Add("Key1", new Bitmap(1, 1));
    imageList.Images.Add("Key2", new Bitmap(1, 1));

    baseSerializerMock.Setup(b => b.Serialize(managerMock.Object, imageList)).Returns(new CodeStatementCollection());

    managerMock.Setup(m => m.GetSerializer(typeof(Component), typeof(CodeDomSerializer)))
             .Returns(baseSerializerMock.Object);

    ContextStack contextStack = new();
    contextStack.Append(new ImageListCodeDomSerializer());
    managerMock.Setup(m => m.Context).Returns(contextStack);

    object? result = _serializer.Serialize(managerMock.Object, imageList);
    result.Should().BeOfType<CodeStatementCollection>();
}

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.

3 participants