| 1 |
import pytest |
| 2 |
from autocommand.autoparse import AnnotationError |
| 3 |
|
| 4 |
|
| 5 |
@pytest.mark.parametrize("type_object", [ |
| 6 |
int, |
| 7 |
lambda value: "FACTORY({})".format(value) |
| 8 |
]) |
| 9 |
def test_all_annotation_types(check_parse, check_help_text, type_object): |
| 10 |
# type_object is either `int` or a factory function that converts "str" to |
| 11 |
# "FACTORY(str)" |
| 12 |
def func( |
| 13 |
typed_arg: type_object, |
| 14 |
note_arg: "note_arg description", |
| 15 |
note_type: ("note_type description", type_object), |
| 16 |
type_note: (type_object, "type_note description")): pass |
| 17 |
|
| 18 |
check_help_text( |
| 19 |
lambda: check_parse(func, '-h'), |
| 20 |
"note_arg description", |
| 21 |
"note_type description", |
| 22 |
"type_note description") |
| 23 |
|
| 24 |
check_parse( |
| 25 |
func, |
| 26 |
"1", "2", "3", "4", |
| 27 |
typed_arg=type_object("1"), |
| 28 |
note_arg="2", |
| 29 |
note_type=type_object("3"), |
| 30 |
type_note=type_object("4")) |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.parametrize('bad_annotation', [ |
| 34 |
1000, # An int? What? |
| 35 |
{'foo': 'bar'}, # A dict isn't any better |
| 36 |
[int, 'fooo'], # For implementation ease we do ask for a tuple |
| 37 |
(), # Though the tuple should have things in it |
| 38 |
(int,), # More things |
| 39 |
(int, 'hello', 'world'), # TOO MANY THINGS |
| 40 |
(int, int), # The wrong kinds of things |
| 41 |
("hello", "world"), # Nope this is bad too |
| 42 |
(lambda value: value, lambda value: value), # Too many lambdas |
| 43 |
]) |
| 44 |
def test_bad_annotation(bad_annotation, check_parse): |
| 45 |
def func(arg: bad_annotation): pass |
| 46 |
|
| 47 |
with pytest.raises(AnnotationError): |
| 48 |
check_parse(func) |