Last active
December 5, 2019 02:13
-
-
Save Jon889/ed9fa22283db42657c334005d66aab2b to your computer and use it in GitHub Desktop.
UI Test reporting
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
public typealias Given = Step | |
public typealias When = Step | |
public typealias Then = Step | |
public typealias And = Step | |
public struct Step { | |
@discardableResult | |
public init(_ handler: @autoclosure () -> Void, file: String = #file, line lineNumber: Int = #line) { | |
step(handler, file: file, line: lineNumber) | |
} | |
@discardableResult | |
public init(I handler: @autoclosure () -> Void, file: String = #file, line lineNumber: Int = #line) { | |
step(handler, file: file, line: lineNumber) | |
} | |
@discardableResult | |
public init(_ handler: () -> Void, file: String = #file, line lineNumber: Int = #line) { | |
step(handler, file: file, line: lineNumber) | |
} | |
@discardableResult | |
public init(I handler: () -> Void, file: String = #file, line lineNumber: Int = #line) { | |
step(handler, file: file, line: lineNumber) | |
} | |
private func step(_ handler: () -> Void, file: String, line lineNumber: Int) { | |
let codeLine = CodeLineCache.line(in: file, at: lineNumber) | |
XCTContext.runActivity(named: codeLine) { _ in | |
handler() | |
} | |
} | |
} | |
private struct CodeLineCache { | |
private static var cache = [String: [String.SubSequence]]() | |
static func line(in file: String, at lineNumber: Int) -> String { | |
if let lines = cache[file] { | |
return getLine(lineNumber, from: lines) | |
} | |
let lines = getLines(of: file) | |
cache[file] = lines | |
return getLine(lineNumber, from: lines) | |
} | |
private static func getLine(_ lineNumber: Int, from lines: [String.SubSequence]) -> String { | |
let lineIndex = lineNumber - 1 | |
return String(lines[lineIndex]).trimmingCharacters(in: .whitespacesAndNewlines) | |
} | |
private static func getLines(of file: String) -> [String.SubSequence] { | |
return (try? String(contentsOf: URL(fileURLWithPath: file)).split(separator: "\n", omittingEmptySubsequences: false)) ?? [] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment