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

Fix code generation for methods with default arguments #35

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

HugoX104447
Copy link
Contributor

Context

This pull request addresses an issue in the code generation process for methods with default arguments. The current implementation does not correctly handle default values for parameters, leading to compilation errors.
These changes ensure that the code generator correctly handles default values for a wide range of parameter types.

Changes Made

  • Refactored GetParametersWithTypesAndModifiers Method:

    • Added a wrapInArg parameter to indicate whether the default values should be wrapped in Arg<T>.
  • Added GetDefaultValueForType Method:

    • This method handles various primitive types, tuples, and other value types.
    • It returns the appropriate default value based on the type and the explicit default value.
  • Added GetEnumDefaultValueString and FindEnumMember Methods:

    • These methods introduce special handling for enums to convert raw values to their corresponding enum members.
    • GetEnumDefaultValueString uses a switch expression to handle different enum underlying types.
    • FindEnumMember uses the Roslyn Semantic Model to iterate over the enum members (fields) and check their constant values, comparing them to the parameters ExplicitDefaultValue.
  • Updated Unit Tests:

    • Added a new test class DefaultArgTests to validate the behavior of the refactored code.
    • Included tests for methods with various default argument types, such as primitives, nullables, strings, enums, and multiple defaults.

@connorivy connorivy changed the base branch from main to dev January 23, 2025 21:34
@connorivy connorivy merged commit 85e8b2d into connorivy:dev Jan 23, 2025
2 of 3 checks passed
connorivy added a commit that referenced this pull request Jan 23, 2025
* Fix code generation for methods with default arguments

* Fix code generation for interfaces inheriting from other interfaces (#36)

- Update the code generator to properly recognize and process inherited
  interfaces.
- Add unit test to validate the changes.

* use unified interface and run formatter

* Fix code generation for methods with default arguments (#35)

* move default argument tests to shared project

* Fix code generation for interfaces with overloaded methods (#38)

* Fix code generation for interfaces inheriting from other interfaces

- Update the code generator to properly recognize and process inherited
  interfaces.
- Add unit test to validate the changes.

* Fix code generation for interfaces with overloaded methods

---------

Co-authored-by: Karsten Heimrich <[email protected]>
Co-authored-by: Karsten Heimrich <[email protected]>
@connorivy
Copy link
Owner

Hey @HugoX104447,

This is a fantastic PR, it is thoroughly tested, and your code is easy to follow. Thanks so much. I merged this in and made a new release, 1.1.0 (with all of you other PRs).

I did end up making one change which was to move the test that you added from the 'MockMe.Tests' project to the 'MockMe.Tests.Overloads' shared project. The difference is that the latter project is run for multiple types of mocks (interfaces, sealed classes, standard classes), so I like to put source gen related tests there.

Let me know if you have any issues with the new version!

@HugoX104447 HugoX104447 deleted the fix-default-args branch January 24, 2025 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants