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

Generated expansion includes unexpected newlines when using Quick #11

Open
2 tasks done
tylervick opened this issue Nov 2, 2023 · 2 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@tylervick
Copy link

Description

Thank you for creating this library! It fills a sorely-needed gap in the new compiler macro feature.

I'm writing BDD tests using the Quick framework. When recording a new assertion, I see the following result:

import MacroTesting
import Quick

@testable import MyMacroImplementation

class MacroSpec: QuickSpec {
    
    override func invokeTest() {
        withMacroTesting(macros: ["MyMacro": MyMacro.self]) {
            super.invokeTest()
        }
    }
    
    override class func spec() {
        describe("the @MyMacro macro") {
            it("should fail when annotating a non-class type") {
                assertMacro {
                    """
                    @MyMacro()
                    struct S {}
                    """
                } diagnostics: {
                    
"""
                    
@MyMacro()
                    
╰─ 🛑 'MyMacro' macro can only be applied to a class
                    
struct S {}
                    
"""
                }
            }
            
        
        }
    }
    
}

Beyond just the initial formatting (non-indented), this generated expansion will fail because of the newlines between each generated statement. This example demonstrates a generated diagnostic message, but I've also observed this for the "expansion" argument as well.

Checklist

  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

Given the above example, the generated diagnostic string should look like the following (indentation omitted):

"""
@MyMacro()
╰─ 🛑 'MyMacro' macro can only be applied to a class
struct S {}
"""

Actual behavior

The generated string contains superfluous newlines:

"""
                    
@MyMacro()
                    
╰─ 🛑 'MyMacro' macro can only be applied to a class
                    
struct S {}
                    
"""

Steps to reproduce

  1. Create a new Swift package with test dependencies on Quick and swift-macro-testing
  2. Create any valid macro and associated test class
  3. Create a test method using assertMacro to test the given macro, only populate the first closure
  4. Run the test to generate the "recording"

swift-macro-testing version information

0.2.1

Destination operating system

macOS 13

Xcode version information

15.0 (15A240d)

Swift Compiler version information

swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Target: arm64-apple-macosx13.0
@tylervick tylervick added the bug Something isn't working label Nov 2, 2023
@stephencelis
Copy link
Member

@tylervick Can you share a project that reproduces the problem? We're using MacroTesting a bunch and haven't encountered the issue, but we're also not Quick users. If you can share a project using Quick that exhibits the problem that would help us diagnose the issue and determine if it's a bug in our library, or somehow in Quick.

@scogeo
Copy link

scogeo commented Nov 19, 2023

I was able to reproduce this issue. It is a bug in how indentation was being calculated when rewriting the test source file. A PR with the fix has been submitted.

@tylervick You can take a look at the PR referenced above for details. But the quick fix is to simply delete the empty line between the class MacroSpec declaration and the invokeTest function declaration. Or make sure there are no leading spaces in that empty line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants