Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix assertions to work correctly with Swift 5.2 and 5.3.
Unfortunately whilst wrapping #file in () fixes the warning it still breaks the line highlighting functionality when forwarded into XCTest. We *need* to use #file on < 5.3 and #filePath on >= 5.3. This should hopefully change soon: https://forums.swift.org/t/revisiting-the-source-compatibility-impact-of-se-0274-concise-magic-file-names/37720/3
- Loading branch information
1 parent
31227d0
commit afc8e56
Showing
1 changed file
with
70 additions
and
10 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,49 +1,109 @@ | ||
import XCTest | ||
import Validations | ||
|
||
func assertValid<Value>( | ||
func _assertValid<Value>( | ||
_ validator: ValidatorOf<Value, String>, | ||
given value: Value, | ||
file: StaticString = (#file), | ||
filePath: StaticString, | ||
line: UInt = #line | ||
) { | ||
let result = validator.validate(value) | ||
|
||
XCTAssert( | ||
result.isValid, | ||
"should be valid given \(value)", | ||
file: file, line: line) | ||
file: filePath, line: line) | ||
} | ||
|
||
func assertNotValid<Value>( | ||
func _assertNotValid<Value>( | ||
_ validator: ValidatorOf<Value, String>, | ||
given value: Value, | ||
file: StaticString = (#file), | ||
filePath: StaticString, | ||
line: UInt = #line | ||
) { | ||
let result = validator.validate(value) | ||
|
||
XCTAssertFalse( | ||
result.isValid, | ||
"should not be valid given \(value)", | ||
file: file, line: line) | ||
file: filePath, line: line) | ||
} | ||
|
||
func assertNotValid<Value, Error>( | ||
func _assertNotValid<Value, Error>( | ||
_ validator: ValidatorOf<Value, Error>, | ||
given value: Value, | ||
errors: [Error], | ||
file: StaticString = (#file), | ||
filePath: StaticString, | ||
line: UInt = #line | ||
) where Error: Comparable { | ||
let result = validator.validate(value) | ||
|
||
XCTAssertFalse( | ||
result.isValid, "should not be valid given \(value)", | ||
file: file, line: line) | ||
file: filePath, line: line) | ||
|
||
XCTAssertEqual( | ||
errors.sorted(), Array(result.errors!), | ||
"expected errors to match: \(errors)", | ||
file: file, line: line) | ||
file: filePath, line: line) | ||
} | ||
|
||
// Remove this once the current source-compatibility breaking change is fixed, hopefully | ||
// before Xcode 12 final drops! | ||
// | ||
// See: https://forums.swift.org/t/revisiting-the-source-compatibility-impact-of-se-0274-concise-magic-file-names/37720/3 | ||
|
||
#if compiler(>=5.3) | ||
func assertValid<Value>( | ||
_ validator: ValidatorOf<Value, String>, | ||
given value: Value, | ||
filePath: StaticString = #filePath, | ||
line: UInt = #line | ||
) { | ||
_assertValid(validator, given: value, filePath: filePath, line: line) | ||
} | ||
func assertNotValid<Value>( | ||
_ validator: ValidatorOf<Value, String>, | ||
given value: Value, | ||
filePath: StaticString = #filePath, | ||
line: UInt = #line | ||
) { | ||
_assertNotValid(validator, given: value, filePath: filePath, line: line) | ||
} | ||
func assertNotValid<Value, Error>( | ||
_ validator: ValidatorOf<Value, Error>, | ||
given value: Value, | ||
errors: [Error], | ||
filePath: StaticString = #filePath, | ||
line: UInt = #line | ||
) where Error: Comparable { | ||
_assertNotValid(validator, given: value, errors: errors, filePath: filePath, line: line) | ||
} | ||
#else | ||
func assertValid<Value>( | ||
_ validator: ValidatorOf<Value, String>, | ||
given value: Value, | ||
filePath: StaticString = #file, | ||
line: UInt = #line | ||
) { | ||
_assertValid(validator, given: value, filePath: filePath, line: line) | ||
} | ||
func assertNotValid<Value>( | ||
_ validator: ValidatorOf<Value, String>, | ||
given value: Value, | ||
filePath: StaticString = #file, | ||
line: UInt = #line | ||
) { | ||
_assertNotValid(validator, given: value, filePath: filePath, line: line) | ||
} | ||
func assertNotValid<Value, Error>( | ||
_ validator: ValidatorOf<Value, Error>, | ||
given value: Value, | ||
errors: [Error], | ||
filePath: StaticString = #file, | ||
line: UInt = #line | ||
) where Error: Comparable { | ||
_assertNotValid(vlidator, given: value, errors: errors, filePath: filePath, line: line) | ||
} | ||
#endif | ||
|