Skip to content

Commit d355868

Browse files
committed
add type hints to testing framework
1 parent 2093411 commit d355868

5 files changed

Lines changed: 95 additions & 41 deletions

File tree

tests/framework/base_control.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@
1919
import shutil
2020
import subprocess
2121

22+
from typing import List, Optional
23+
24+
from hvcc.interpreters.pd2hv.NotificationEnum import NotificationEnum
2225
from tests.framework.base_test import HvBaseTest
2326

2427

2528
class TestPdControlBase(HvBaseTest):
2629

2730
def compile_and_run(
2831
self,
29-
source_files,
30-
out_dir,
31-
num_iterations,
32-
flag=None
33-
):
32+
source_files: List[str],
33+
out_dir: str,
34+
num_iterations: int,
35+
flag: Optional[str] = None
36+
) -> List[str]:
3437
exe_path = self._compile_and_run(source_files, out_dir, flag)
3538

3639
# run executable (returns stdout)
@@ -41,21 +44,34 @@ def compile_and_run(
4144

4245
return [x.decode('utf-8') for x in output]
4346

44-
def create_fail_message(self, result, golden, flag=None):
47+
def create_fail_message(
48+
self,
49+
result: str,
50+
golden: str,
51+
flag: Optional[str] = None
52+
) -> str:
4553
return "\nResult ({0})\n-----------\n{1}\n\nGolden\n-----------\n{2}".format(
4654
flag or "",
4755
"\n".join(result),
4856
"\n".join(golden))
4957

50-
def _test_control_patch_expect_error(self, pd_file, expected_enum):
58+
def _test_control_patch_expect_error(
59+
self,
60+
pd_file: str,
61+
expected_enum: NotificationEnum
62+
) -> None:
5163
pd_path = os.path.join(self.TEST_DIR, pd_file)
5264

5365
try:
5466
self._run_hvcc(pd_path, expect_fail=True, expected_enum=expected_enum)
5567
except Exception as e:
5668
self.fail(str(e))
5769

58-
def _test_control_patch_expect_warning(self, pd_file, expected_enum):
70+
def _test_control_patch_expect_warning(
71+
self,
72+
pd_file: str,
73+
expected_enum: NotificationEnum
74+
) -> None:
5975
# setup
6076
pd_path = os.path.join(self.TEST_DIR, pd_file)
6177

@@ -64,7 +80,13 @@ def _test_control_patch_expect_warning(self, pd_file, expected_enum):
6480
except Exception as e:
6581
self.fail(str(e))
6682

67-
def _test_control_patch(self, pd_file, num_iterations=1, allow_warnings=True, fail_message=None):
83+
def _test_control_patch(
84+
self,
85+
pd_file: str,
86+
num_iterations: int = 1,
87+
allow_warnings: bool = True,
88+
fail_message: Optional[str] = None
89+
) -> None:
6890
"""Compiles, runs, and tests a control patch.
6991
Allows warnings by default, always fails on errors.
7092
@param fail_message An optional message displayed in case of test failure.

tests/framework/base_midi.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@
1919
import shutil
2020
import subprocess
2121

22+
from typing import List, Optional
23+
24+
from hvcc.interpreters.pd2hv.NotificationEnum import NotificationEnum
2225
from tests.framework.base_test import HvBaseTest
2326

2427

2528
class TestPdMIDIBase(HvBaseTest):
2629

2730
def compile_and_run(
2831
self,
29-
source_files,
30-
out_dir,
31-
num_iterations,
32-
flag=None
33-
):
32+
source_files: List[str],
33+
out_dir: str,
34+
num_iterations: int,
35+
flag: Optional[str] = None
36+
) -> List[str]:
3437
exe_path = self._compile_and_run(source_files, out_dir, flag)
3538

3639
# run executable (returns stdout)
@@ -42,21 +45,34 @@ def compile_and_run(
4245

4346
return [x.decode('utf-8') for x in output]
4447

45-
def create_fail_message(self, result, golden, flag=None):
48+
def create_fail_message(
49+
self,
50+
result: str,
51+
golden: str,
52+
flag: Optional[str] = None
53+
) -> str:
4654
return "\nResult ({0})\n-----------\n{1}\n\nGolden\n-----------\n{2}".format(
4755
flag or "",
4856
"\n".join(result),
4957
"\n".join(golden))
5058

51-
def _test_control_patch_expect_error(self, pd_file, expected_enum):
59+
def _test_control_patch_expect_error(
60+
self,
61+
pd_file: str,
62+
expected_enum: NotificationEnum
63+
) -> None:
5264
pd_path = os.path.join(self.TEST_DIR, pd_file)
5365

5466
try:
5567
self._run_hvcc(pd_path, expect_fail=True, expected_enum=expected_enum)
5668
except Exception as e:
5769
self.fail(str(e))
5870

59-
def _test_midi_patch_expect_warning(self, pd_file, expected_enum):
71+
def _test_control_patch_expect_warning(
72+
self,
73+
pd_file: str,
74+
expected_enum: NotificationEnum
75+
) -> None:
6076
# setup
6177
pd_path = os.path.join(self.TEST_DIR, pd_file)
6278

@@ -65,7 +81,13 @@ def _test_midi_patch_expect_warning(self, pd_file, expected_enum):
6581
except Exception as e:
6682
self.fail(str(e))
6783

68-
def _test_midi_patch(self, pd_file, num_iterations=1, allow_warnings=True, fail_message=None):
84+
def _test_midi_patch(
85+
self,
86+
pd_file: str,
87+
num_iterations: int = 1,
88+
allow_warnings: bool = True,
89+
fail_message: Optional[str] = None
90+
) -> None:
6991
"""Compiles, runs, and tests a control patch.
7092
Allows warnings by default, always fails on errors.
7193
@param fail_message An optional message displayed in case of test failure.

tests/framework/base_signal.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import subprocess
2121
import numpy
2222

23+
from typing import List, Optional
24+
2325
from scipy.io import wavfile
2426

2527
from tests.framework.base_test import HvBaseTest
@@ -29,12 +31,12 @@ class TestPdSignalBase(HvBaseTest):
2931

3032
def compile_and_run(
3133
self,
32-
source_files,
33-
out_dir,
34-
sample_rate=None,
35-
block_size=None,
36-
num_iterations=None,
37-
flag=None
34+
source_files: List[str],
35+
out_dir: str,
36+
sample_rate: Optional[int] = None,
37+
block_size: Optional[int] = None,
38+
num_iterations: Optional[int] = None,
39+
flag: Optional[str] = None
3840
):
3941
exe_path = self._compile_and_run(source_files, out_dir, flag)
4042

@@ -50,7 +52,13 @@ def compile_and_run(
5052

5153
return wav_path
5254

53-
def _compare_wave_output(self, out_dir, c_sources, golden_path, flag=None):
55+
def _compare_wave_output(
56+
self,
57+
out_dir: str,
58+
c_sources: List[str],
59+
golden_path: str,
60+
flag: Optional[str] = None
61+
):
5462
# http://stackoverflow.com/questions/10580676/comparing-two-numpy-arrays-for-equality-element-wise
5563
# http://docs.scipy.org/doc/numpy/reference/routines.testing.html
5664

@@ -69,7 +77,7 @@ def _compare_wave_output(self, out_dir, c_sources, golden_path, flag=None):
6977
except AssertionError as e:
7078
self.fail(e)
7179

72-
def _test_signal_patch(self, pd_file):
80+
def _test_signal_patch(self, pd_file: str):
7381
"""Compiles, runs, and tests a signal patch.
7482
"""
7583

tests/framework/base_speed.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@
1919
import shutil
2020
import subprocess
2121

22+
from typing import List, Optional
23+
2224
from tests.framework.base_test import HvBaseTest
2325

2426

2527
class TestPdSpeedBase(HvBaseTest):
2628

2729
def compile_and_run(
2830
self,
29-
source_files,
30-
out_dir,
31-
sample_rate=None,
32-
block_size=None,
33-
num_iterations=None,
34-
flag=None
31+
source_files: List[str],
32+
out_dir: str,
33+
sample_rate: Optional[int] = None,
34+
block_size: Optional[int] = None,
35+
num_iterations: Optional[int] = None,
36+
flag: Optional[str] = None
3537
):
3638
exe_path = self._compile_and_run_clang(source_files, out_dir, flag)
3739

@@ -44,7 +46,7 @@ def compile_and_run(
4446

4547
return float(result)
4648

47-
def _test_speed_patch(self, pd_file):
49+
def _test_speed_patch(self, pd_file: str):
4850
pd_path = os.path.join(self.TEST_DIR, pd_file)
4951
# out_dir = os.path.join(os.path.dirname(__file__), "build")
5052

tests/framework/base_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import subprocess
2121
import unittest
2222

23-
from typing import Optional
23+
from typing import List, Optional
2424

2525
import hvcc
2626

@@ -48,7 +48,7 @@ def setUp(self):
4848

4949
def _run_hvcc(
5050
self,
51-
pd_path,
51+
pd_path: str,
5252
expect_warning: bool = False,
5353
expect_fail: bool = False,
5454
expected_enum: NotificationEnum = NotificationEnum.EMPTY
@@ -97,9 +97,9 @@ def _run_hvcc(
9797

9898
def _compile_and_run(
9999
self,
100-
source_files,
101-
out_dir,
102-
flag=None
100+
source_files: List[str],
101+
out_dir: str,
102+
flag: Optional[str] = None
103103
):
104104
exe_path = os.path.join(out_dir, "heavy")
105105

@@ -119,9 +119,9 @@ def _compile_and_run(
119119

120120
def _compile_and_run_clang(
121121
self,
122-
source_files,
123-
out_dir,
124-
flag=None,
122+
source_files: List[str],
123+
out_dir: str,
124+
flag: Optional[str] = None,
125125
):
126126
flag = flag or "HV_SIMD_NONE"
127127
self.assertTrue(flag in simd_flags, f"Unknown compiler flag: {flag}")

0 commit comments

Comments
 (0)