Skip to content

Commit 00bfac2

Browse files
committed
🚨 complete tests for repeat-until
1 parent 70b5a24 commit 00bfac2

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

PascalInterpreter/PascalInterpreter/Parser/Parser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public class Parser {
300300

301301
/**
302302
Rule:
303-
303+
304304
repeat_until : REPEAT statement UNTIL condition
305305
*/
306306
private func repeatUntilLoop() -> RepeatUntil {
@@ -316,7 +316,7 @@ public class Parser {
316316
}
317317
eat(.until)
318318
let condition = self.condition()
319-
return RepeatUntil(statement: Compound(children: statements), condition: condition)
319+
return RepeatUntil(statement: statements.count == 1 ? statements[0] : Compound(children: statements), condition: condition)
320320
}
321321

322322
/**

PascalInterpreter/PascalInterpreterTests/InterpreterTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class InterpreterTests: XCTestCase {
195195

196196
func testProgramWithRecursiveFunctionsAndParameterTheSameName() {
197197
let program =
198-
"""
198+
"""
199199
program Main;
200200
var number, result: integer;
201201
@@ -224,7 +224,7 @@ class InterpreterTests: XCTestCase {
224224

225225
func testProgramWithRepeatUntil() {
226226
let program =
227-
"""
227+
"""
228228
program Main;
229229
var x: integer;
230230
@@ -239,5 +239,10 @@ class InterpreterTests: XCTestCase {
239239

240240
let interpeter = Interpreter(program)
241241
interpeter.interpret()
242+
let (integerState, realState, boolState, stringState) = interpeter.getState()
243+
XCTAssert(realState == [:])
244+
XCTAssert(integerState == ["x": 6])
245+
XCTAssert(boolState == [:])
246+
XCTAssert(stringState == [:])
242247
}
243248
}

PascalInterpreter/PascalInterpreterTests/ParserTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ class ParserTests: XCTestCase {
422422

423423
let parser = Parser(program)
424424
let result = parser.parse()
425+
let xDec = VariableDeclaration(variable: Variable(name: "x"), type: VariableType(type: .integer))
426+
let compound = Compound(children: [Assignment(left: Variable(name: "x"), right: Number.integer(0)), RepeatUntil(statement: Assignment(left: Variable(name: "x"), right: BinaryOperation(left: Variable(name: "x"), operation: BinaryOperationType.plus, right: Number.integer(1))), condition: Condition(type: .equals, leftSide: Variable(name: "x"), rightSide: Number.integer(6))), FunctionCall(name: "writeln", actualParameters: [Variable(name: "x")]), NoOp()])
427+
let block = Block(declarations: [xDec], compound: compound)
428+
let node = Program(name: "Main", block: block)
429+
XCTAssertEqual(result, node)
425430
}
426431

427432
}

PascalInterpreter/PascalInterpreterTests/XCTestCase+FatalError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func XCTAssertEqual(_ left: AST, _ right: AST) {
134134
XCTAssert(left == right)
135135
case let (left as Bool, right as Bool):
136136
XCTAssert(left == right)
137+
case let (left as RepeatUntil, right as RepeatUntil):
138+
XCTAssertEqual(left.condition, right.condition)
139+
XCTAssertEqual(left.statement, right.statement)
137140
default:
138141
XCTFail("\(left) and \(right) are not equal")
139142
}

0 commit comments

Comments
 (0)