tests(checker): add control flow test
This commit is contained in:
25
tests/cases/checker/05_control_flow.py
Normal file
25
tests/cases/checker/05_control_flow.py
Normal 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"
|
||||||
46
tests/cases/checker/05_control_flow.py.ref.json
Normal file
46
tests/cases/checker/05_control_flow.py.ref.json
Normal 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')]"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user