diff --git a/Sources/SwiftFixture/Fixture+DefaultProviders.swift b/Sources/SwiftFixture/Fixture+DefaultProviders.swift index bddfc94..9156789 100644 --- a/Sources/SwiftFixture/Fixture+DefaultProviders.swift +++ b/Sources/SwiftFixture/Fixture+DefaultProviders.swift @@ -1,18 +1,11 @@ import Foundation -extension Fixture { - func registerDefaults(using preferredFormat: PreferredFormat) { - switch preferredFormat { - case .random: - registerRandomDefaults() - case .constant: - registerConstantDefaults() - } - } - - // TODO: Conditionally support other common types, such as CGFloat from CoreGraphics +#if canImport(CoreGraphics) +import CoreGraphics +#endif - private func registerRandomDefaults() { +extension Fixture { + func registerDefaultProviders() { // Standard Library register(Int.self) { .random(in: 0 ... .max) } register(Int8.self) { .random(in: 0 ... .max) } @@ -29,34 +22,41 @@ extension Fixture { register(Bool.self) { .random() } register(String.self) { UUID().uuidString.lowercased() } register(Character.self) { UUID().uuidString.first! } + register(Data.self) { Data((0 ..< 16).map({ _ in UInt8.random(in: .min ... .max) })) } // Foundation register(UUID.self) { UUID() } register(URL.self) { URL(string: "https://www.\(UUID().uuidString.lowercased()).com/")! } - register(Date.self) { Date(timeIntervalSinceReferenceDate: TimeInterval.random(in: 0 ... Date().timeIntervalSinceReferenceDate)) } - } + register(Date.self) { Date(timeIntervalSinceReferenceDate: .random(in: 0 ... Date().timeIntervalSinceReferenceDate)) } - private func registerConstantDefaults() { - // Standard Library - register(Int.self) { 0 } - register(Int8.self) { 0 } - register(Int16.self) { 0 } - register(Int32.self) { 0 } - register(Int64.self) { 0 } - register(UInt.self) { 0 } - register(UInt8.self) { 0 } - register(UInt16.self) { 0 } - register(UInt32.self) { 0 } - register(UInt64.self) { 0 } - register(Float.self) { 0.0 } - register(Double.self) { 0.0 } - register(Bool.self) { false } - register(String.self) { "" } - register(Character.self) { Character("") } - - // Foundation - register(UUID.self) { UUID(uuidString: "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")! } - register(URL.self) { URL(string: "https://www.example.com/")! } - register(Date.self) { Date(timeIntervalSinceReferenceDate: 0) } + #if canImport(CoreGraphics) + register(CGFloat.self) { .random(in: 0 ... 2048) } + register(CGPoint.self) { values in + CGPoint( + x: try values.get("x") as CGFloat, + y: try values.get("y") as CGFloat + ) + } + register(CGSize.self) { values in + CGSize( + width: try values.get("width") as CGFloat, + height: try values.get("height") as CGFloat + ) + } + register(CGRect.self) { values in + CGRect( + x: try values.get("x") as CGFloat, + y: try values.get("y") as CGFloat, + width: try values.get("width") as CGFloat, + height: try values.get("height") as CGFloat + ) + } + register(CGVector.self) { values in + CGVector( + dx: try values.get("dx") as CGFloat, + dy: try values.get("dy") as CGFloat + ) + } + #endif } } diff --git a/Sources/SwiftFixture/Fixture.swift b/Sources/SwiftFixture/Fixture.swift index 31d672f..b27a878 100644 --- a/Sources/SwiftFixture/Fixture.swift +++ b/Sources/SwiftFixture/Fixture.swift @@ -61,11 +61,9 @@ open class Fixture { /// A lookup of closures used to provide fixture values keyed by the type name private var providers: [String: AnyProvider] = [:] - /// Creates a fixture instance with builtin fixture types using the preferred format provided. - /// - /// - Parameter preferredFormat: The preferred format used when vending fixture values. Defaults to ``PreferredFormat/random``. - public init(preferredFormat: PreferredFormat = .random) { - registerDefaults(using: preferredFormat) + /// Creates a new instance for vending fixture values + public init() { + registerDefaultProviders() } } diff --git a/Sources/SwiftFixture/PreferredFormat.swift b/Sources/SwiftFixture/PreferredFormat.swift deleted file mode 100644 index 6084bfe..0000000 --- a/Sources/SwiftFixture/PreferredFormat.swift +++ /dev/null @@ -1,10 +0,0 @@ -/// The preferred format of fixture values provided by ``Fixture``. -/// -/// - Note: This value is only guaranteed to influence the default registered types and may be ignored elsewhere. -public enum PreferredFormat { - /// Prefer random fixture values. - case random - - /// Prefer constant fixture values for a given type. - case constant -} diff --git a/Tests/SwiftFixtureTests/FixtureExampleTests.swift b/Tests/SwiftFixtureTests/FixtureExampleTests.swift index 7e63e85..6363981 100644 --- a/Tests/SwiftFixtureTests/FixtureExampleTests.swift +++ b/Tests/SwiftFixtureTests/FixtureExampleTests.swift @@ -21,7 +21,7 @@ private struct Group { #endif final class FixtureExampleTests: XCTestCase { - let fixture = Fixture(preferredFormat: .random) + let fixture = Fixture() override func setUp() { super.setUp() diff --git a/Tests/SwiftFixtureTests/FixtureTests.swift b/Tests/SwiftFixtureTests/FixtureTests.swift index 689a25d..95f798c 100644 --- a/Tests/SwiftFixtureTests/FixtureTests.swift +++ b/Tests/SwiftFixtureTests/FixtureTests.swift @@ -53,7 +53,10 @@ final class FixtureTests: XCTestCase { } func testRegisteredModel() throws { - let fixture = Fixture(preferredFormat: .constant) + let fixture = Fixture() + fixture.register(String.self) { "" } + fixture.register(Date.self) { Date(timeIntervalSinceReferenceDate: 0) } + fixture.register(Bool.self) { false } fixture.register(TestEnum.self) { .one } fixture.register(TestModel.self) { values in TestModel(