### Run Swift Fixtures Client Example Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Execute the example client application to see the Swift Fixtures library in action. This demonstrates practical usage. ```bash swift run FixturesClient ``` -------------------------------- ### Build Swift Package Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Use this command to build the Swift package. Ensure you have the Swift toolchain installed. ```bash swift build ``` -------------------------------- ### FixtureMacro Implementation Example Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Handles the generation of fixture implementations for structs and enums. This code is part of the compiler plugin. ```swift func makeFixtureMacro(of declaration: some DeclSyntaxProtocol) throws -> [DeclSyntax] { guard let extensionDecl = declaration.as(ExtensionDeclSyntax.self) else { return [] } return try makeFixtureExtension(of: extensionDecl) } ``` -------------------------------- ### Builder for Clear Test Intent Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Using the builder pattern with fixtures makes test setup self-documenting by clearly indicating the properties being customized for specific test scenarios. ```swift func testDiscountCalculation() { let regularProduct = Product.fixture { $0.price = 100.0 $0.category = "Regular" } let premiumProduct = Product.fixture { $0.price = 100.0 $0.category = "Premium" } // Test logic is clear about what differs between products } ``` -------------------------------- ### Builder Type Safety Example Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md The FixtureBuilder enforces type safety, preventing compilation errors when attempting to assign incorrect types or non-existent properties. ```swift Product.fixture { $0.name = "Test Product" // ✅ Valid $0.price = "expensive" // ❌ Compile error - wrong type $0.invalidProperty = true // ❌ Compile error - property doesn't exist } ``` -------------------------------- ### Generate Documentation Locally Source: https://github.com/gibachan/swift-fixtures/blob/main/README.md Use the provided scripts or Swift Package Manager commands to generate local documentation for the library. This is useful for offline viewing or custom documentation builds. ```bash # Generate and open documentation ./scripts/generate-docs.sh # Or using swift-docc-plugin swift package generate-documentation --target Fixtures # Or manually with xcodebuild xcodebuild docbuild -scheme Fixtures -destination 'platform=macOS' ``` -------------------------------- ### Development Commands for swift-fixtures Source: https://github.com/gibachan/swift-fixtures/blob/main/README.md A set of make commands are available for common development tasks including building, testing, formatting, linting, cleaning, and viewing help. ```bash make build # Build project make test # Run tests make format # Format code make lint # Lint code make clean # Clean build artifacts make help # Show all commands ``` -------------------------------- ### Import Fixtures Library Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/GettingStarted.md Import the Fixtures library into your Swift files to use its features. ```swift import Fixtures ``` -------------------------------- ### Implement Manual Fixtureable Conformance Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Conform to the `Fixtureable` protocol directly when custom fixture logic is required. This allows for precise control over fixture creation. ```swift struct ComplexCalculation { let input: Double let algorithmType: String let result: Double init(input: Double, algorithmType: String) { self.input = input self.algorithmType = algorithmType self.result = performCalculation(input, algorithmType) } } extension ComplexCalculation: Fixtureable { static var fixture: Self { // Create a meaningful test case ComplexCalculation(input: 100.0, algorithmType: "fast") } } ``` -------------------------------- ### Build Complex Test Scenarios Step-by-Step Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Combine reusable fixtures and nested builders to create sophisticated test data for complex scenarios. ```swift let project = Project.fixture { $0.name = "Test Project" $0.owner = User.adminFixture $0.team = Team.fixture { $0.members = [User.fixture, User.fixture, User.adminFixture] } } ``` -------------------------------- ### Add Fixtures to Package.swift Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/GettingStarted.md Add the Fixtures library as a dependency in your Package.swift file. Ensure you specify the correct version. ```swift dependencies: [ .package(url: "https://github.com/gibachan/swift-fixtures", from: "0.4.0") ] ``` -------------------------------- ### Run All Swift Tests Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Execute all tests within the Swift package. This is useful for verifying the integrity of the library and its components. ```bash swift test ``` -------------------------------- ### Custom Builder Extensions for Convenience Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Extend the generated FixtureBuilder with static methods to create pre-configured fixtures, simplifying common customization patterns. ```swift extension Product { static func premiumFixture(_ configure: (inout FixtureBuilder) -> Void = { _ in }) -> Product { Product.fixture { // Set premium defaults $0.category = "Premium" $0.price = 500.0 $0.inStock = true // Apply additional customizations configure(&$0) } } static func budgetFixture(_ configure: (inout FixtureBuilder) -> Void = { _ in }) -> Product { Product.fixture { $0.category = "Budget" $0.price = 50.0 configure(&$0) } } } // Usage let customPremium = Product.premiumFixture { $0.name = "Custom Premium Product" } let customBudget = Product.budgetFixture { $0.name = "Custom Budget Product" } ``` -------------------------------- ### Add swift-fixtures to Swift Package Manager Source: https://github.com/gibachan/swift-fixtures/blob/main/README.md Add the swift-fixtures package to your project's dependencies in Package.swift. Ensure the 'Fixtures' product is linked to your target. ```swift // Package.swift dependencies: [ .package(url: "https://github.com/gibachan/swift-fixtures.git", from: "0.4.0") ], targets: [ .target( name: "YourTarget", dependencies: [ .product(name: "Fixtures", package: "swift-fixtures") ] ) ] ``` -------------------------------- ### Add Fixtures to Target Dependencies Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/GettingStarted.md Include the Fixtures library as a product dependency for your target in Package.swift. ```swift .target( name: "YourTarget", dependencies: [ .product(name: "Fixtures", package: "swift-fixtures") ] ) ``` -------------------------------- ### Use Builders for Edge Cases Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Define specific fixture variations to test boundary conditions and edge cases, such as negative balances or zero values. ```swift extension BankAccount { static var overdraftFixture: BankAccount { BankAccount.fixture { $0.balance = -100.0 // Negative balance $0.overdraftLimit = 500.0 $0.isActive = true } } static var zeroBalanceFixture: BankAccount { BankAccount.fixture { $0.balance = 0.0 } } } ``` -------------------------------- ### Struct Fixture Generation - Initializer Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Generates an `init(fixture...:)` initializer for structs. This allows creating instances with fixture-prefixed parameters. ```swift let initializer = InitializerDeclSyntax( "init(\(parameters.map { "fixture\($0.name): \($0.type.trimmed)" }.joined(separator: ", ")))" ) { // Initializer body } ``` -------------------------------- ### Macro Expansion Verification Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Uses `assertMacroExpansion` from `SwiftSyntaxMacrosTestSupport` to verify macro expansions. This is crucial for testing macro logic. ```swift assertMacroExpansion( """@Fixture struct MyStruct { let value: Int }""", expandedSource: """ struct MyStruct { let value: Int init(fixtureValue: Int) { self.value = fixtureValue } static var fixture: Self { Self(fixtureValue: 1) } } """, macros: testMacros ) ``` -------------------------------- ### Define Reusable Fixture Variations Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Create static properties on your fixture types to provide commonly used variations. This promotes DRY principles in your tests. ```swift extension User { static var adminFixture: User { User.fixture { $0.name = "Admin User" $0.email = "admin@company.com" } } static var readOnlyUserFixture: User { User.fixture { $0.role = .user $0.permissions = [.read] $0.isActive = true } } static var suspendedUserFixture: User { User.fixture { $0.isActive = false $0.permissions = [] } } } ``` -------------------------------- ### Conform External Types to Fixtureable Protocol Source: https://github.com/gibachan/swift-fixtures/blob/main/README.md For types from external libraries, manually conform them to the Fixtureable protocol. Define the static `fixture` property to provide a default instance of the external type. ```swift import ExternalLibrary import Fixtures extension ExternalType: Fixtureable { static var fixture: Self { ExternalType( name: .fixture, value: .fixture ) } } // Now you can use .fixture with external types let external: ExternalType = .fixture ``` -------------------------------- ### Run Specific Swift Test Case Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Execute a single, specific test case within a target. This is helpful for debugging a particular test. ```bash swift test --filter FixturesTests.FixtureTests/fixtureInt ``` -------------------------------- ### Define and Access a Simple Fixture Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Define a struct with the @Fixture macro and access its default fixture instance via the static `fixture` property. ```swift @Fixture struct User { let name: String let age: Int let email: String } let user = User.fixture // Creates: User(name: "a", age: 1, email: "a") ``` -------------------------------- ### Create Nested Object Graphs with Fixtures Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Use nested fixture builders to construct complex object hierarchies. This is useful for setting up intricate test data structures. ```swift @Fixture struct Order { let id: UUID let customer: Customer let items: [Product] let total: Double let status: OrderStatus } @Fixture struct Customer { let id: UUID let name: String let email: String let isPremium: Bool } let complexOrder = Order.fixture { $0.customer = Customer.fixture { $0.name = "John Doe" $0.email = "john@example.com" $0.isPremium = true } $0.items = [ Product.fixture { $0.name = "Product A" $0.price = 25.0 }, Product.fixture { $0.name = "Product B" $0.price = 75.0 } ] $0.total = $0.items.reduce(0) { $0 + $1.price } $0.status = .confirmed } ``` -------------------------------- ### Use the Generated Fixture Initializer Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Access the generated initializer for a fixture-enabled type, using `fixture` prefixed parameter names for customization. ```swift let specificUser = User( fixtureName: "Bob Smith", fixtureAge: 35, fixtureEmail: "bob@example.com" ) ``` -------------------------------- ### @Fixture Macro Definition Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md An attached extension macro that generates fixture implementations for conforming types. It simplifies the creation of test data. ```swift @attached(extension, conformances: Fixtureable, names: named(fixture)) extension Never: Fixtureable {} ``` -------------------------------- ### Struct Fixture Generation - Customization Method Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Generates a `static func fixture(_ configure:)` method for structs. This allows customization of fixture instances using a closure. ```swift let customizationMethod = FunctionDeclSyntax( "static func fixture(_ configure: (inout FixtureBuilder) -> Void) -> Self" ) { var builder = FixtureBuilder() configure(&builder) return Self(fixture: builder.build()) } ``` -------------------------------- ### Parameter Helper Structure Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Represents parameters used during macro expansion. This helps in parsing and processing arguments passed to the macro. ```swift struct Parameter { let name: String let type: String let defaultValue: String? = nil } ``` -------------------------------- ### Define and Use @Fixture Macro for Structs and Enums Source: https://github.com/gibachan/swift-fixtures/blob/main/README.md Apply the @Fixture macro to your structs and enums to automatically generate a `.fixture` static property. This allows for easy creation of default instances and customizable instances using a builder pattern. ```swift @Fixture struct User { let id: String let name: String var age: Int @Fixture enum Role { case guest case user(name: String) case admin(id: String, permissions: [String]) } var role: Role } // Generate fixture with default values let user: User = .fixture // User(id: "a", name: "a", age: 1, role: .guest) // Customize specific properties let bob = User.fixture { $0.name = "Bob" $0.age = 30 $0.role = .admin(id: "admin123", permissions: ["read", "write"]) } // User(id: "a", name: "Bob", age: 30, role: .admin(id: "admin123", permissions: ["read", "write"])). ``` -------------------------------- ### Add Fixture Support to External Types Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Extend external types with `Fixtureable` conformance to use the library's features with types you don't own. Ensure necessary imports are included. ```swift // For a third-party library type import ExternalLibrary extension ExternalType: Fixtureable { static var fixture: Self { ExternalType( name: .fixture, value: .fixture ) } } // Now you can use .fixture with external types let external: ExternalType = .fixture ``` -------------------------------- ### Fixtureable Protocol Definition Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Defines the contract for types that can provide fixtures. Any type conforming to this protocol can generate its own fixtures. ```swift public protocol Fixtureable where Fixture: Sendable { associatedtype Fixture static var fixture: Fixture { get } } ``` -------------------------------- ### Debug-only Code Wrapper Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md All generated fixture code is wrapped in `#if DEBUG`. This ensures that no fixture-related code is included in release builds. ```swift #if DEBUG // Generated fixture code here #endif ``` -------------------------------- ### Focus on Relevant Data for Test Readability Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md When testing, only define the fields that are relevant to the test's assertion. This improves clarity and reduces maintenance. ```swift func testUserValidation() { let invalidUser = User.fixture { $0.email = "invalid-email" } XCTAssertThrowsError(try validateUser(invalidUser)) } ``` -------------------------------- ### Fixture with Default Property Values Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Properties with default values are automatically used by the fixture initializer. Only properties without defaults need to be specified. ```swift @Fixture struct Settings { let theme: String = "dark" let notifications: Bool let language: String = "en" } // Only notifications needs to be specified let settings = Settings(fixtureNotifications: true) // theme = "dark", language = "en" (from defaults) ``` -------------------------------- ### Run Specific Swift Test Target Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Filter and run tests belonging to a specific target within the Swift package. Useful for isolating issues. ```bash swift test --filter FixturesTests ``` ```bash swift test --filter FixturesMacrosTests ``` -------------------------------- ### Define a Fixtureable Struct Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/GettingStarted.md Apply the @Fixture macro to a struct to automatically generate fixture-related code, including conformance to Fixtureable and a static fixture property. ```swift @Fixture struct Product { let id: UUID let name: String let price: Double let inStock: Bool } ``` -------------------------------- ### Builder for Partial Customization Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md The builder pattern allows for partial customization of fixture instances, where only the properties relevant to the test are modified, while others retain their default fixture values. ```swift let outOfStockProduct = Product.fixture { $0.inStock = false // All other properties use their fixture defaults } ``` -------------------------------- ### Struct Fixture Generation - FixtureBuilder Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Generates a `FixtureBuilder` struct for structs. This supports the builder pattern for creating complex instances. ```swift let builderStruct = StructDeclSyntax( "struct FixtureBuilder {" + parameters.map { "var \($0.name): \($0.type.trimmed)? = nil" }.joined(separator: "\n") + "\n func build() -> Self { return self } }" ) ``` -------------------------------- ### Customize Fixture Properties Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Customize specific properties of a fixture instance using the closure-based `fixture` method. Other properties will retain their default fixture values. ```swift let customUser = User.fixture { $0.name = "Alice Johnson" $0.age = 28 } // email remains "a" (default fixture value) ``` -------------------------------- ### Conceptual FixtureBuilder Structure Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md The @Fixture macro generates a conceptual FixtureBuilder struct for each marked type, providing a blueprint for creating customized instances. ```swift @Fixture struct Product { let id: UUID let name: String let price: Double let category: String let inStock: Bool } // Generated FixtureBuilder (conceptual) struct FixtureBuilder { var id: UUID = .fixture var name: String = .fixture var price: Double = .fixture var category: String = .fixture var inStock: Bool = .fixture } ``` -------------------------------- ### Use Generated Fixture in Tests Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/GettingStarted.md Access the generated static `fixture` property to create an instance of your struct with default values. This is commonly used in unit tests. ```swift func testProductCreation() { let product = Product.fixture XCTAssertNotNil(product.id) XCTAssertEqual(product.name, "a") XCTAssertEqual(product.price, 1.0) XCTAssertTrue(product.inStock) } ``` -------------------------------- ### Struct Fixture Generation - Static Property Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Generates a `static var fixture` property for structs. This property returns a default fixture instance of the struct. ```swift let staticFixtureProperty = VariableDeclSyntax( "static var fixture: Self { Self(fixture: " + parameters.map { "\($0.name): \($0.name)" }.joined(separator: ", ") + ") }" ) ``` -------------------------------- ### Fixture for Struct with Collection Properties Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Structs with collection types like arrays can be used with @Fixture, automatically generating default collection instances. ```swift @Fixture struct Team { let name: String let members: [User] let settings: Settings? } let team = Team.fixture // Creates team with 3 User fixtures and nil settings ``` -------------------------------- ### Enum Fixture Generation - Associated Value Handling Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Automatic handling of associated values for enum cases, including labeled, unlabeled, and mixed types. ```swift let enumCase = EnumCaseDeclSyntax( "case \(name)\(associatedValue)" ) ``` -------------------------------- ### Enum Fixture Generation - Static Property Source: https://github.com/gibachan/swift-fixtures/blob/main/CLAUDE.md Generates a `static var fixture` property for enums. This property defaults to the first case of the enum. ```swift let staticFixtureProperty = VariableDeclSyntax( "static var fixture: Self { .\(firstCaseName) }" ) ``` -------------------------------- ### Define a Fixtureable Type with @Fixture Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/Fixtures.md Apply the `@Fixture` macro to your Swift structs or enums to automatically generate fixture values. This macro provides static typing and compile-time verification for your fixtures. ```swift import Fixtures @Fixture struct User { let id: UUID let name: String let age: Int let isActive: Bool } // Use in tests let user = User.fixture let customUser = User.fixture { $0.name = "Alice" $0.age = 25 } ``` -------------------------------- ### Fixture for Simple Enum Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md Enums marked with @Fixture use their first declared case as the default fixture value. ```swift @Fixture enum Status { case pending case approved case rejected } let status = Status.fixture // Returns: .pending ``` -------------------------------- ### Fixture for Enum with Associated Values Source: https://github.com/gibachan/swift-fixtures/blob/main/Sources/Fixtures/Fixtures.docc/BasicUsage.md The @Fixture macro automatically provides default fixture values for associated types within enums. ```swift @Fixture enum Result { case success(data: String) case failure(error: String) } let result = Result.fixture // Returns: .success(data: .fixture) which is .success(data: "a") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.