|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | +// |
| 10 | +// XCTSkip.swift |
| 11 | +// APIs for skipping tests |
| 12 | +// |
| 13 | + |
| 14 | +/// An error which causes the current test to cease executing |
| 15 | +/// and be marked as skipped when it is thrown. |
| 16 | +public struct XCTSkip: Error { |
| 17 | + |
| 18 | + /// The user-supplied message related to this skip, if specified. |
| 19 | + public let message: String? |
| 20 | + |
| 21 | + /// A complete description of the skip. Includes the string-ified expression and user-supplied message when possible. |
| 22 | + let summary: String |
| 23 | + |
| 24 | + /// An explanation of why the skip has occurred. |
| 25 | + /// |
| 26 | + /// - Note: May be nil if the skip was unconditional. |
| 27 | + private let explanation: String? |
| 28 | + |
| 29 | + /// The source code location where the skip occurred. |
| 30 | + let sourceLocation: SourceLocation? |
| 31 | + |
| 32 | + private init(explanation: String?, message: String?, sourceLocation: SourceLocation?) { |
| 33 | + self.explanation = explanation |
| 34 | + self.message = message |
| 35 | + self.sourceLocation = sourceLocation |
| 36 | + |
| 37 | + var summary = "Test skipped" |
| 38 | + if let explanation = explanation { |
| 39 | + summary += ": \(explanation)" |
| 40 | + } |
| 41 | + if let message = message, !message.isEmpty { |
| 42 | + summary += " - \(message)" |
| 43 | + } |
| 44 | + self.summary = summary |
| 45 | + } |
| 46 | + |
| 47 | + public init(_ message: @autoclosure () -> String? = nil, file: StaticString = #file, line: UInt = #line) { |
| 48 | + self.init(explanation: nil, message: message(), sourceLocation: SourceLocation(file: file, line: line)) |
| 49 | + } |
| 50 | + |
| 51 | + fileprivate init(expectedValue: Bool, message: String?, file: StaticString, line: UInt) { |
| 52 | + let explanation = expectedValue |
| 53 | + ? "required true value but got false" |
| 54 | + : "required false value but got true" |
| 55 | + self.init(explanation: explanation, message: message, sourceLocation: SourceLocation(file: file, line: line)) |
| 56 | + } |
| 57 | + |
| 58 | + internal init(error: Error, message: String?, sourceLocation: SourceLocation?) { |
| 59 | + let explanation = #"threw error "\#(error)""# |
| 60 | + self.init(explanation: explanation, message: message, sourceLocation: sourceLocation) |
| 61 | + } |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +extension XCTSkip: XCTCustomErrorHandling { |
| 66 | + |
| 67 | + var shouldRecordAsTestFailure: Bool { |
| 68 | + // Don't record this error as a test failure since it's a test skip |
| 69 | + false |
| 70 | + } |
| 71 | + |
| 72 | + var shouldRecordAsTestSkip: Bool { |
| 73 | + true |
| 74 | + } |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +/// Evaluates a boolean expression and, if it is true, throws an error which |
| 79 | +/// causes the current test to cease executing and be marked as skipped. |
| 80 | +public func XCTSkipIf( |
| 81 | + _ expression: @autoclosure () throws -> Bool, |
| 82 | + _ message: @autoclosure () -> String? = nil, |
| 83 | + file: StaticString = #file, line: UInt = #line |
| 84 | +) throws { |
| 85 | + try skipIfEqual(expression(), true, message(), file: file, line: line) |
| 86 | +} |
| 87 | + |
| 88 | +/// Evaluates a boolean expression and, if it is false, throws an error which |
| 89 | +/// causes the current test to cease executing and be marked as skipped. |
| 90 | +public func XCTSkipUnless( |
| 91 | + _ expression: @autoclosure () throws -> Bool, |
| 92 | + _ message: @autoclosure () -> String? = nil, |
| 93 | + file: StaticString = #file, line: UInt = #line |
| 94 | +) throws { |
| 95 | + try skipIfEqual(expression(), false, message(), file: file, line: line) |
| 96 | +} |
| 97 | + |
| 98 | +private func skipIfEqual( |
| 99 | + _ expression: @autoclosure () throws -> Bool, |
| 100 | + _ expectedValue: Bool, |
| 101 | + _ message: @autoclosure () -> String?, |
| 102 | + file: StaticString, line: UInt |
| 103 | +) throws { |
| 104 | + let expressionValue: Bool |
| 105 | + |
| 106 | + do { |
| 107 | + // evaluate the expression exactly once |
| 108 | + expressionValue = try expression() |
| 109 | + } catch { |
| 110 | + throw XCTSkip(error: error, message: message(), sourceLocation: SourceLocation(file: file, line: line)) |
| 111 | + } |
| 112 | + |
| 113 | + if expressionValue == expectedValue { |
| 114 | + throw XCTSkip(expectedValue: expectedValue, message: message(), file: file, line: line) |
| 115 | + } |
| 116 | +} |
0 commit comments