Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
1 change: 1 addition & 0 deletions Example/.prefire.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test_configuration:
- UIKit
- Foundation
- use_grouped_snapshots: false
- record_in_dark_mode: true

playbook_configuration:
- imports:
Expand Down
38 changes: 38 additions & 0 deletions Example/Shared/Assets.xcassets/gradientEnd.colorset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x82",
"green" : "0x00",
"red" : "0x4B"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x00",
"green" : "0x80",
"red" : "0x00"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x00",
"green" : "0x80",
"red" : "0x00"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x82",
"green" : "0x00",
"red" : "0x4B"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
3 changes: 3 additions & 0 deletions Example/Shared/Examples/AuthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ struct AuthView_Preview: PreviewProvider, PrefireProvider {
Group {
AuthView(login: "", password: "")

AuthView(login: "", password: "")
.preferredColorScheme(.dark)

AuthView(login: "FireUser", password: "FirePassword")
}
.previewUserStory(.auth)
Expand Down
41 changes: 38 additions & 3 deletions Example/Shared/Examples/TestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ struct TestView: View {

var body: some View {
VStack {
LinearGradient(gradient: Gradient(colors: [.green, .indigo]), startPoint: .top, endPoint: .bottom)
LinearGradient(gradient: Gradient(
colors: [Color("gradientStart", bundle: .module),
Color("gradientEnd", bundle: .module)]),
startPoint: .top, endPoint: .bottom)
.frame(height: 200)

CircleImage()
Expand Down Expand Up @@ -74,15 +77,17 @@ struct TestViewWithoutState_Previews: PreviewProvider, PrefireProvider {

struct GreenButton_Previews: PreviewProvider, PrefireProvider {
static var previews: some View {

Button("Apply", action: {})
.foregroundColor(.black)
.foregroundColor(.dynamicForegroundColor)
.font(.title)
.padding()
.frame(maxWidth: .infinity)
.frame(height: 68)
.background(Capsule().foregroundColor(.green.opacity(0.2)))
.background(Capsule().foregroundColor(.dynamicGreen))
.previewLayout(.sizeThatFits)
.previewUserStory("Buttons")

}
}

Expand All @@ -92,3 +97,33 @@ struct GreenButton_Previews: PreviewProvider, PrefireProvider {
.snapshot(delay: 0.1, precision: 0.9)
.previewUserStory(.auth)
}

extension Color {
static var dynamicForegroundColor: Color {
Color(UIColor.dynamicForegroundColor)
}

static var dynamicGreen: Color {
Color(UIColor.dynamicGreen)
}
}

extension UIColor {
static var dynamicForegroundColor: UIColor {
UIColor(dynamicProvider: { trait in
switch trait.userInterfaceStyle {
case .dark: return .white
default: return .black
}
})
}

static var dynamicGreen: UIColor {
UIColor(dynamicProvider: { trait in
switch trait.userInterfaceStyle {
case .dark: return UIColor.green.withAlphaComponent(0.8)
default: return UIColor.green.withAlphaComponent(0.2)
}
})
}
}
4 changes: 2 additions & 2 deletions Example/Shared/Examples/UIKitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import Prefire

#Preview {
let view = UIView(frame: .init(origin: .zero, size: .init(width: 100, height: 100)))
view.backgroundColor = .red
view.backgroundColor = UIColor.dynamicGreen
return view
}

#Preview {
let viewController = UIViewController()
viewController.view.backgroundColor = .green
viewController.view.backgroundColor = .dynamicForegroundColor
return viewController
}
3 changes: 2 additions & 1 deletion PrefireExecutable/Sources/PrefireCore/PrefireGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public enum PrefireGenerator {
inlineTemplate: String,
defaultEnabled: Bool,
cacheDir: Path? = nil,
useGroupedSnapshots: Bool
useGroupedSnapshots: Bool,
recordInDarkMode: Bool
) async throws {
startTime = Date()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ import SnapshotTesting
if let failure = assertSnapshots(for: PrefireSnapshot(preview, device: preview.device?.snapshotDevice() ?? deviceConfig)) {
XCTFail(failure)
}

{% if argument.recordInDarkMode == "true" %}
if let failure = assertSnapshots(for: PrefireSnapshot(preview, device: preview.device?.snapshotDevice() ?? deviceConfig, isDark: true)) {
XCTFail(failure)
}
{% endif %}
}
}
{%- if not forloop.last %}
Expand Down Expand Up @@ -83,6 +89,13 @@ import SnapshotTesting
if let failure = assertSnapshots(for: PrefireSnapshot(preview(), name: "{{ macroModel.displayName }}", isScreen: isScreen, device: deviceConfig)) {
XCTFail(failure)
}

{% if argument.recordInDarkMode == "true" %}
if let failure = assertSnapshots(for: PrefireSnapshot(preview(), name: "{{ macroModel.displayName }}", isScreen: isScreen, device: deviceConfig, isDark: true)) {
XCTFail(failure)
}
{% endif %}

}
{%- if not forloop.last %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ enum GeneratePlaybookCommand {
inlineTemplate: try options.template?.read(.utf8) ?? EmbeddedTemplates.previewModels,
defaultEnabled: options.previewDefaultEnabled,
cacheDir: options.cacheBasePath,
useGroupedSnapshots: true // Playbooks are always grouped
useGroupedSnapshots: true, // Playbooks are always grouped
recordInDarkMode: false
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct GeneratedTestsOptions {
var imports: [String]?
var testableImports: [String]?
var useGroupedSnapshots: Bool
var recordInDarkMode: Bool

init(
target: String?,
Expand Down Expand Up @@ -57,6 +58,7 @@ struct GeneratedTestsOptions {
snapshotDevices = config?.tests.snapshotDevices
imports = config?.tests.imports
testableImports = config?.tests.testableImports
self.recordInDarkMode = config?.tests.recordInDarkMode ?? false
}
}

Expand All @@ -77,6 +79,7 @@ enum GenerateTestsCommand {
static let testableImports = "testableImports"
static let previewsMacros = "previewsMacros"
static let previewsMacrosDict = "previewsMacrosDict"
static let recordInDarkMode = "recordInDarkMode"
}

static func run(_ options: GeneratedTestsOptions) async throws {
Expand All @@ -88,7 +91,8 @@ enum GenerateTestsCommand {
inlineTemplate: try options.template?.read(.utf8) ?? EmbeddedTemplates.previewTests,
defaultEnabled: options.prefireEnabledMarker,
cacheDir: options.cacheBasePath,
useGroupedSnapshots: options.useGroupedSnapshots
useGroupedSnapshots: options.useGroupedSnapshots,
recordInDarkMode: options.recordInDarkMode
)
}

Expand All @@ -115,6 +119,7 @@ enum GenerateTestsCommand {
Keys.testableImports: options.testableImports as? NSArray,
Keys.mainTarget: options.target as? NSString,
Keys.file: snapshotOutput?.string as? NSString,
Keys.recordInDarkMode: "\(options.recordInDarkMode)" as NSString,
].filter({ $0.value != nil }) as? [String: NSObject] ?? [:]
}
}
2 changes: 2 additions & 0 deletions PrefireExecutable/Sources/prefire/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct TestsConfig {
var imports: [String]?
var testableImports: [String]?
var useGroupedSnapshots: Bool?
var recordInDarkMode: Bool?

enum CodingKeys: String, CodingKey {
case target = "target"
Expand All @@ -38,6 +39,7 @@ struct TestsConfig {
case imports = "imports"
case testableImports = "testable_imports"
case useGroupedSnapshots = "use_grouped_snapshots"
case recordInDarkMode = "record_in_dark_mode"
}
}

Expand Down
2 changes: 2 additions & 0 deletions PrefireExecutable/Sources/prefire/Config/ConfigDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ final class ConfigDecoder {
config.tests.testTargetPath = getValue(from: components.last, env: env)
case .useGroupedSnapshots:
config.tests.useGroupedSnapshots = getValue(from: components.last, env: env) == "true"
case .recordInDarkMode:
config.tests.recordInDarkMode = getValue(from: components.last, env: env) == "true"
}
}

Expand Down
15 changes: 10 additions & 5 deletions PrefireExecutable/Tests/PrefireTests/PrefireGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ final class PrefireGeneratorTests: XCTestCase {
arguments: args,
inlineTemplate: template,
defaultEnabled: true,
useGroupedSnapshots: true
useGroupedSnapshots: true,
recordInDarkMode: false
)
} catch {
XCTFail("Unexpected error thrown: \(error)")
Expand All @@ -39,7 +40,8 @@ final class PrefireGeneratorTests: XCTestCase {
inlineTemplate: template,
defaultEnabled: true,
cacheDir: cache,
useGroupedSnapshots: true
useGroupedSnapshots: true,
recordInDarkMode: false
)

let result = try output.read(.utf8)
Expand All @@ -63,7 +65,8 @@ final class PrefireGeneratorTests: XCTestCase {
inlineTemplate: template,
defaultEnabled: true,
cacheDir: cache,
useGroupedSnapshots: false // Test ungrouped generation
useGroupedSnapshots: false, // Test ungrouped generation
recordInDarkMode: false
)

// Should generate file with name based on the fixture file
Expand All @@ -90,7 +93,8 @@ final class PrefireGeneratorTests: XCTestCase {
inlineTemplate: template,
defaultEnabled: true,
cacheDir: cache,
useGroupedSnapshots: true // Test grouped generation
useGroupedSnapshots: true, // Test grouped generation
recordInDarkMode: false
)

// Should generate file with Preview as class name for grouped snapshots
Expand All @@ -116,7 +120,8 @@ final class PrefireGeneratorTests: XCTestCase {
inlineTemplate: template,
defaultEnabled: true,
cacheDir: cache,
useGroupedSnapshots: false // Test ungrouped generation
useGroupedSnapshots: false, // Test ungrouped generation
recordInDarkMode: false
)

// Should generate file with source file name as class name for ungrouped snapshots
Expand Down
Loading