BarredEwe / Prefire

πŸ”₯ A library based on SwiftUI Preview, for easy generation: Playbook view, Snapshot and Accessibility tests
Apache License 2.0
290 stars 18 forks source link

#Preview macro breaks the PreviewTests.generated file #47

Closed ynnckcmprnl closed 6 months ago

ynnckcmprnl commented 6 months ago

Context πŸ•΅οΈβ€β™€οΈ

I'm integrating Prefire 2.1.1 in a SwiftUI Design System package.

What 🌱

The #Preview macro causes the PreviewTests.generated file to contain compile errors. It seems that in some cases the #Preview macro breaks the generated code. Error: Expected '(' in argument list of function declaration.

When I remove the #Preview macro and create the SwiftUI preview with the old method, the compiler will fail on another one of these errors, but for a different preview. It seems like Prefires parsing of the #Preview macro is incorrectly adding a curly brace to the name in the generated code.

Example #Preview:

#Preview {
    VStack {
        Redacted("Hello, World!", .style1, .color1)
    }
}

The generated code in PreviewTests.generated, note the VStack{ reference in the name.

func test_VStack{_Preview() {
    let preview = {
        VStack {
            Redacted("Hello, World!", .style1, .color1)
        }
    }

    if let failure = assertSnapshots(matching: AnyView(preview()), name: "VStack{", isScreen: true, device: deviceConfig) {
        XCTFail(failure)
    }
}
BarredEwe commented 6 months ago

Thanks for starting the issue!
As a quick fix, I can suggest adding a name to the #Preview.

For example:

#Preview("Redacted") {
    VStack {
        Redacted("Hello, World!", .style1, .color1)
    }
}

PS: I am currently making a Pull Request with a fix)

BarredEwe commented 6 months ago

A new version with a fix has been released: https://github.com/BarredEwe/Prefire/issues/47

ynnckcmprnl commented 6 months ago

Thanks! This resolves the curly brace issue.

Although I've ran into another problem: https://github.com/BarredEwe/Prefire/issues/49