Skip to content

Commit

Permalink
Remove PreferredFormat add support for Data and CoreGraphics types (
Browse files Browse the repository at this point in the history
#11)

* Remove PreferredFormat

* Add support for Data

* Add support for CoreGraphics types
  • Loading branch information
liamnichols authored Jul 2, 2023
1 parent 916d411 commit 4ee45e7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 53 deletions.
72 changes: 36 additions & 36 deletions Sources/SwiftFixture/Fixture+DefaultProviders.swift
Original file line number Diff line number Diff line change
@@ -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) }
Expand All @@ -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
}
}
8 changes: 3 additions & 5 deletions Sources/SwiftFixture/Fixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
10 changes: 0 additions & 10 deletions Sources/SwiftFixture/PreferredFormat.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/SwiftFixtureTests/FixtureExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion Tests/SwiftFixtureTests/FixtureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4ee45e7

Please sign in to comment.