forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssert.swift
More file actions
38 lines (33 loc) · 1.4 KB
/
Assert.swift
File metadata and controls
38 lines (33 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// Assert.swift
//
//
// Created by Alsey Coleman Miller on 6/4/20.
//
/// Unconditionally prints a given message and stops execution.
internal func fatalError(_ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Never {
let message = message()
JSConsole.error("Fatal error: \(message)")
Swift.fatalError(message, file: file, line: line)
}
/// Performs a traditional C-style assert with an optional message.
internal func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
let condition = condition()
let message = message()
JSConsole.assert(condition, "Assertion failure: \(message)")
Swift.assert(condition, message, file: file, line: line)
}
/// Performs a traditional C-style assert with an optional message.
internal func assert(_ condition: @autoclosure () -> JSBoolean, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
assert(condition().rawValue, message(), file: file, line: line)
}
internal extension Optional {
func assert(_ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Wrapped {
switch self {
case .none:
fatalError(message(), file: file, line: line)
case let .some(value):
return value
}
}
}