-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for single data set tests
Useful when using logic tests to test a single set of values against a single set of expected results
- Loading branch information
1 parent
2c50ce4
commit 9130982
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
Sources/ParameterizedTesting/ParameterizedTestsCase1.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// ParameterizedTestsCase1.swift | ||
// Copyright © 2022 Cameron Cooke. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
public class ParameterizedTestCase1<IN1, OUT>: XCTestCase { | ||
// MARK: - Public - | ||
|
||
public class func customTestSuite(_ subclassType: (some XCTestCase).Type) -> XCTestSuite { | ||
let suite = XCTestSuite(forTestCaseClass: Self.self) | ||
let params1 = values() | ||
|
||
var counter = 0 | ||
let totalCombinations = params1.count | ||
let expectedValues = expectedValues() | ||
|
||
ParameterizedTestHandler.allCombinations( | ||
params1, | ||
{ value1 in | ||
|
||
let selector = ParameterizedTestCase1.registerTestMethod( | ||
name: "\(value1)".lowercased(), | ||
testMethod: #selector(self.internalHandler) | ||
) | ||
|
||
let test = subclassType.init(selector: selector) | ||
test.setValue(value: value1, forKey: &ParameterizedTestCaseKey.value1) | ||
|
||
if let expectedValues { | ||
if expectedValues.count == totalCombinations { | ||
let expectedValue = expectedValues[counter] | ||
test.setValue(value: expectedValue, forKey: &ParameterizedTestCaseKey.expectedValue) | ||
|
||
} else { | ||
preconditionFailure( | ||
"The number of expected values (\(expectedValues.count)) does not satisfy the total number of all combinations of values (\(totalCombinations))." | ||
) | ||
} | ||
} | ||
|
||
suite.addTest(test) | ||
counter += 1 | ||
} | ||
) | ||
|
||
return suite | ||
} | ||
|
||
public class func values() -> ([IN1]) { | ||
fatalError("Not implemented") | ||
} | ||
|
||
public class func expectedValues() -> [OUT]? { | ||
nil | ||
} | ||
|
||
public func testAllCombinations(_ value1: IN1, _ expectedResult: OUT?) { | ||
fatalError("Not implemented") | ||
} | ||
|
||
// MARK: - Internal - | ||
|
||
func getValue1() -> IN1? { | ||
getValue(forKey: &ParameterizedTestCaseKey.value1) | ||
} | ||
|
||
func getExpectedValue() -> OUT? { | ||
getValue(forKey: &ParameterizedTestCaseKey.expectedValue) | ||
} | ||
|
||
@objc | ||
func internalHandler() { | ||
guard let value1 = getValue1() else { | ||
preconditionFailure("Params not set") | ||
} | ||
|
||
let expectedValue = getExpectedValue() | ||
testAllCombinations(value1, expectedValue) | ||
} | ||
} |