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

Invalid redeclaration of 'test_VStack_Preview()' #49

Closed ynnckcmprnl closed 6 months ago

ynnckcmprnl commented 6 months 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 6 months ago

Could you try to set a name?

#Preview("Redacted") {
    VStack {
        Redacted(.horizontal)
        Redacted(.vertical)
    }
}
ynnckcmprnl commented 6 months ago

Could you try to set a name?

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

BarredEwe commented 6 months 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 6 months 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 5 months 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 5 months 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 5 months ago

Sounds great! Looking forward @BarredEwe

BarredEwe commented 4 months ago

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