-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_typefire.py
More file actions
106 lines (75 loc) · 2.79 KB
/
test_typefire.py
File metadata and controls
106 lines (75 loc) · 2.79 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
from typefire import TypeFire, typefire, typeswitch, Agreement, Switch
import fire
import pathlib
def add(x, y):
return x + y
@typefire(Agreement(Switch(str, pathlib.Path, lambda p: pathlib.Path(p))))
class BrokenCalculator:
def __init__(self, offset=0):
self._offset = offset
def add(self, x, y):
return x + y + self._offset
def multiply(self, x, y):
return x * y + self._offset
def hint(self, path: pathlib.Path):
print(path, type(path))
return path
@staticmethod
def static_add(x: str, y: str):
print(type(x), type(y))
return x + y
@classmethod
def class_add(cls, x: str, y: str):
return x + y
@property
def getoffset(self):
return self._offset
def test_typefire():
cmd = "--x=1 --y=2"
assert typefire()(add)(cmd) == fire.Fire(add, cmd)
def test_typefire_help():
cmd = "--help"
assert typefire()(BrokenCalculator)(cmd) == fire.Fire(BrokenCalculator, cmd)
def test_typeswitch():
@typeswitch(Agreement(Switch(str, pathlib.Path, lambda p: pathlib.Path(p))))
def main(path: pathlib.Path, *args, **kwargs):
print(path, type(path))
return path
assert main("/Users/joe/Desktop/test.txt") == pathlib.Path(
"/Users/joe/Desktop/test.txt"
)
def test_typefire_class():
cmd = "--offset=1 - add --x=1 --y=2"
assert typefire()(BrokenCalculator)(cmd) == fire.Fire(BrokenCalculator, cmd)
def test_typefire_class_subclass():
cmd = "add --x=1 --y=2"
sub = BrokenCalculator(1)
assert typefire()(sub)(cmd) == fire.Fire(sub, cmd)
def test_typefire_class_subclass_method():
cmd = "hint --path=/Users/joe/Desktop/test.txt"
sub = BrokenCalculator(1)
agm = Agreement(Switch(str, pathlib.Path, lambda p: pathlib.Path(p)))
assert typefire(agm)(sub)(cmd) == fire.Fire(sub, cmd)
def test_typefire_class_subclass_method_static():
cmd = "static_add --x=1 --y=2"
sub = BrokenCalculator(1)
agm = Agreement(Switch(int, str, lambda p: str(p)))
assert typefire(agm)(sub)(cmd) == fire.Fire(sub, cmd)
def test_typefire_class_subclass_method_class():
cmd = "class_add --x=1 --y=2"
agm = Agreement(Switch(int, str, lambda p: str(p)))
assert typefire(agm)(BrokenCalculator)(cmd) == fire.Fire(BrokenCalculator, cmd)
def test_typefire_class_subclass_method_property():
cmd = "getoffset"
sub = BrokenCalculator(1)
# agm = Agreement(Switch(int, str, lambda p: str(p)))
assert typefire()(sub)(cmd) == fire.Fire(sub, cmd)
# test_typefire()
# test_typefire_help()
# test_typeswitch()
# test_typefire_class()
# test_typefire_class_subclass()
# test_typefire_class_subclass_method()
# test_typefire_class_subclass_method_static()
# test_typefire_class_subclass_method_class()
# test_typefire_class_subclass_method_property()