-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1302 from benoit-pierre/improve_dictionary_implem…
…entation_test_framework Improve dictionary implementation test framework
- Loading branch information
Showing
26 changed files
with
966 additions
and
533 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Add some new helpers to `plover_build_utils.testing`: | ||
- `dictionary_test`: torture tests for dictionary implementations. | ||
- `make_dict`: create a temporary dictionary. | ||
- `parametrize`: parametrize helper for tracking test data source line numbers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fix 2 corner cases when handling dictionaries: | ||
- If the class implementation is marked as read-only, then loading from a writable file should still result in a read-only dictionary. | ||
- Don't allow `clear` on a read-only dictionary. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .blackbox import blackbox_test | ||
from .dict import make_dict | ||
from .output import CaptureOutput | ||
from .parametrize import parametrize | ||
from .steno import steno_to_stroke | ||
from .steno_dictionary import dictionary_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
import os | ||
import tempfile | ||
|
||
|
||
@contextmanager | ||
def make_dict(tmp_path, contents, extension=None, name=None): | ||
kwargs = {'dir': str(tmp_path)} | ||
if name is not None: | ||
kwargs['prefix'] = name + '_' | ||
if extension is not None: | ||
kwargs['suffix'] = '.' + extension | ||
fd, path = tempfile.mkstemp(**kwargs) | ||
try: | ||
os.write(fd, contents) | ||
os.close(fd) | ||
yield Path(path) | ||
finally: | ||
os.unlink(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class CaptureOutput: | ||
|
||
def __init__(self): | ||
self.instructions = [] | ||
self.text = '' | ||
|
||
def send_backspaces(self, n): | ||
assert n <= len(self.text) | ||
self.text = self.text[:-n] | ||
self.instructions.append(('b', n)) | ||
|
||
def send_string(self, s): | ||
self.text += s | ||
self.instructions.append(('s', s)) | ||
|
||
def send_key_combination(self, c): | ||
self.instructions.append(('c', c)) | ||
|
||
def send_engine_command(self, c): | ||
self.instructions.append(('e', c)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import inspect | ||
|
||
import pytest | ||
|
||
|
||
def parametrize(tests, arity=None): | ||
'''Helper for parametrizing pytest tests. | ||
Expects a list of lambdas, one per test. Each lambda must return | ||
the parameters for its respective test. | ||
Test identifiers will be automatically generated, from the test | ||
number and its lambda definition line (1.10, 2.12, 3.20, ...). | ||
If arity is None, the arguments being parametrized will be automatically | ||
set from the function's last arguments, according to the numbers of | ||
parameters for each test. | ||
''' | ||
ids = [] | ||
argvalues = [] | ||
for n, t in enumerate(tests): | ||
line = inspect.getsourcelines(t)[1] | ||
ids.append('%u:%u' % (n+1, line)) | ||
argvalues.append(t()) | ||
if arity is None: | ||
arity = len(argvalues[0]) | ||
assert arity > 0 | ||
def decorator(fn): | ||
argnames = list( | ||
parameter.name | ||
for parameter in inspect.signature(fn).parameters.values() | ||
if parameter.default is inspect.Parameter.empty | ||
)[-arity:] | ||
if arity == 1: | ||
argnames = argnames[0] | ||
return pytest.mark.parametrize(argnames, argvalues, ids=ids)(fn) | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from plover import system | ||
from plover.steno import Stroke | ||
|
||
|
||
def steno_to_stroke(steno): | ||
# Check if the system changed, or | ||
# we need to perform initial setup. | ||
if steno_to_stroke.system != system.NAME: | ||
keys = [] | ||
letters = '' | ||
has_hyphen = False | ||
for k in system.KEYS: | ||
if not has_hyphen and k.startswith('-'): | ||
has_hyphen = True | ||
keys.append(None) | ||
letters += '-' | ||
keys.append(k) | ||
letters += k.strip('-') | ||
steno_to_stroke.keys = keys | ||
steno_to_stroke.letters = letters | ||
steno_to_stroke.system = system.NAME | ||
steno_to_stroke.numbers = { | ||
v.strip('-'): k.strip('-') | ||
for k, v in system.NUMBERS.items() | ||
} | ||
n = -1 | ||
keys = set() | ||
for li, l in enumerate(steno): | ||
rl = steno_to_stroke.numbers.get(l) | ||
if rl is not None: | ||
keys.add('#') | ||
l = rl | ||
n = steno_to_stroke.letters.find(l, n + 1) | ||
if n < 0: | ||
raise ValueError('invalid steno letter at index %u:\n%s\n%s^' % (li, steno, ' ' * li)) | ||
k = steno_to_stroke.keys[n] | ||
if k is not None: | ||
keys.add(k) | ||
return Stroke(keys) | ||
|
||
steno_to_stroke.system = None |
Oops, something went wrong.