BarredEwe / Prefire

🔥 A library based on SwiftUI Preview, for easy generation: Playbook view, Snapshot and Accessibility tests
Apache License 2.0
251 stars 16 forks source link

Invalid redeclaration of 'test_VStack_Preview()' #49

Closed ynnckcmprnl closed 1 month ago

ynnckcmprnl commented 1 month ago

Context 🕵️‍♀️

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

What 🌱

A common approach in our previews is the use of a VStack or HStack to display multiple instantiations of a view. This comes in handy for the (small) components of our design system, being able to see several variations at once.

Example:

#Preview {
    VStack {
        Redacted(.horizontal)
        Redacted(.vertical)
    }
}

This setup causes Prefire to use the same function name (test_VStack_Preview and test_HStack_Preview) for every generated preview test which results in invalid redeclaration compile errors.

func test_VStack_Preview() {
      let preview = {
          VStack {
              ...
          }
      }

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

Setting a custom previewDisplayName or previewUserStory isn't picked up either by Prefire, which could have been a workaround although I'd rather not have to specify a custom name/story.

BarredEwe commented 1 month ago

Could you try to set a name?

#Preview("Redacted") {
    VStack {
        Redacted(.horizontal)
        Redacted(.vertical)
    }
}
ynnckcmprnl commented 1 month ago

Could you try to set a name?

That isn't picked up either, the function name remains unchanged.

BarredEwe commented 1 month ago

Could you try to set a name?

That isn't picked up either, the function name remains unchanged.

I can't reproduce it. Can you try to clear the build?

ynnckcmprnl commented 1 month ago

I can't reproduce it. Can you try to clear the build?

Indeed, I was too quick to judge, specifying a preview name is getting picked up! I think that's an acceptable method, otherwise Prefire would have to do some magic with the contents of the preview and the filename to guess the correct name.

davidnext commented 1 month ago

Please consider adding this example to the Readme. I had the same problem, and this issue helped me a lot, but it might not be easily noticeable.

BarredEwe commented 4 weeks ago

@davidnext I think a tweak can be made and use the filename as the main identifier. Then the name will be stable and you won't need to specify a custom name every time. Pretty simple modification, I'll try to upload it in the next day.

davidnext commented 4 weeks ago

Sounds great! Looking forward @BarredEwe

BarredEwe commented 2 weeks ago

@davidnext Made a new release: 2.8.0 There the difficulties with the name should go away