Skip to content

Commit ec20af3

Browse files
committed
better error messages
1 parent 25a4b7b commit ec20af3

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

tests/_static_analysis.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1+
from checkpy import *
12
from typing import Any
23
import ast
34

45
from checkpy import static
56

7+
def defines_function(name: str) -> bool:
8+
check = name in static.getFunctionDefinitions()
9+
if not check:
10+
raise AssertionError(f"`{name}` is niet aanwezig")
11+
return check
12+
13+
def not_in_code(construct: type):
14+
check = construct in static.AbstractSyntaxTree()
15+
name = str(construct).split(".")[1].split("'")[0].lower()
16+
if check:
17+
raise AssertionError(f"`{name}` mag niet gebruikt worden in deze opdracht")
18+
return check
19+
20+
def in_code(construct: type):
21+
check = construct in static.AbstractSyntaxTree()
22+
name = str(construct).split(".")[1].split("'")[0].lower()
23+
if not check:
24+
raise AssertionError(f"`{name}` moet gebruikt worden in deze opdracht")
25+
return check
26+
627
def has_syntax_error():
728
try:
829
compile(static.getSource(), "<your program>", "exec")

tests/acidTest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from checkpy import *
22
from _basics_no_listcomp import *
3+
from _static_analysis import *
34

45
@t.passed(doctest_ok)
56
def has_functions():
67
"""functie `is_acidic` is aanwezig"""
7-
assert "is_acidic" in static.getFunctionDefinitions(), "`is_acidic` is niet aanwezig"
8+
assert defines_function("is_acidic")
89

910
@t.passed(has_functions)
1011
def test_weeks_elapsed(test):

tests/colaTest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import checkpy.assertlib as asserts
44

55
from _basics_no_listcomp import *
6+
from _static_analysis import *
67

78
@t.passed(doctest_ok)
89
def has_functions():
910
"""alle gevraagde functies zijn aanwezig"""
10-
assert "check_coin" in static.getFunctionDefinitions(), "`check_coin` is niet aanwezig"
11-
assert "determine_due" in static.getFunctionDefinitions(), "`determine_due` is niet aanwezig"
12-
assert "prompt_coin" not in static.getFunctionDefinitions(), "`prompt_coin` is aanwezig, maar dat staat niet in de opdracht"
13-
assert ast.While in static.AbstractSyntaxTree(), "er wordt geen gebruik gemaakt van een `while`-loop"
11+
assert defines_function("check_coin")
12+
assert defines_function("determine_due")
13+
assert defines_function("prompt_coin")
14+
assert in_code(ast.While)
1415

1516
@t.passed(doctest_ok)
1617
@t.test(10)

tests/fdrTest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from checkpy import *
22
from _basics_no_listcomp import *
3+
from _static_analysis import *
34

45
@t.passed(doctest_ok)
56
def has_functions():
67
"""alle gevraagde functies zijn aanwezig"""
7-
assert "pie_percent" in static.getFunctionDefinitions(), "`pie_percent` is niet aanwezig"
8-
assert "triple" in static.getFunctionDefinitions(), "`triple` is niet aanwezig"
9-
assert "absdiff" in static.getFunctionDefinitions(), "`absdiff` is niet aanwezig"
10-
assert "kmmiles" in static.getFunctionDefinitions(), "`kmmiles` is niet aanwezig"
11-
assert "avg3" in static.getFunctionDefinitions(), "`avg3` is niet aanwezig"
12-
assert "avg3of4" in static.getFunctionDefinitions(), "`avg3of4` is niet aanwezig"
8+
assert defines_function("pie_percent")
9+
assert defines_function("triple")
10+
assert defines_function("absdiff")
11+
assert defines_function("kmmiles")
12+
assert defines_function("avg3")
13+
assert defines_function("avg3of4")
1314

1415
@t.passed(has_functions)
1516
def test_pie_percent(test):

tests/string_functionsTest.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
@t.passed(doctest_ok)
88
def has_functions():
99
"""alle gevraagde functies zijn aanwezig"""
10-
assert "repeat" in static.getFunctionDefinitions(), "`repeat` is niet aanwezig"
11-
assert "total_length" in static.getFunctionDefinitions(), "`total_length` is niet aanwezig"
12-
13-
assert ast.If not in static.AbstractSyntaxTree()
14-
assert ast.While not in static.AbstractSyntaxTree()
15-
assert ast.For not in static.AbstractSyntaxTree()
10+
assert defines_function("repeat")
11+
assert defines_function("total_length")
12+
assert not_in_code(ast.If)
13+
assert not_in_code(ast.While)
14+
assert not_in_code(ast.For)
1615

1716
@t.passed(has_functions)
1817
def test_repeat(test):

tests/wekenTest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from checkpy import *
22
from _basics_no_listcomp import *
3+
from _static_analysis import *
34

45
@t.passed(doctest_ok)
56
def has_functions():
67
"""functie `weeks_elapsed` is aanwezig"""
7-
assert "weeks_elapsed" in static.getFunctionDefinitions(), "`weeks_elapsed` is niet aanwezig"
8+
assert defines_function("weeks_elapsed")
89

910
@t.passed(has_functions)
1011
def test_weeks_elapsed(test):

0 commit comments

Comments
 (0)