|
13 | 13 | def basic_style(test):
|
14 | 14 | """het bestand is in orde"""
|
15 | 15 | def testMethod():
|
16 |
| - # if lineno := has_syntax_error(): |
17 |
| - # return False, f"de code bevat een syntax error op regel {lineno}" |
18 |
| - # if has_string(" "): |
19 |
| - # return False, "let op dat je geen tabs gebruikt" |
20 |
| - # if has_string("Optional"): |
21 |
| - # return False, "let op dat je niet Optional[...] gebruikt als type hint maar ... | None" |
22 |
| - # if has_string("List[", "Tuple[", "Dict[", "Set["): |
23 |
| - # return False, "let op dat je niet List[...] e.d. gebruikt als type hint maar list[...]" |
24 |
| - # # if has_call('min', 'max'): |
25 |
| - # # return False, "let op dat je geen min() of max() gebruikt" |
26 |
| - # if has_call('sorted'): |
27 |
| - # return False, "let op dat je geen sorted() gebruikt" |
28 |
| - # if has_call('map'): |
29 |
| - # return False, "let op dat je geen map() gebruikt" |
30 |
| - # if has_call('eval'): |
31 |
| - # return False, "let op dat je geen eval() gebruikt" |
32 |
| - # if has_import('math'): |
33 |
| - # return False, "let op dat je geen import math gebruikt" |
34 |
| - # |
35 |
| - # # run pycodestyle for a couple of basic checks |
36 |
| - # try: |
37 |
| - # max_line_length = os.environ['MAX_LINE_LENGTH'] |
38 |
| - # except KeyError: |
39 |
| - # max_line_length = 99 |
40 |
| - # try: |
41 |
| - # max_doc_length = os.environ['MAX_DOC_LENGTH'] |
42 |
| - # except KeyError: |
43 |
| - # max_doc_length = 79 |
44 |
| - # p = subprocess.run([ |
45 |
| - # 'pycodestyle', |
46 |
| - # '--select=E101,E112,E113,E115,E116,E117,E501,E502,W505,W291', |
47 |
| - # f"--max-line-length={max_line_length}", |
48 |
| - # f"--max-doc-length={max_doc_length}", |
49 |
| - # test.fileName |
50 |
| - # ], capture_output=True, universal_newlines=True) |
51 |
| - # if p.returncode != 0: |
52 |
| - # if "E1" in p.stdout: |
53 |
| - # test.fail = lambda info : f"let op juiste indentatie" |
54 |
| - # return False, p.stdout |
55 |
| - # if "E501" in p.stdout or "W505" in p.stdout: |
56 |
| - # test.fail = lambda info : f"regel(s) te lang, code max {max_line_length} tekens, comments max {max_doc_length} tekens" |
57 |
| - # return False, p.stdout |
58 |
| - # if "E502" in p.stdout: |
59 |
| - # test.fail = lambda info: f"gebruik tussen haakjes geen \\ om de regel af te breken" |
60 |
| - # return False, p.stdout |
61 |
| - # if "W291" in p.stdout: |
62 |
| - # pattern = r'[^:\n]+:(\d+):\d+: W291' |
63 |
| - # matches = re.findall(pattern, p.stdout) |
64 |
| - # test.fail = lambda info: f"zorg dat er geen spaties aan het eind van een regel staan (regel {', '.join(matches)})" |
65 |
| - # return False, p.stdout |
| 16 | + if lineno := has_syntax_error(): |
| 17 | + return False, f"de code bevat een syntax error op regel {lineno}" |
| 18 | + if has_string(" "): |
| 19 | + return False, "let op dat je geen tabs gebruikt" |
| 20 | + if has_string("Optional"): |
| 21 | + return False, "let op dat je niet Optional[...] gebruikt als type hint maar ... | None" |
| 22 | + if has_string("List[", "Tuple[", "Dict[", "Set["): |
| 23 | + return False, "let op dat je niet List[...] e.d. gebruikt als type hint maar list[...]" |
| 24 | + # if has_call('min', 'max'): |
| 25 | + # return False, "let op dat je geen min() of max() gebruikt" |
| 26 | + if has_call('sorted'): |
| 27 | + return False, "let op dat je geen sorted() gebruikt" |
| 28 | + if has_call('map'): |
| 29 | + return False, "let op dat je geen map() gebruikt" |
| 30 | + if has_call('eval'): |
| 31 | + return False, "let op dat je geen eval() gebruikt" |
| 32 | + if has_import('math'): |
| 33 | + return False, "let op dat je geen import math gebruikt" |
| 34 | + |
| 35 | + # run pycodestyle for a couple of basic checks |
| 36 | + try: |
| 37 | + max_line_length = os.environ['MAX_LINE_LENGTH'] |
| 38 | + except KeyError: |
| 39 | + max_line_length = 99 |
| 40 | + try: |
| 41 | + max_doc_length = os.environ['MAX_DOC_LENGTH'] |
| 42 | + except KeyError: |
| 43 | + max_doc_length = 79 |
| 44 | + p = subprocess.run([ |
| 45 | + 'pycodestyle', |
| 46 | + '--select=E101,E112,E113,E115,E116,E117,E501,E502,W505,W291', |
| 47 | + f"--max-line-length={max_line_length}", |
| 48 | + f"--max-doc-length={max_doc_length}", |
| 49 | + test.fileName |
| 50 | + ], capture_output=True, universal_newlines=True) |
| 51 | + if p.returncode != 0: |
| 52 | + if "E1" in p.stdout: |
| 53 | + test.fail = lambda info : f"let op juiste indentatie" |
| 54 | + return False, p.stdout |
| 55 | + if "E501" in p.stdout or "W505" in p.stdout: |
| 56 | + test.fail = lambda info : f"regel(s) te lang, code max {max_line_length} tekens, comments max {max_doc_length} tekens" |
| 57 | + return False, p.stdout |
| 58 | + if "E502" in p.stdout: |
| 59 | + test.fail = lambda info: f"gebruik tussen haakjes geen \\ om de regel af te breken" |
| 60 | + return False, p.stdout |
| 61 | + if "W291" in p.stdout: |
| 62 | + pattern = r'[^:\n]+:(\d+):\d+: W291' |
| 63 | + matches = re.findall(pattern, p.stdout) |
| 64 | + test.fail = lambda info: f"zorg dat er geen spaties aan het eind van een regel staan (regel {', '.join(matches)})" |
| 65 | + return False, p.stdout |
66 | 66 | return True
|
67 | 67 | test.test = testMethod
|
68 | 68 |
|
69 |
| -# @t.passed(basic_style, hide=False) |
70 |
| -# @t.test(2) |
71 |
| -# def mypy_ok(test): |
72 |
| -# """type hints zijn ingevuld en consistent bevonden""" |
73 |
| -# def testMethod(): |
74 |
| -# p = subprocess.run(['mypy', '--strict', '--ignore-missing-imports', '--disable-error-code=name-defined', test.fileName], capture_output=True, universal_newlines=True) |
75 |
| -# return p.returncode == 0, p.stdout |
76 |
| -# test.test = testMethod |
77 |
| -# def report(output): |
78 |
| -# return '- line ' + '\n- line '.join([':'.join(i.split(':')[1:])[:60] for i in output.splitlines()[:-1]]) |
79 |
| -# test.fail = report |
| 69 | +@t.passed(basic_style, hide=False) |
| 70 | +@t.test(2) |
| 71 | +def mypy_ok(test): |
| 72 | + """type hints zijn ingevuld en consistent bevonden""" |
| 73 | + def testMethod(): |
| 74 | + p = subprocess.run(['mypy', '--strict', '--ignore-missing-imports', '--disable-error-code=name-defined', test.fileName], capture_output=True, universal_newlines=True) |
| 75 | + return p.returncode == 0, p.stdout |
| 76 | + test.test = testMethod |
| 77 | + def report(output): |
| 78 | + return '- line ' + '\n- line '.join([':'.join(i.split(':')[1:])[:60] for i in output.splitlines()[:-1]]) |
| 79 | + test.fail = report |
80 | 80 |
|
81 |
| -# @t.passed(mypy_ok, hide=False) |
| 81 | +@t.passed(mypy_ok, hide=False) |
82 | 82 | @t.test(3)
|
83 | 83 | def doctest_ok(test):
|
84 | 84 | """doctests zijn voldoende aanwezig en geven allemaal akkoord"""
|
85 | 85 | def testMethod():
|
86 |
| - # with open(test.fileName, 'r') as source_file: |
87 |
| - # source = source_file.read() |
88 |
| - # functions = re.findall(r'def\s+(\w+)\(([^\)]*)\)[^-]+(->\s*([\w\[,\] _]+))?:', source) |
89 |
| - # n_functions_not_returning = len([function for function in functions if ('file' in function[1] or function[3] == 'None' or function[3] == '')]) |
90 |
| - # n_functions = len(functions) |
91 |
| - # p = subprocess.run([sys.executable or 'python3', '-m', 'doctest', '-v', test.fileName], capture_output=True, universal_newlines=True) |
92 |
| - # if "Traceback" in p.stderr: |
93 |
| - # return False, p.stderr.splitlines()[-1] |
94 |
| - # test_stats_rex = re.compile('(\d*) tests in (\d*) items') |
95 |
| - # test_pass_rex = re.compile('(\d*) passed and (\d*) failed') |
96 |
| - # test_stats = test_stats_rex.search(p.stdout.splitlines()[-3]) |
97 |
| - # test_pass = test_pass_rex.search(p.stdout.splitlines()[-2]) |
98 |
| - # n_tests = int(test_stats.group(1)) |
99 |
| - # n_items = int(test_stats.group(2))-1 |
100 |
| - # n_tested = n_items-n_functions_not_returning |
101 |
| - # n_pass = int(test_pass.group(1)) |
102 |
| - # if n_functions == 0: |
103 |
| - # return False, "je programma moet functies gebruiken (of type hints ontbreken!)" |
104 |
| - # elif n_tested == 0: |
105 |
| - # # geen testbare functies blijkbaar? |
106 |
| - # return True |
107 |
| - # elif n_tested > 0 and n_tests // n_tested < 2: |
108 |
| - # return False, f"{n_tests} voorbeelden bij {n_tested} testbare functies is niet genoeg \n (we tellen alleen functies die iets returnen)" |
109 |
| - # elif n_pass < n_tests: |
110 |
| - # return False, f"{n_pass} van {n_tests} voorbeelden slagen" |
| 86 | + with open(test.fileName, 'r') as source_file: |
| 87 | + source = source_file.read() |
| 88 | + functions = re.findall(r'def\s+(\w+)\(([^\)]*)\)[^-]+(->\s*([\w\[,\] _]+))?:', source) |
| 89 | + n_functions_not_returning = len([function for function in functions if ('file' in function[1] or function[3] == 'None' or function[3] == '')]) |
| 90 | + n_functions = len(functions) |
| 91 | + p = subprocess.run([sys.executable or 'python3', '-m', 'doctest', '-v', test.fileName], capture_output=True, universal_newlines=True) |
| 92 | + if "Traceback" in p.stderr: |
| 93 | + return False, p.stderr.splitlines()[-1] |
| 94 | + test_stats_rex = re.compile('(\d*) tests in (\d*) items') |
| 95 | + test_pass_rex = re.compile('(\d*) passed and (\d*) failed') |
| 96 | + test_stats = test_stats_rex.search(p.stdout.splitlines()[-3]) |
| 97 | + test_pass = test_pass_rex.search(p.stdout.splitlines()[-2]) |
| 98 | + n_tests = int(test_stats.group(1)) |
| 99 | + n_items = int(test_stats.group(2))-1 |
| 100 | + n_tested = n_items-n_functions_not_returning |
| 101 | + n_pass = int(test_pass.group(1)) |
| 102 | + if n_functions == 0: |
| 103 | + return False, "je programma moet functies gebruiken (of type hints ontbreken!)" |
| 104 | + elif n_tested == 0: |
| 105 | + # geen testbare functies blijkbaar? |
| 106 | + return True |
| 107 | + elif n_tested > 0 and n_tests // n_tested < 2: |
| 108 | + return False, f"{n_tests} voorbeelden bij {n_tested} testbare functies is niet genoeg \n (we tellen alleen functies die iets returnen)" |
| 109 | + elif n_pass < n_tests: |
| 110 | + return False, f"{n_pass} van {n_tests} voorbeelden slagen" |
111 | 111 | return True
|
112 | 112 |
|
113 | 113 | test.test = testMethod
|
|
0 commit comments