|
|
|
@ -201,4 +201,108 @@ print c;
|
|
|
|
System.setOut(standardOut)
|
|
|
|
System.setOut(standardOut)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
fun `If statement should work`() {
|
|
|
|
|
|
|
|
val standardOut = System.out
|
|
|
|
|
|
|
|
val outputStreamCaptor = ByteArrayOutputStream()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.setOut(PrintStream(outputStreamCaptor))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
val code = """
|
|
|
|
|
|
|
|
if (1 < 2) print "true";
|
|
|
|
|
|
|
|
""".trimIndent()
|
|
|
|
|
|
|
|
val scanner = Scanner(code)
|
|
|
|
|
|
|
|
val tokens = scanner.scanTokens()
|
|
|
|
|
|
|
|
val parser = Parser(tokens)
|
|
|
|
|
|
|
|
val statements = parser.parse()
|
|
|
|
|
|
|
|
assertEquals(1, statements.size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Interpreter().interpret(statements)
|
|
|
|
|
|
|
|
val output = outputStreamCaptor.toString().trim()
|
|
|
|
|
|
|
|
assertEquals("true", output)
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
System.setOut(standardOut)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
fun `If statement with else should work`() {
|
|
|
|
|
|
|
|
val standardOut = System.out
|
|
|
|
|
|
|
|
val outputStreamCaptor = ByteArrayOutputStream()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.setOut(PrintStream(outputStreamCaptor))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
val code = """
|
|
|
|
|
|
|
|
if (1 > 2) print "true";
|
|
|
|
|
|
|
|
else print "false";
|
|
|
|
|
|
|
|
""".trimIndent()
|
|
|
|
|
|
|
|
val scanner = Scanner(code)
|
|
|
|
|
|
|
|
val tokens = scanner.scanTokens()
|
|
|
|
|
|
|
|
val parser = Parser(tokens)
|
|
|
|
|
|
|
|
val statements = parser.parse()
|
|
|
|
|
|
|
|
assertEquals(1, statements.size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Interpreter().interpret(statements)
|
|
|
|
|
|
|
|
val output = outputStreamCaptor.toString().trim()
|
|
|
|
|
|
|
|
assertEquals("false", output)
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
System.setOut(standardOut)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
fun `If statement with else if should work`() {
|
|
|
|
|
|
|
|
val standardOut = System.out
|
|
|
|
|
|
|
|
val outputStreamCaptor = ByteArrayOutputStream()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.setOut(PrintStream(outputStreamCaptor))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
val code = """
|
|
|
|
|
|
|
|
if (1 > 2) print "true";
|
|
|
|
|
|
|
|
else if (1 < 2) print "false";
|
|
|
|
|
|
|
|
""".trimIndent()
|
|
|
|
|
|
|
|
val scanner = Scanner(code)
|
|
|
|
|
|
|
|
val tokens = scanner.scanTokens()
|
|
|
|
|
|
|
|
val parser = Parser(tokens)
|
|
|
|
|
|
|
|
val statements = parser.parse()
|
|
|
|
|
|
|
|
assertEquals(1, statements.size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Interpreter().interpret(statements)
|
|
|
|
|
|
|
|
val output = outputStreamCaptor.toString().trim()
|
|
|
|
|
|
|
|
assertEquals("false", output)
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
System.setOut(standardOut)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
fun `If statement with else if and else should work`() {
|
|
|
|
|
|
|
|
val standardOut = System.out
|
|
|
|
|
|
|
|
val outputStreamCaptor = ByteArrayOutputStream()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.setOut(PrintStream(outputStreamCaptor))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
val code = """
|
|
|
|
|
|
|
|
if (1 > 2) print "true";
|
|
|
|
|
|
|
|
else if (1 < 2) print "false";
|
|
|
|
|
|
|
|
else print "else";
|
|
|
|
|
|
|
|
""".trimIndent()
|
|
|
|
|
|
|
|
val scanner = Scanner(code)
|
|
|
|
|
|
|
|
val tokens = scanner.scanTokens()
|
|
|
|
|
|
|
|
val parser = Parser(tokens)
|
|
|
|
|
|
|
|
val statements = parser.parse()
|
|
|
|
|
|
|
|
assertEquals(1, statements.size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Interpreter().interpret(statements)
|
|
|
|
|
|
|
|
val output = outputStreamCaptor.toString().trim()
|
|
|
|
|
|
|
|
assertEquals("false", output)
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
System.setOut(standardOut)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|