|
| 1 | +// |
| 2 | +// ParameterizedTestsCase1.swift |
| 3 | +// Copyright © 2022 Cameron Cooke. All rights reserved. |
| 4 | +// |
| 5 | + |
| 6 | +import XCTest |
| 7 | + |
| 8 | +public class ParameterizedTestCase1<IN1, OUT>: XCTestCase { |
| 9 | + // MARK: - Public - |
| 10 | + |
| 11 | + public class func customTestSuite(_ subclassType: (some XCTestCase).Type) -> XCTestSuite { |
| 12 | + let suite = XCTestSuite(forTestCaseClass: Self.self) |
| 13 | + let params1 = values() |
| 14 | + |
| 15 | + var counter = 0 |
| 16 | + let totalCombinations = params1.count |
| 17 | + let expectedValues = expectedValues() |
| 18 | + |
| 19 | + ParameterizedTestHandler.allCombinations( |
| 20 | + params1, |
| 21 | + { value1 in |
| 22 | + |
| 23 | + let selector = ParameterizedTestCase1.registerTestMethod( |
| 24 | + name: "\(value1)".lowercased(), |
| 25 | + testMethod: #selector(self.internalHandler) |
| 26 | + ) |
| 27 | + |
| 28 | + let test = subclassType.init(selector: selector) |
| 29 | + test.setValue(value: value1, forKey: &ParameterizedTestCaseKey.value1) |
| 30 | + |
| 31 | + if let expectedValues { |
| 32 | + if expectedValues.count == totalCombinations { |
| 33 | + let expectedValue = expectedValues[counter] |
| 34 | + test.setValue(value: expectedValue, forKey: &ParameterizedTestCaseKey.expectedValue) |
| 35 | + |
| 36 | + } else { |
| 37 | + preconditionFailure( |
| 38 | + "The number of expected values (\(expectedValues.count)) does not satisfy the total number of all combinations of values (\(totalCombinations))." |
| 39 | + ) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + suite.addTest(test) |
| 44 | + counter += 1 |
| 45 | + } |
| 46 | + ) |
| 47 | + |
| 48 | + return suite |
| 49 | + } |
| 50 | + |
| 51 | + public class func values() -> ([IN1]) { |
| 52 | + fatalError("Not implemented") |
| 53 | + } |
| 54 | + |
| 55 | + public class func expectedValues() -> [OUT]? { |
| 56 | + nil |
| 57 | + } |
| 58 | + |
| 59 | + public func testAllCombinations(_ value1: IN1, _ expectedResult: OUT?) { |
| 60 | + fatalError("Not implemented") |
| 61 | + } |
| 62 | + |
| 63 | + // MARK: - Internal - |
| 64 | + |
| 65 | + func getValue1() -> IN1? { |
| 66 | + getValue(forKey: &ParameterizedTestCaseKey.value1) |
| 67 | + } |
| 68 | + |
| 69 | + func getExpectedValue() -> OUT? { |
| 70 | + getValue(forKey: &ParameterizedTestCaseKey.expectedValue) |
| 71 | + } |
| 72 | + |
| 73 | + @objc |
| 74 | + func internalHandler() { |
| 75 | + guard let value1 = getValue1() else { |
| 76 | + preconditionFailure("Params not set") |
| 77 | + } |
| 78 | + |
| 79 | + let expectedValue = getExpectedValue() |
| 80 | + testAllCombinations(value1, expectedValue) |
| 81 | + } |
| 82 | +} |
0 commit comments