Utilities.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Charts
  2. import SnapshotTesting
  3. import UIKit
  4. import XCTest
  5. private enum Snapshot {
  6. static func identifier(_ size: CGSize) -> String {
  7. #if os(tvOS)
  8. let identifier = "tvOS"
  9. #elseif os(iOS)
  10. let identifier = "iOS"
  11. #elseif os(OSX)
  12. let identifier = "macOS"
  13. #else
  14. let identifier = ""
  15. #endif
  16. return "\(identifier)_\(size.width)_\(size.height)"
  17. }
  18. }
  19. func assertChartSnapshot<Value: ChartViewBase>(
  20. matching value: @autoclosure () throws -> Value,
  21. record recording: Bool = false,
  22. timeout: TimeInterval = 5,
  23. file: StaticString = #file,
  24. testName: String = #function,
  25. line: UInt = #line
  26. ) {
  27. let fileUrl = URL(fileURLWithPath: "\(file)", isDirectory: false)
  28. let fileName = fileUrl.deletingPathExtension().lastPathComponent
  29. #if arch(arm64)
  30. let snapshotDirectory = fileUrl.deletingLastPathComponent()
  31. .appendingPathComponent("__Snapshots__AppleSilicon__")
  32. .appendingPathComponent(fileName)
  33. .path
  34. #else
  35. let snapshotDirectory = fileUrl.deletingLastPathComponent()
  36. .appendingPathComponent("__Snapshots__x86__")
  37. .appendingPathComponent(fileName)
  38. .path
  39. #endif
  40. let failure = verifySnapshot(
  41. matching: try value(),
  42. as: .image,
  43. record: recording,
  44. snapshotDirectory: snapshotDirectory,
  45. timeout: timeout,
  46. file: file,
  47. testName: testName + Snapshot.identifier(UIScreen.main.bounds.size)
  48. )
  49. guard let message = failure else { return }
  50. XCTFail(message, file: file, line: line)
  51. }