Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,43 @@
SOFTWARE.
*/

#if canImport(XCTest)

import XCTest

class XCTestFailureRecorder: FailureRecorder {
func recordFailure(message: String, file: StaticString, line: UInt) {
XCTFail(message, file: file, line: line)
}
}
#endif

#if canImport(Testing)

import Testing

class SwiftTestingFailureRecorder: FailureRecorder {
func recordFailure(message: String, file: StaticString, line: UInt) {
Issue.record(
Comment(rawValue: message),
sourceLocation: Testing.SourceLocation(
fileID: #fileID, // TODO: pass #fileID from then()
filePath: file.withUTF8Buffer { String(decoding: $0, as: UTF8.self) },
line: Int(line),
column: 1 // TODO: pass #column from then()
)
)
}
}
#endif

class TestingFailureRecorder: FailureRecorder {
func recordFailure(message: String, file: StaticString, line: UInt) {
#if canImport(XCTest)
XCTestFailureRecorder().recordFailure(message: message, file: file, line: line)
#endif
#if canImport(Testing)
SwiftTestingFailureRecorder().recordFailure(message: message, file: file, line: line)
#endif
}
}
2 changes: 1 addition & 1 deletion Sources/MockSwift/Core/Then/Then.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func then<WrappedType>(_ value: WrappedType,
public func then<WrappedType>(_ value: WrappedType,
file: StaticString = #file,
line: UInt = #line) -> Then<WrappedType> {
then(value, errorHandler: ErrorHandler(), failureRecorder: XCTestFailureRecorder(), file: file, line: line)
then(value, errorHandler: ErrorHandler(), failureRecorder: TestingFailureRecorder(), file: file, line: line)
}

/// Call `completion` with a `Then` based on `value`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ extension Given where WrappedType == DummyProtocol {
mockable(first, second)
}

subscript(first: Predicate<Int>, second: Predicate<String>) -> MockableSubscript.Readable<String> {
subscript(first: MockSwift.Predicate<Int>, second: MockSwift.Predicate<String>) -> MockableSubscript.Readable<String> {
mockable(first, second)
}

subscript(x first: Int, y second: Int) -> MockableSubscript.Writable<String> {
mockable(first, second)
}

subscript(x first: Predicate<Int>, y second: Predicate<Int>) -> MockableSubscript.Writable<String> {
subscript(x first: MockSwift.Predicate<Int>, y second: MockSwift.Predicate<Int>) -> MockableSubscript.Writable<String> {
mockable(first, second)
}

func function(identifier: String) -> Mockable<Int> { mockable(identifier) }
func function(identifier: Predicate<String>) -> Mockable<Int> { mockable(identifier) }
func function(identifier: MockSwift.Predicate<String>) -> Mockable<Int> { mockable(identifier) }

func function(identifier: String) -> Mockable<String> { mockable(identifier) }
func function(identifier: Predicate<String>) -> Mockable<String> { mockable(identifier) }
func function(identifier: MockSwift.Predicate<String>) -> Mockable<String> { mockable(identifier) }
}

extension Then where WrappedType == DummyProtocol {
Expand All @@ -99,21 +99,21 @@ extension Then where WrappedType == DummyProtocol {
verifiable(first, second)
}

subscript(first: Predicate<Int>, second: Predicate<String>) -> VerifiableSubscript.Readable<String> {
subscript(first: MockSwift.Predicate<Int>, second: MockSwift.Predicate<String>) -> VerifiableSubscript.Readable<String> {
verifiable(first, second)
}

subscript(x first: Int, y second: Int) -> VerifiableSubscript.Writable<String> {
verifiable(first, second)
}

subscript(x first: Predicate<Int>, y second: Predicate<Int>) -> VerifiableSubscript.Writable<String> {
subscript(x first: MockSwift.Predicate<Int>, y second: MockSwift.Predicate<Int>) -> VerifiableSubscript.Writable<String> {
verifiable(first, second)
}

func function(identifier: String) -> Verifiable<Int> { verifiable(identifier) }
func function(identifier: Predicate<String>) -> Verifiable<Int> { verifiable(identifier) }
func function(identifier: MockSwift.Predicate<String>) -> Verifiable<Int> { verifiable(identifier) }

func function(identifier: String) -> Verifiable<String> { verifiable(identifier) }
func function(identifier: Predicate<String>) -> Verifiable<String> { verifiable(identifier) }
func function(identifier: MockSwift.Predicate<String>) -> Verifiable<String> { verifiable(identifier) }
}
12 changes: 6 additions & 6 deletions Tests/MockSwiftTests/Predicates/PredicateBoolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ import XCTest

class PredicatBoolTests: XCTestCase {
func test_isTrue_shouldReturnTrue() {
let predicate: Predicate<Bool> = .isTrue()
let predicate: MockSwift.Predicate<Bool> = .isTrue()
XCTAssertTrue(predicate.satisfy(by: true))
}

func test_isTrue_shouldReturnFalse() {
let predicate: Predicate<Bool> = .isTrue()
let predicate: MockSwift.Predicate<Bool> = .isTrue()
XCTAssertFalse(predicate.satisfy(by: false))
}

func test_isTrue_description() {
let predicate: Predicate<Bool> = .isTrue()
let predicate: MockSwift.Predicate<Bool> = .isTrue()
XCTAssertEqual(predicate.description, "True")
}

func test_isFalse_shouldReturnTrue() {
let predicate: Predicate<Bool> = .isFalse()
let predicate: MockSwift.Predicate<Bool> = .isFalse()
XCTAssertTrue(predicate.satisfy(by: false))
}

func test_isFalse_shouldReturnFalse() {
let predicate: Predicate<Bool> = .isFalse()
let predicate: MockSwift.Predicate<Bool> = .isFalse()
XCTAssertFalse(predicate.satisfy(by: true))
}

func test_isFalse_description() {
let predicate: Predicate<Bool> = .isFalse()
let predicate: MockSwift.Predicate<Bool> = .isFalse()
XCTAssertEqual(predicate.description, "False")
}
}
6 changes: 3 additions & 3 deletions Tests/MockSwiftTests/Predicates/PredicateOptionalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ import XCTest

class PredicateOptionalTests: XCTestCase {
func test_isNil_shouldReturnTrue() {
let predicate: Predicate<Int?> = .isNil()
let predicate: MockSwift.Predicate<Int?> = .isNil()
XCTAssertTrue(predicate.satisfy(by: nil))
}

func test_isNil_shouldReturnFalse() {
let predicate: Predicate<Int?> = .isNil()
let predicate: MockSwift.Predicate<Int?> = .isNil()
XCTAssertFalse(predicate.satisfy(by: 0))
}

func test_isNil_description() {
let predicate: Predicate<Int?> = .isNil()
let predicate: MockSwift.Predicate<Int?> = .isNil()
XCTAssertEqual(predicate.description, "nil")
}
}
28 changes: 14 additions & 14 deletions Tests/MockSwiftTests/Predicates/PredicateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@ class PredicateTests: XCTestCase {
// MARK: - match(description:any type:_ predicate:)

func test_match_withMatchShouldReturnFalseIfInputIsNotTheSameType() {
func assertion<T>(_ predicate: Predicate<T>) {
func assertion<T>(_ predicate: MockSwift.Predicate<T>) {
XCTAssertFalse(predicate.satisfy(by: 1))
}

assertion(.match(any: String.self) { _ in true })
}

func test_match_shouldReturnFalseIfInputNotMatched() {
func assertion<T>(_ predicate: Predicate<T>) {
func assertion<T>(_ predicate: MockSwift.Predicate<T>) {
XCTAssertFalse(predicate.satisfy(by: "not Empty"))
}

assertion(.match(any: String.self) { $0.isEmpty })
}

func test_match_shouldReturnTrueIfInputMatched() {
func assertion<T>(_ predicate: Predicate<T>) {
func assertion<T>(_ predicate: MockSwift.Predicate<T>) {
XCTAssertTrue(predicate.satisfy(by: ""))
}

assertion(.match(any: String.self) { $0.isEmpty })
}

func test_match_description() {
let predicate: Predicate<String> = .match { _ in
let predicate: MockSwift.Predicate<String> = .match { _ in
true
}
XCTAssertEqual("\(predicate)", "a String")
Expand All @@ -65,7 +65,7 @@ class PredicateTests: XCTestCase {
// MARK: - match(_ value:file:line:)

func test_match_shouldReturnTrueIfInputMatchedByAnyPredicate() {
let anyPredicate: AnyPredicate = Predicate<String>.match { value in
let anyPredicate: AnyPredicate = MockSwift.Predicate<String>.match { value in
value.isEmpty
}
let predicate: AnyPredicate = Predicate<AnyPredicate>.match(anyPredicate)
Expand Down Expand Up @@ -99,63 +99,63 @@ class PredicateTests: XCTestCase {
// MARK: - match(description:any type:when keyPath:)

func test_match_shouldReturnTrueIfKeyPathReturnTrue() {
func assertion<T>(_ predicate: Predicate<T>) {
func assertion<T>(_ predicate: MockSwift.Predicate<T>) {
XCTAssertTrue(predicate.satisfy(by: ""))
}

assertion(.match(any: String.self, \.isEmpty))
}

func test_match_shouldReturnFalseIfKeyPathReturnFalse() {
func assertion<T>(_ predicate: Predicate<T>) {
func assertion<T>(_ predicate: MockSwift.Predicate<T>) {
XCTAssertFalse(predicate.satisfy(by: "not Empty"))
}

assertion(.match(any: String.self, \.isEmpty))
}

func test_match_KeyPathDescription() {
let predicate: Predicate<String> = .match(\.isEmpty)
let predicate: MockSwift.Predicate<String> = .match(\.isEmpty)

XCTAssertEqual("\(predicate)", "a String")
}

// MARK: - any(_ type:)

func test_any_shouldReturnFalse() {
func assertion<T>(_ predicate: Predicate<T>) {
func assertion<T>(_ predicate: MockSwift.Predicate<T>) {
XCTAssertFalse(predicate.satisfy(by: 1))
}

assertion(.any(String.self))
}

func test_any_shouldReturnTrue() {
func assertion<T>(_ predicate: Predicate<T>) {
func assertion<T>(_ predicate: MockSwift.Predicate<T>) {
XCTAssertTrue(predicate.satisfy(by: ""))
}

assertion(.any(String.self))
}

func test_any_description() {
let predicate: Predicate<String> = .any()
let predicate: MockSwift.Predicate<String> = .any()

XCTAssertEqual("\(predicate)", "any String")
}

func test_not_shouldReturnFalse() {
let predicate: Predicate<String> = .not(.match { _ in true })
let predicate: MockSwift.Predicate<String> = .not(.match { _ in true })
XCTAssertFalse(predicate.satisfy(by: ""))
}

func test_not_shouldReturnTrue() {
let predicate: Predicate<String> = .not(.match { _ in false })
let predicate: MockSwift.Predicate<String> = .not(.match { _ in false })
XCTAssertTrue(predicate.satisfy(by: ""))
}

func test_not_description() {
let predicate: Predicate<String> = .not(.match(description: "description") { _ in true })
let predicate: MockSwift.Predicate<String> = .not(.match(description: "description") { _ in true })
XCTAssertEqual("\(predicate)", "not description")
}
}