From 62666af626ed19bf431561940d822ce537c2e610 Mon Sep 17 00:00:00 2001 From: Liam Nichols Date: Sun, 2 Jul 2023 23:26:05 +0200 Subject: [PATCH] Support Array, Set and Dictionary (#12) --- Sources/SwiftFixture/FixtureProviding.swift | 31 +++++++++++++++++++++ Tests/SwiftFixtureTests/FixtureTests.swift | 11 +++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftFixture/FixtureProviding.swift b/Sources/SwiftFixture/FixtureProviding.swift index c2d8358..acd0a27 100644 --- a/Sources/SwiftFixture/FixtureProviding.swift +++ b/Sources/SwiftFixture/FixtureProviding.swift @@ -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 { + if let value: Element = try? values.get() { + return [value] + } else { + return Array() + } + } +} + +extension Set: FixtureProviding { + public static func provideFixture(using values: ValueProvider) throws -> Set { + if let value: Element = try? values.get() { + return [value] + } else { + return Set() + } + } +} + +extension Dictionary: FixtureProviding { + public static func provideFixture(using values: ValueProvider) throws -> Dictionary { + if let key: Key = try? values.get(), let value: Value = try? values.get() { + return [key: value] + } else { + return Dictionary() + } + } +} diff --git a/Tests/SwiftFixtureTests/FixtureTests.swift b/Tests/SwiftFixtureTests/FixtureTests.swift index 95f798c..d7e9774 100644 --- a/Tests/SwiftFixtureTests/FixtureTests.swift +++ b/Tests/SwiftFixtureTests/FixtureTests.swift @@ -2,7 +2,7 @@ import XCTest @testable import SwiftFixture final class FixtureTests: XCTestCase { - struct UnregisteredType: Equatable { + struct UnregisteredType: Hashable { } enum EmptyEnum: CaseIterable, Equatable { @@ -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, ["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?, ["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, []) + XCTAssertEqual(try fixture() as [String: UnregisteredType], [:]) XCTAssertThrowsError(try fixture() as UnregisteredType) { error in XCTAssertTrue(error is ResolutionError) }