pointfreeco/swift-snapshot-testing

Test crashes when view contains Swift Charts

hiragram opened this issue · 1 comments

Describe the bug
Test crashes view.layer.render(in: ctx.cgContext) when view contains Swift Charts

To Reproduce
Minimum reproduction code is TBD.

❌ Crashed

            Chart(data ?? [], id: \.date) { entry in
                AreaMark(
                    x: .value("Date", entry.date),
                    y: .value("Count", entry.count)
                )
                .foregroundStyle(.linearGradient(.init(colors: [.key.opacity(0.4), .key.opacity(0.05)]), startPoint: .top, endPoint: .bottom))
                .interpolationMethod(interpolationMethod)

                LineMark(
                    x: .value("Date", entry.date),
                    y: .value("Count", entry.count)
                )
                .lineStyle(.init(lineWidth: 2))
                .foregroundStyle(Color.key)
                .symbol {
                    Circle()
                        .foregroundColor(.key)
                        .frame(width: 8)
                        .overlay {
                            Circle()
                                .padding(2)
                                .foregroundColor(.background)
                        }
                }
                .interpolationMethod(interpolationMethod)
            }
            .frame(height: 200)
            .chartXAxis(.visible)
            .chartYAxis(.visible)
            .chartYScale(domain: 0 ... yAxisMax)
            .chartXAxis {
                AxisMarks(values: .stride(by: .day)) { value in
                    if value.index % (value.count / 3) == 0 {
                        AxisGridLine()
                        AxisValueLabel()
                    } else {
                        AxisGridLine()
                    }
                }
            }
            .chartOverlay { chartProxy in
                GeometryReader { geometry in
                    Rectangle().fill(.clear).contentShape(Rectangle())
                        .gesture(
                            DragGesture(minimumDistance: 0)
                                .onChanged({ value in
                                    let currentX = value.location.x - geometry[chartProxy.plotAreaFrame].origin.x
                                    if let currentDate: Date = chartProxy.value(atX: currentX) {
                                        presenter.setSelectedDate(date: currentDate)
                                    }
                                })
                        )
                }
            }

❌ Crashed even if all modifiers of Chart are removed

            Chart(data ?? [], id: \.date) { entry in
                AreaMark(
                    x: .value("Date", entry.date),
                    y: .value("Count", entry.count)
                )
                .foregroundStyle(.linearGradient(.init(colors: [.key.opacity(0.4), .key.opacity(0.05)]), startPoint: .top, endPoint: .bottom))
                .interpolationMethod(interpolationMethod)

                LineMark(
                    x: .value("Date", entry.date),
                    y: .value("Count", entry.count)
                )
                .lineStyle(.init(lineWidth: 2))
                .foregroundStyle(Color.key)
                .symbol {
                    Circle()
                        .foregroundColor(.key)
                        .frame(width: 8)
                        .overlay {
                            Circle()
                                .padding(2)
                                .foregroundColor(.background)
                        }
                }
                .interpolationMethod(interpolationMethod)
            }

❌ Crashed even if all modifiers of XXXMarks are removed.

            Chart(data ?? [], id: \.date) { entry in
                AreaMark(
                    x: .value("Date", entry.date),
                    y: .value("Count", entry.count)
                )

                LineMark(
                    x: .value("Date", entry.date),
                    y: .value("Count", entry.count)
                )
            }

✅ Did not crash if Chart contains no XXXMark

            Chart(data ?? [], id: \.date) { entry in

            }

Expected behavior
No crash

Environment

  • swift-snapshot-testing version [e.g. 1.9.0]: 1.10.0, 1.13.0, latest main(d1082c48edaee17914556517b29cea7e3388b2b7)
  • Xcode [e.g. 13.2]: 13.4.1
  • Swift [e.g. 5.5]:
  • OS: [e.g. iOS 15]: iOS 16.4, iPhone 14 Pro simulator

Additional context
Add any more context about the problem here.

Hi @hiragram, I am going to convert this to a discussion because it is not clear that it is actually a problem with the library that we could possible fix. It seems to be a problem with the Charts framework.