Skip to content

Commit

Permalink
Support Array, Set and Dictionary (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamnichols authored Jul 2, 2023
1 parent 4ee45e7 commit 62666af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
31 changes: 31 additions & 0 deletions Sources/SwiftFixture/FixtureProviding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,34 @@ public protocol FixtureProviding {
/// - Returns: An instance of `Self` initialized with values suitable for use in testing.
static func provideFixture(using values: ValueProvider) throws -> Self
}

// MARK: - Containers
extension Array: FixtureProviding {
public static func provideFixture(using values: ValueProvider) throws -> Array<Element> {
if let value: Element = try? values.get() {
return [value]
} else {
return Array()
}
}
}

extension Set: FixtureProviding {
public static func provideFixture(using values: ValueProvider) throws -> Set<Element> {
if let value: Element = try? values.get() {
return [value]
} else {
return Set()
}
}
}

extension Dictionary: FixtureProviding {
public static func provideFixture(using values: ValueProvider) throws -> Dictionary<Key, Value> {
if let key: Key = try? values.get(), let value: Value = try? values.get() {
return [key: value]
} else {
return Dictionary()
}
}
}
11 changes: 10 additions & 1 deletion Tests/SwiftFixtureTests/FixtureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import XCTest
@testable import SwiftFixture

final class FixtureTests: XCTestCase {
struct UnregisteredType: Equatable {
struct UnregisteredType: Hashable {
}

enum EmptyEnum: CaseIterable, Equatable {
Expand Down Expand Up @@ -39,14 +39,23 @@ final class FixtureTests: XCTestCase {
XCTAssertEqual(try fixture() as Int, 42)
XCTAssertEqual(try fixture() as TestEnum, .three)
XCTAssertEqual(try fixture() as Container, .init(id: 42, name: "foo"))
XCTAssertEqual(try fixture() as [Int], [42])
XCTAssertEqual(try fixture() as Set<String>, ["foo"])
XCTAssertEqual(try fixture() as [String: Int], ["foo": 42])

// Optional
XCTAssertEqual(try fixture() as Int?, 42)
XCTAssertEqual(try fixture() as TestEnum?, .three)
XCTAssertEqual(try fixture() as Container?, .init(id: 42, name: "foo"))
XCTAssertEqual(try fixture() as [Int]?, [42])
XCTAssertEqual(try fixture() as Set<String>?, ["foo"])
XCTAssertEqual(try fixture() as [String: Int]?, ["foo": 42])

// Unregistered
XCTAssertEqual(try fixture() as UnregisteredType?, nil)
XCTAssertEqual(try fixture() as [UnregisteredType], [])
XCTAssertEqual(try fixture() as Set<UnregisteredType>, [])
XCTAssertEqual(try fixture() as [String: UnregisteredType], [:])
XCTAssertThrowsError(try fixture() as UnregisteredType) { error in
XCTAssertTrue(error is ResolutionError)
}
Expand Down

0 comments on commit 62666af

Please sign in to comment.