tests(checker): add control flow test

This commit is contained in:
2026-06-01 14:22:03 +02:00
parent f4dc57cb96
commit bd31713ab4
3 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
def valid(a: int, b: int) -> int:
return a + b
def with_if(a: int, b: int) -> int:
if a < b:
return b - a
else:
return a - b
def unreachable1():
return
a = 0
def unreachable2(a: int) -> int:
if a > 10:
return a - 10
else:
return a
b = 0
def mixed(a: int, b: int):
if a < b:
return b - a
else:
return "oops"

View File

@@ -0,0 +1,46 @@
{
"diagnostics": [
{
"type": "Warning",
"location": {
"start": [
12,
4
],
"end": [
12,
9
]
},
"message": "Unreachable statement"
},
{
"type": "Warning",
"location": {
"start": [
19,
4
],
"end": [
19,
9
]
},
"message": "Unreachable statement"
},
{
"type": "Error",
"location": {
"start": [
21,
0
],
"end": [
25,
21
]
},
"message": "Mixed return types: [BaseType(name='int'), BaseType(name='str')]"
}
]
}

View File

@@ -15,6 +15,7 @@ from midas.ast.python import (
FrameType, FrameType,
Function, Function,
GetExpr, GetExpr,
IfStmt,
LiteralExpr, LiteralExpr,
LogicalExpr, LogicalExpr,
MidasType, MidasType,
@@ -164,6 +165,14 @@ class PythonAstJsonSerializer(
"value": self._serialize_optional(stmt.value), "value": self._serialize_optional(stmt.value),
} }
def visit_if_stmt(self, stmt: IfStmt) -> dict:
return {
"_type": "IfStmt",
"test": stmt.test.accept(self),
"body": self._serialize_list(stmt.body),
"orelse": self._serialize_list(stmt.orelse),
}
def visit_binary_expr(self, expr: BinaryExpr) -> dict: def visit_binary_expr(self, expr: BinaryExpr) -> dict:
return { return {
"_type": "BinaryExpr", "_type": "BinaryExpr",