Skip to content

Enhance assertMacroExpansion to Validate Post-Fix-It Macro Expansions #2355

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

Closed

Conversation

Matejkob
Copy link
Contributor

This PR introduces an enhancement to the assertMacroExpansion function in SwiftSyntax's testing suite. The key addition is the capability to validate macro expansions on source code after the application of Fix-Its. This change ensures a more robust and comprehensive testing process, particularly for macros that propose fix-its for diagnostics.

Modifications include:

  • Addition of a new parameter expandedFixedSource in test assertions. This parameter allows users to assert against the expanded source code after applying Fix-Its.
  • Update of assertMacroExpansion to handle the new testing workflow. It now performs macro expansion tests on both the original and the fixed source code, ensuring that both the pre-fix and post-fix states are correctly validated.
  • Refinement of the internal logic for applying Fix-Its and conducting macro expansion tests, ensuring accuracy and efficiency in the testing process.
  • Update of test cases in AddBlockerTests.swift and AddCompletionHandlerMacroTests.swift to demonstrate and validate the new functionality.

Resolves: #2333

@Matejkob Matejkob force-pushed the expandedSourceAfterApplyingFixIt branch from 6f3c6e7 to c7055b3 Compare November 13, 2023 16:37
@Matejkob
Copy link
Contributor Author

Blocked by #2329

@Matejkob Matejkob force-pushed the expandedSourceAfterApplyingFixIt branch from c7055b3 to c023cc9 Compare November 16, 2023 19:25
@Matejkob Matejkob marked this pull request as ready for review November 16, 2023 19:26
This commit introduces an enhancement to the `assertMacroExpansion` function in SwiftSyntax's testing suite. The key addition is the capability to validate macro expansions on source code after the application of Fix-Its. This change ensures a more robust and comprehensive testing process, particularly for macros that propose fix-its for diagnostics.

Modifications include:
- Addition of a new parameter `expandedFixedSource` in test assertions. This parameter allows users to assert against the expanded source code after applying Fix-Its.
- Update of `assertMacroExpansion` to handle the new testing workflow. It now performs macro expansion tests on both the original and the fixed source code, ensuring that both the pre-fix and post-fix states are correctly validated.
- Refinement of the internal logic for applying Fix-Its and conducting macro expansion tests, ensuring accuracy and efficiency in the testing process.
- Update of test cases in `AddBlockerTests.swift` and `AddCompletionHandlerMacroTests.swift` to demonstrate and validate the new functionality.

Resolves: swiftlang#2333
@Matejkob Matejkob force-pushed the expandedSourceAfterApplyingFixIt branch from c023cc9 to 9d0491a Compare November 16, 2023 19:28
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

I’m not sure how useful this is since the compiler would never automatically apply the Fix-Its and then expand the macro. IMO you should write a test case that checks the error case (and fixedSource) and then test the actual expansion in a different test case.

@Matejkob
Copy link
Contributor Author

I’m not sure how useful this is since the compiler would never automatically apply the Fix-Its and then expand the macro. IMO you should write a test case that checks the error case (and fixedSource) and then test the actual expansion in a different test case.

Thank you for your input. I understand your point about the compiler's typical behavior. However, the enhancement to assertMacroExpansion is intended to streamline testing by simulating a developer's real-world workflow. In practice, developers often apply Fix-Its and then expect macros to function correctly. This update allows for testing this entire process within a single test case, reflecting how users actually interact with macros.

By incorporating expandedFixedSource, we're not just following the compiler's process, but providing a more efficient and comprehensive testing approach. This helps in validating the macro's behavior in scenarios that developers are likely to encounter, making our testing suite more practical and robust.

@bnbarham
Copy link
Contributor

I understand the motivation here, but assertMacroExpansion is already relatively complex and adding yet another parameter to it only adds to that complexity. IMO it's generally more understandable for each test to have a focus - in this case, tests for the fix-its and other tests for the expansion of valid code. Mixing them together ends up creating giant tests that are difficult to process and likely duplicates already tested functionality.

@Matejkob
Copy link
Contributor Author

In that case I'm closing the PR and associated issue

@Matejkob Matejkob closed this Nov 20, 2023
@mbrandonw
Copy link
Contributor

mbrandonw commented Nov 20, 2023

IMO it's generally more understandable for each test to have a focus - in this case, tests for the fix-its and other tests for the expansion of valid code. Mixing them together ends up creating giant tests that are difficult to process and likely duplicates already tested functionality.

There are also downsides to not automatically expanding fix-its that are worth considering. It becomes very easy to assert that some fix-it is emitted but forget to test how the fix-it applies to the source code. In fact, Apple's recent swift-mmio makes this mistake a number of times where it is never asserted how the fix-it applies.

So, while the test size can increase with the automatic application of fix-its, it does make the test more correct and less chance of forgetting something. And in our experience, it's still possible to keep the test size down to a reasonable size, even for complex macros (here's an example).

@Matejkob
Copy link
Contributor Author

There are also downsides to not automatically expanding fix-its that are worth considering. It becomes very easy to assert that some fix-it is emitted but forget how the fix-it applies to the source code. In fact, Apple's recent swift-mmio makes this mistake a number of times where it is never asserted how the fix-it applies.

It would be beneficial if the application of fix-its were covered in those test cases. However, if I'm not mistaken, the current official version of swift-syntax does not include the feature to assert fix-it applications in the assert macro function. 😟

@ahoppen
Copy link
Member

ahoppen commented Nov 21, 2023

It would be beneficial if the application of fix-its were covered in those test cases. However, if I'm not mistaken, the current official version of swift-syntax does not include the feature to assert fix-it applications in the assert macro function. 😟

FWIW @kimdv added the capability to test the fixed source in #2021 but it’s not part of a swift-syntax release yet.

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.

Add a possibility to validate fixedSource post macro expansion in the assertMacroExpansion
4 participants