-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathXCTestCase+FatalError.swift
151 lines (142 loc) · 6.23 KB
/
XCTestCase+FatalError.swift
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// XCTestCase+FatalError.swift
// SwiftPascalInterpreterTests
//
// Created by Igor Kulman on 10/12/2017.
// Copyright © 2017 Igor Kulman. All rights reserved.
//
import Foundation
@testable import PascalInterpreter
import XCTest
/**
Taken from https://medium.com/@marcosantadev/how-to-test-fatalerror-in-swift-e1be9ff11a29
*/
extension XCTestCase {
func expectFatalError(expectedMessage: String, testcase: @escaping () -> Void) {
let expectation = self.expectation(description: "expectingFatalError")
var assertionMessage: String?
FatalErrorUtil.replaceFatalError { message, _, _ in
assertionMessage = message
expectation.fulfill()
self.unreachable()
}
DispatchQueue.global(qos: .userInitiated).async(execute: testcase)
waitForExpectations(timeout: 5) { _ in
XCTAssertEqual(assertionMessage, expectedMessage)
FatalErrorUtil.restoreFatalError()
}
}
private func unreachable() -> Never {
repeat {
RunLoop.current.run()
} while true
}
}
func XCTAssertEqual(_ left: AST, _ right: AST) {
switch (left, right) {
case let (Number.integer(left), Number.integer(right)):
XCTAssert(left == right)
case let (left as Program, right as Program):
XCTAssert(left.name == right.name)
XCTAssertEqual(left.block, right.block)
case let (left as Block, right as Block):
XCTAssert(left.declarations.count == right.declarations.count)
if left.declarations.count > 0 && left.declarations.count == right.declarations.count {
for i in 0 ... left.declarations.count - 1 {
XCTAssertEqual(left.declarations[i], right.declarations[i])
}
}
XCTAssertEqual(left.compound, right.compound)
case let (left as Compound, right as Compound):
XCTAssert(left.children.count == right.children.count)
if left.children.count > 0 && left.children.count == right.children.count {
for i in 0 ... left.children.count - 1 {
XCTAssertEqual(left.children[i], right.children[i])
}
}
case let (left as Assignment, right as Assignment):
XCTAssertEqual(left.left, right.left)
XCTAssertEqual(left.right, right.right)
case let (left as Variable, right as Variable):
XCTAssert(left.name == right.name)
case let (left as BinaryOperation, right as BinaryOperation):
XCTAssert(left.operation == right.operation)
XCTAssertEqual(left.left, right.left)
XCTAssertEqual(left.right, right.right)
case (is NoOp, is NoOp):
break
case let (left as UnaryOperation, right as UnaryOperation):
XCTAssert(left.operation == right.operation)
XCTAssertEqual(left.operand, right.operand)
case let (left as VariableDeclaration, right as VariableDeclaration):
XCTAssertEqual(left.type, right.type)
XCTAssertEqual(left.variable, right.variable)
case let (left as VariableType, right as VariableType):
XCTAssert(left.type == right.type)
case let (left as Function, right as Function):
XCTAssertEqual(left.returnType, right.returnType)
XCTAssert(left.name == right.name)
XCTAssertEqual(left.block, right.block)
XCTAssert(left.params.count == right.params.count)
if left.params.count > 0 && left.params.count == right.params.count {
for i in 0 ... left.params.count - 1 {
XCTAssertEqual(left.params[i], right.params[i])
}
}
case let (left as Procedure, right as Procedure):
XCTAssert(left.name == right.name)
XCTAssertEqual(left.block, right.block)
XCTAssert(left.params.count == right.params.count)
if left.params.count > 0 {
for i in 0 ... left.params.count - 1 {
XCTAssertEqual(left.params[i], right.params[i])
}
}
case let (left as FunctionCall, right as FunctionCall):
XCTAssert(left.name == right.name)
XCTAssert(left.actualParameters.count == right.actualParameters.count)
if left.actualParameters.count > 0 && left.actualParameters.count == right.actualParameters.count {
for i in 0 ... left.actualParameters.count - 1 {
XCTAssertEqual(left.actualParameters[i], right.actualParameters[i])
}
}
case let (left as Param, right as Param):
XCTAssert(left.name == right.name)
XCTAssertEqual(left.type, right.type)
case let (left as Condition, right as Condition):
XCTAssertEqual(left.type, right.type)
XCTAssertEqual(left.leftSide, right.leftSide)
XCTAssertEqual(left.rightSide, right.rightSide)
case let (left as IfElse, right as IfElse):
XCTAssertEqual(left.trueExpression, right.trueExpression)
switch (left.falseExpression == nil, right.falseExpression == nil) {
case (true, false):
XCTFail("\(String(describing: left.falseExpression)) and \(String(describing: right.falseExpression)) are not equal")
case (false, true):
XCTFail("\(String(describing: left.falseExpression)) and \(String(describing: right.falseExpression)) are not equal")
default:
break
}
XCTAssertEqual(left.condition, right.condition)
if let leftFalse = left.falseExpression, let rightFalse = right.falseExpression {
XCTAssertEqual(leftFalse, rightFalse)
}
case let (left as String, right as String):
XCTAssert(left == right)
case let (left as Bool, right as Bool):
XCTAssert(left == right)
case let (left as RepeatUntil, right as RepeatUntil):
XCTAssertEqual(left.condition, right.condition)
XCTAssertEqual(left.statement, right.statement)
case let (left as While, right as While):
XCTAssertEqual(left.condition, right.condition)
XCTAssertEqual(left.statement, right.statement)
case let (left as For, right as For):
XCTAssertEqual(left.variable, right.variable)
XCTAssertEqual(left.startValue, right.startValue)
XCTAssertEqual(left.endValue, right.endValue)
XCTAssertEqual(left.statement, right.statement)
default:
XCTFail("\(left) and \(right) are not equal")
}
}