Skip to content

Add a public function that can be used to test macro expansions #1550

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

Merged
merged 2 commits into from
Apr 24, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Apr 15, 2023

Testing macros should be as easy as possible and we should thus make the assertMacroExpansion function public.

While making the function public, I made the following changes:

  • Changed diagnosticMessages to expect DiagnsoticSpec similar to the parser tests. The only difference is that they take line and columns instead of location markers because the location markers have a slighgly high learning curve IMO.

Slightly unrelated, I also made the following changes:

  • Made a few minor improvements to SwiftParserTest/Assertions.swift
  • Fixed a bug in MacroApplication where the wrong syntax node was passed as the node parametertoaddDiagnostics`, leading to a highlight that was bigger than expected.

@ahoppen ahoppen requested review from DougGregor and bnbarham April 15, 2023 00:50
@kimdv kimdv mentioned this pull request Apr 15, 2023
@ahoppen ahoppen force-pushed the ahoppen/assert-macro branch from 7a16806 to 956ef25 Compare April 15, 2023 21:11
@ahoppen
Copy link
Member Author

ahoppen commented Apr 15, 2023

@swift-ci Please test

@ahoppen ahoppen changed the title Add a public function that can be used to test macro expansions 🚥 #1541 Add a public function that can be used to test macro expansions Apr 15, 2023
@ahoppen
Copy link
Member Author

ahoppen commented Apr 16, 2023

@swift-ci Please test macOS

@ahoppen
Copy link
Member Author

ahoppen commented Apr 16, 2023

@swift-ci Please test Windows

@ahoppen
Copy link
Member Author

ahoppen commented Apr 16, 2023

@swift-ci Please test macOS

@ahoppen ahoppen force-pushed the ahoppen/assert-macro branch from 956ef25 to d967132 Compare April 16, 2023 23:06
@ahoppen
Copy link
Member Author

ahoppen commented Apr 16, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Apr 17, 2023

@swift-ci Please test Linux

@ahoppen
Copy link
Member Author

ahoppen commented Apr 17, 2023

@swift-ci Please test Windows

@ahoppen ahoppen force-pushed the ahoppen/assert-macro branch 6 times, most recently from 462ae24 to 8d1b64d Compare April 19, 2023 22:38
@ahoppen
Copy link
Member Author

ahoppen commented Apr 19, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Apr 19, 2023

@swift-ci Please test Windows

@@ -56,6 +56,7 @@ let package = Package(
.library(name: "SwiftSyntax", type: .static, targets: ["SwiftSyntax"]),
.library(name: "SwiftSyntaxBuilder", type: .static, targets: ["SwiftSyntaxBuilder"]),
.library(name: "SwiftSyntaxMacros", type: .static, targets: ["SwiftSyntaxMacros"]),
.library(name: "SwiftSyntaxMacrosTestSupport", type: .static, targets: ["SwiftSyntaxMacrosTestSupport"]),
Copy link
Member

Choose a reason for hiding this comment

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

We should add this to the CMake build as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don’t think there’s any reason why this should be in the toolchain. So no need to add it to the CMake build…

import SwiftParser
import SwiftSyntax
import SwiftSyntaxMacros
import XCTest
Copy link
Member

Choose a reason for hiding this comment

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

... or is this why we don't have it in the CMake build?

Comment on lines 264 to 273
public func assertMacroExpansion(
_ originalSource: String,
expandedSource: String,
diagnostics: [DiagnosticSpec] = [],
macros: [String: Macro.Type],
testModuleName: String = "TestModule",
testFileName: String = "test.swift",
file: StaticString = #file,
line: UInt = #line
Copy link
Member

Choose a reason for hiding this comment

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

This is a really nice API 👍

@ahoppen ahoppen force-pushed the ahoppen/assert-macro branch from 8d1b64d to 444648f Compare April 20, 2023 19:16
@ahoppen
Copy link
Member Author

ahoppen commented Apr 21, 2023

Note to self: This requires the BasicFormat improvements I’m merging in other PRs.

@ahoppen ahoppen force-pushed the ahoppen/assert-macro branch from 444648f to 996858b Compare April 24, 2023 02:12
Testing macros should be as easy as possible and we should thus make the  `assertMacroExpansion` function public.

While making the function public, I made the following changes:
- Changed `diagnosticMessages` to expect `DiagnsoticSpec` similar to the parser tests. The only difference is that they take line and columns instead of location markers because the location markers have a slighgly high learning curve IMO.

Slightly unrelated, I also made the following changes:
- Made a few minor improvements to `SwiftParserTest/Assertions.swift`
- Fixed a bug in MacroApplication where the wrong syntax node was passed as the `node` parameter` to `addDiagnostics`, leading to a highlight that was bigger than expected.
@ahoppen ahoppen force-pushed the ahoppen/assert-macro branch from 996858b to 21654ab Compare April 24, 2023 02:14
@ahoppen ahoppen force-pushed the ahoppen/assert-macro branch from 21654ab to dd9c9e8 Compare April 24, 2023 02:20
@ahoppen
Copy link
Member Author

ahoppen commented Apr 24, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Apr 24, 2023

@swift-ci Please test Windows

@ahoppen ahoppen merged commit f34307b into swiftlang:main Apr 24, 2023
@ahoppen ahoppen deleted the ahoppen/assert-macro branch April 24, 2023 16:08

private extension String {
// This implementation is really slow; to use it outside a test it should be optimized.
func trimmingTrailingWhitespace() -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this if we've fixed BasicFormat?

)
}
if diag.notes.count != spec.notes.count {
XCTFail(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we output both notes, ie. actual and expected? Same with fix-it's below.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don’t think it’s worth outputting the expected notes because the diagnostic will point right into the source that defines them. We used to output the expected diagnostics if the count did not match and I found that verbose without giving any value.

Comment on lines +262 to +263
/// - testModuleName: The name of the test module to use.
/// - testFileName: The name of the test file name to use.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it useful to expose these? It doesn't actually matter for anything right?

Copy link
Member Author

Choose a reason for hiding this comment

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

You could use them in diagnostic messages, right?

macros: [String: Macro.Type],
testModuleName: String = "TestModule",
testFileName: String = "test.swift",
indentationWidth: Trivia = .spaces(4),
Copy link
Contributor

Choose a reason for hiding this comment

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

What are your thoughts on indentWidth vs indentationWidth?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don’t have strong feelings. Do you?

Copy link
Contributor

Choose a reason for hiding this comment

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

Define "strong". I prefer indentWidth because it's shorter. But 🤷

Copy link
Member Author

Choose a reason for hiding this comment

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

I just checked. SwiftSyntax general prefers indentation over indent, so I’d prefer to stick with indentation for consistency.

@@ -721,51 +673,56 @@ public let testMacros: [String: Macro.Type] = [
]

final class MacroSystemTests: XCTestCase {
private let indentationWidth: Trivia = .spaces(2)
Copy link
Contributor

Choose a reason for hiding this comment

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

static?

Copy link
Member Author

Choose a reason for hiding this comment

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

But then I need to access it with Self.indentationWidth which is a little to verbose for my taste.

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.

4 participants