Skip to content

Commit

Permalink
Fix assertions to work correctly with Swift 5.2 and 5.3.
Browse files Browse the repository at this point in the history
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
lukeredpath committed Jul 1, 2020
1 parent 31227d0 commit afc8e56
Showing 1 changed file with 70 additions and 10 deletions.
80 changes: 70 additions & 10 deletions Tests/ValidationsTests/Support/Assertions.swift
@@ -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

0 comments on commit afc8e56

Please sign in to comment.