-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
109 lines (91 loc) · 3.7 KB
/
tests.py
File metadata and controls
109 lines (91 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from typing import Any, Union, Optional, Callable, Iterable, Literal, Sized
from pytest import mark
from pyannotating import FormalAnnotation, AnnotationTemplate, input_annotation, Special, Subgroup, _InputAnnotationAnnotation
@mark.parametrize(
"input_resource, result",
[(42, 42), (Any, Any), (int | float, int | float)]
)
def test_formal_annotation(input_resource: Any, result: Any):
assert FormalAnnotation()[input_resource] == result
@mark.parametrize(
'doc',
["Documentation", "\n\t.!#@$2*-_\n", b"\tHello\n\tWorld\n\t!", str()]
)
def test_formal_annotation_documenting(doc: str):
assert FormalAnnotation(doc).__doc__ == doc
@mark.parametrize('number_of_creation', [8])
def test_input_annotation_annotation_loneliness(number_of_creation: int):
annotations = tuple(_InputAnnotationAnnotation() for _ in range(number_of_creation))
for first_annotation_index in range(number_of_creation):
for second_annotation in annotations[first_annotation_index + 1:]:
assert annotations[first_annotation_index] is second_annotation
@mark.parametrize(
"type_to_group",
[int, float, int | float, Union[set, frozenset], Optional[tuple], _InputAnnotationAnnotation]
)
def test_input_annotation_annotation_grouping(type_to_group: type):
annotation = _InputAnnotationAnnotation()
assert annotation | type_to_group == Union[annotation, type_to_group]
assert type_to_group | annotation == Union[type_to_group | annotation]
@mark.parametrize(
"annotation_template, input_resource, result",
[
(AnnotationTemplate(Optional, [input_annotation]), int, Optional[int]),
(
AnnotationTemplate(Callable, [[input_annotation | int, input_annotation], Any]),
float,
Callable[[float | int, float], Any]
),
(
AnnotationTemplate(
Callable,
[[input_annotation], AnnotationTemplate(Optional, [input_annotation])]
),
str,
Callable[[str], Optional[str]]
),
(
AnnotationTemplate(
Callable,
[[AnnotationTemplate(Iterable, [input_annotation])], Any]
),
int,
Callable[[Iterable[int]], Any]
),
(
AnnotationTemplate(
Callable,
[[AnnotationTemplate(Iterable, [input_annotation])], Iterable[int]]
),
int,
Callable[[Iterable[int]], Iterable[int]]
),
(
AnnotationTemplate(Iterable, [input_annotation | Ellipsis]),
Literal[256],
Iterable[Literal[256] | Ellipsis]
),
]
)
def test_annotation_template(annotation_template: AnnotationTemplate, input_resource: Any, result: Any):
assert annotation_template[input_resource] == result
@mark.parametrize(
"input_resource, result",
[(int, Any), ((tuple | list, Iterable), Iterable), ((str, Sized), Sized)]
)
def test_special(input_resource: Any, result: Any):
assert Special[input_resource] == result
@mark.parametrize(
"subgroup, instance, result_of_checking",
[
(Subgroup(object, lambda _: True), 1, True),
(Subgroup(object, lambda _: True), None, True),
(Subgroup(object, lambda _: False), 256, False),
(Subgroup(int, lambda number: number > 0), 5, True),
(Subgroup(int, lambda number: number > 0), -42, False),
(Subgroup(int, lambda number: number > 0), 6.4, False),
(Subgroup(int | float, lambda number: number > 0), 6.4, True),
]
)
def test_subgroup(subgroup: Subgroup, instance: Any, result_of_checking: bool):
assert isinstance(instance, subgroup) is (instance in subgroup) is result_of_checking