[SWIFT] [For copy and paste] Test functions for Result [XCTest]

It's for copy and paste for me.


func M_XCTAssertResultIsSuccess<T, E>(_ result: @autoclosure () -> Result<T, E>, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
    switch result() {
    case .success: ()
    case .failure:
        XCTFail("M_XCTAssertResultIsSuccess faild: Reuslt is not Success.", file: file, line: line)
    }
}

func M_XCTAssertResultIsFailure<T, E>(_ result: @autoclosure () -> Result<T, E>, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
    switch result() {
    case .failure: ()
    case .success:
        XCTFail("M_XCTAssertResultIsFailure faild: Reuslt is not Failure.", file: file, line: line)
    }
}

///
func M_XCTAssertResultEqualValue<T: Equatable, E>(_ result: @autoclosure () -> Result<T, E>, _ value: @autoclosure () -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
    switch result() {
    case let .success(r):
        if r != value() {
            XCTFail("M_XCTAssertResultEqualValue faild: \(r) is not equal to \(value()).", file: file, line: line)
        }
    case .failure:
        XCTFail("M_XCTAssertResultEqualValue faild: result is Failure.", file: file, line: line)
    }
}

func M_XCTAssertResultEqualError<T, E: Equatable>(_ result: @autoclosure () -> Result<T, E>, _ value: @autoclosure () -> E, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
    switch result() {
    case let .failure(e):
        if e != value() {
            XCTFail("M_XCTAssertResultEqualError faild: \(e) is not equal to \(value()).", file: file, line: line)
        }
    case .success:
        XCTFail("M_XCTAssertResultEqualError faild: result is Success.", file: file, line: line)
    }
}

///
func M_XCTAssertResultGreaterThan<T: Comparable, E>(_ result: @autoclosure () -> Result<T, E>, _ value: @autoclosure () -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
    switch result() {
    case let .success(r):
        if r <= value() {
            XCTFail("M_XCTAssertResultGreaterThan faild: \(r) is not greater than \(value()).", file: file, line: line)
        }
    case .failure:
        XCTFail("M_XCTAssertResultGreaterThan faild: result is Failure.", file: file, line: line)
    }
}

func M_XCTAssertResultGreaterThanOrEqual<T: Comparable, E>(_ result: @autoclosure () -> Result<T, E>, _ value: @autoclosure () -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
    switch result() {
    case let .success(r):
        if r < value() {
            XCTFail("M_XCTAssertResultGreaterThanOrEqual faild: \(r) is not greater than or equal to \(value()).", file: file, line: line)
        }
    case .failure:
        XCTFail("M_XCTAssertResultGreaterThanOrEqual faild: result is Failure.", file: file, line: line)
    }
}

func M_XCTAssertResultLessThan<T: Comparable, E>(_ result: @autoclosure () -> Result<T, E>, _ value: @autoclosure () -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
    switch result() {
    case let .success(r):
        if r >= value() {
            XCTFail("M_XCTAssertResultLessThan faild: \(r) is not less than \(value()).", file: file, line: line)
        }
    case .failure:
        XCTFail("M_XCTAssertResultLessThan faild: result is Failure.", file: file, line: line)
    }
}

func M_XCTAssertResultLessThanOrEqual<T: Comparable, E>(_ result: @autoclosure () -> Result<T, E>, _ value: @autoclosure () -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
    switch result() {
    case let .success(r):
        if r > value() {
            XCTFail("M_XCTAssertResultLessThanOrEqual faild: \(r) is not less than or equal to \(value()).", file: file, line: line)
        }
    case .failure:
        XCTFail("M_XCTAssertResultLessThanOrEqual faild: result is Failure.", file: file, line: line)
    }
}

Recommended Posts

[For copy and paste] Test functions for Result [XCTest]
Copy and paste test with RSpec
Articles and books useful for studying test code
Functions and methods
Use Vue.js on a CDN (only copy and paste!)