Skip to content

Commit 4dda40d

Browse files
committed
🚨 updated Parser tests
1 parent cc90bb4 commit 4dda40d

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

PascalInterpreter/PascalInterpreter/FatalError.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import Foundation
1111
/**
1212
Taken from https://medium.com/@marcosantadev/how-to-test-fatalerror-in-swift-e1be9ff11a29
1313
*/
14-
/*func fatalError(_ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Never {
14+
func fatalError(_ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Never {
1515
FatalErrorUtil.fatalErrorClosure(message(), file, line)
16-
}*/
16+
}
1717

1818
struct FatalErrorUtil {
1919

PascalInterpreter/PascalInterpreterTests/ParserTests.swift

+15-3
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,20 @@ class ParserTests: XCTestCase {
373373
"""
374374

375375
let parser = Parser(program)
376-
_ = parser.parse()
376+
let result = parser.parse()
377+
let compound = Compound(children: [Assignment(left: Variable(name: "result"), right: FunctionCall(name: "Factorial", actualParameters: [Number.integer(6)])), NoOp()])
378+
let resultDeclaration = VariableDeclaration(variable: Variable(name: "result"), type: VariableType(type: .integer))
379+
let ifelse = IfElse(condition: Condition(type: .greaterThan, leftSide: Variable(name: "number"), rightSide: Number.integer(1)), trueExpression: Assignment(left: Variable(name: "Factorial"), right: BinaryOperation(left: Variable(name: "number"), operation: .mult, right: FunctionCall(name: "Factorial", actualParameters: [BinaryOperation(left: Variable(name: "number"), operation: .minus, right: Number.integer(1))]))), falseExpression: Assignment(left: Variable(name: "Factorial"), right: Number.integer(1)))
380+
let factorialBlock = Block(declarations: [], compound: Compound(children: [ifelse]))
381+
let factorialDeclarion = Function(name: "Factorial", params: [Param(name: "number", type: VariableType(type: .integer))], block: factorialBlock, returnType: VariableType(type: .integer))
382+
let block = Block(declarations: [resultDeclaration, factorialDeclarion], compound: compound)
383+
let node = Program(name: "Main", block: block)
384+
XCTAssertEqual(result, node)
377385
}
378386

379387
func testProgramWithStringConstants() {
380388
let program =
381-
"""
389+
"""
382390
program Main;
383391
var s: String;
384392
a: Boolean;
@@ -390,6 +398,10 @@ class ParserTests: XCTestCase {
390398
"""
391399

392400
let parser = Parser(program)
393-
_ = parser.parse()
401+
let result = parser.parse()
402+
let compound = Compound(children: [Assignment(left: Variable(name: "s"), right: "Test"), Assignment(left: Variable(name: "a"), right: false), FunctionCall(name: "writeln", actualParameters: [Variable(name:"s"), Variable(name:"a")]), NoOp()])
403+
let block = Block(declarations: [VariableDeclaration(variable: Variable(name: "s"), type: VariableType(type: .string)), VariableDeclaration(variable: Variable(name: "a"), type: VariableType(type: .boolean))], compound: compound)
404+
let node = Program(name: "Main", block: block)
405+
XCTAssertEqual(result, node)
394406
}
395407
}

PascalInterpreter/PascalInterpreterTests/XCTestCase+FatalError.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func XCTAssertEqual(_ left: AST, _ right: AST) {
8787
XCTAssert(left.name == right.name)
8888
XCTAssertEqual(left.block, right.block)
8989
XCTAssert(left.params.count == right.params.count)
90-
if left.params.count > 0 {
90+
if left.params.count > 0 && left.params.count == right.params.count {
9191
for i in 0 ... left.params.count - 1 {
9292
XCTAssertEqual(left.params[i], right.params[i])
9393
}
@@ -130,6 +130,10 @@ func XCTAssertEqual(_ left: AST, _ right: AST) {
130130
if let leftFalse = left.falseExpression, let rightFalse = right.falseExpression {
131131
XCTAssertEqual(leftFalse, rightFalse)
132132
}
133+
case let (left as String, right as String):
134+
XCTAssert(left == right)
135+
case let (left as Bool, right as Bool):
136+
XCTAssert(left == right)
133137
default:
134138
XCTFail("\(left) and \(right) are not equal")
135139
}

0 commit comments

Comments
 (0)